首页 > 代码库 > 用Java向数据库中添加数据成功,但在数据库中找不到添加的数据

用Java向数据库中添加数据成功,但在数据库中找不到添加的数据

程序如下:
--第一个类:
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向数据库中添加数据成功,但在数据库中找不到添加的数据