標籤:
程式如下:
--第一個類:
package com.StudentCurriculavariable;
import java.sql.*;
public class JDBC_Connection {
static String drivername="sun.jdbc.odbc.JdbcOdbcDriver";
static String url="jdbc:odbc:chong";
static String username="sa";
static String password="123456";
static{
try{
Class.forName(drivername);
System.out.println("建立成功!");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn=null;
try{
conn=DriverManager.getConnection(url,username,password);
System.out.println("串連資料庫成功!");
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public static void free(ResultSet rs,Connection conn,Statement stmt){
try{
if(rs!=null)
rs.close();
}catch(SQLException e){
System.out.println("關閉ResultSet失敗!");
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
System.out.println("關閉Connection失敗!");
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException e){
System.out.println("關閉Statement失敗!");
e.printStackTrace();
}
}
}
}
}
--第二個類:
package com.StudentCurriculavariable;
public class UserVo {
private static String name;
private static String id;
private static String sex;
private static int age;
private static String address;
private static String xuanclass;
private static String room;
private static String username;
private static int password;
public static String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
public static String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
public static String getRoom(){
return room;
}
public void setRoom(String room){
this.room=room;
}
public static String getXuanclass(){
return xuanclass;
}
public void setXuanclass(String xuanclass){
this.xuanclass=xuanclass;
}
public static String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
public static String getId(){
return id;
}
public void setId(String id){
this.id=id;
}
public static String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public static int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public static int getPassword(){
return password;
}
public void setPassword(int password){
this.password=password;
}
}
--第三個類:
package com.StudentCurriculavariable;
import java.sql.*;
public class AddUser {
public void add(UserVo userVo){
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
try{
conn=JDBC_Connection.getConnection();
String sql="INSERT INTO stu(name,id,sex,age,address,xuanclass,room,username,password) VALUES(?,?,?,?,?,?,?,?,?)";
pstm=conn.prepareStatement(sql);
pstm.setString(1,UserVo.getName());
pstm.setString(2,UserVo.getId());
pstm.setString(3,UserVo.getSex());
pstm.setInt(4,UserVo.getAge());
pstm.setString(5,UserVo.getAddress());
pstm.setString(6,UserVo.getXuanclass());
pstm.setString(7,UserVo.getRoom());
pstm.setString(8,UserVo.getUsername());
pstm.setInt(9,UserVo.getPassword());
System.out.println("資料添加成功!");
}catch(Exception e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs, conn, pstm);
}
}
public static void main(String[] args){
AddUser adduser=new AddUser();
UserVo uservo=new UserVo();
String name="趙六";
String id="00006";
String sex="男";
int age=19;
String address="北京海澱區";
String xuanclass="資料結構";
String room="軟體工程14-1";
String username="趙六";
int password=123;
uservo.setName(name);
uservo.setId(id);
uservo.setSex(sex);
uservo.setAge(age);
uservo.setAddress(address);
uservo.setXuanclass(xuanclass);
uservo.setRoom(room);
uservo.setUsername(username);
uservo.setPassword(password);
adduser.add(uservo);
}
}
執行程式後顯示執行成功,但在資料庫中沒有該資料,問題出在哪裡?
用Java向資料庫中添加資料成功,但在資料庫中找不到添加的資料