这是用于自定义连接的 JDBC 代码。我正在使用运行正常的 sql 5 数据库服务器。我已在 net beans 7.3 版上尝试过此代码。它包含 3 个类
public class MyDbOperation {
private static Connection c;
private static Statement st;
private static ResultSet rs;
private static ArrayList al;
private static int m;
private static Student student;
static {
try{
Class.forName("com.mysql.jdbc.Driver");
c= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/stp13","root","@Ashish123");
st=c.createStatement();
}catch(Exception e){}
}
public static int getInsert(Student s) throws SQLException {
m=st.executeUpdate("insert into student values("+s.getRollNo()+",'"+s.getFname()+"','"+s.getLname()+"','"+s.getEmail()+"',"+s.getMob()+",'"+s.getStreet()+"','"+s.getCity()+"')");
return m;
}
}
当我执行代码时,出现以下错误
java.lang.NullPointerException
at stp13.MyDbOperation.getInsert(MyDbOperation.java:44)
at stp13.Stp13hbt.main(Stp13hbt.java:24)
代码有什么问题?如何解决?