用Java向資料庫中添加資料成功,但在資料庫中找不到添加的資料

來源:互聯網
上載者:User

標籤:

程式如下:
--第一個類:
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向資料庫中添加資料成功,但在資料庫中找不到添加的資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.