我试图使用 jdbc 将值从文本字段插入到 mySql,但我的问题是我在关闭准备好的语句时出现空指针错误。这是我的代码
private Connection con;
private Statement stm;
private ResultSet rs;
private PreparedStatement ps ;
public void dbConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
+ "", "root", "password");
}
catch(ClassNotFoundException ex){}
catch(SQLException ex){}
}
public boolean insert(Member member)throws SQLException{
this.dbConnect();
boolean success = false;
String query = "INSERT INTO try VALUES(?, ?)";
try {
this.ps = this.con.prepareStatement(query);
this.ps.setString(1, member.getMemberId());
this.ps.setString(2, member.getFirstName());
int rows = this.ps.executeUpdate();
if(rows>0){
success = true;
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} finally{
this.ps.close(); // this is my line: 56 (im getting null pointer here)
this.con.close();
}
return success;
}
这是堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at rtu.tomis.dao.MemberDao.insert(MemberDao.java:56)
at rtu.tomis.gui.AddMemberView.btnSaveActionPerformed(AddMemberView.java:993)
at rtu.tomis.gui.AddMemberView.access$800(AddMemberView.java:21)
at rtu.tomis.gui.AddMemberView$9.actionPerformed(AddMemberView.java:476)
谢谢你的帮助