[英]Why doesnt it get data from SQLite database?
I am trying to connect to a database located in my project directory and get data from it, but is give Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
error. 我正在尝试连接到位于项目目录中的数据库并从中获取数据,但是
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
错误中给出了Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
。
Here is my code: 这是我的代码:
public static Connection ConnectDB(){
Connection conn = null;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:database01s.sqlite");
JOptionPane.showMessageDialog(null, "Connected");
conn.setAutoCommit(false);
System.out.println("Opened database successfully");
ResultSet rs = stmt.executeQuery( "SELECT id FROM DAN" );
while(rs.next()){
System.out.println( rs.getInt("id") );
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException | SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null, e);
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
return null;
}
It does connect successfully, but it gives error when it reaches this line: ResultSet rs = stmt.executeQuery( "SELECT id FROM dan" );
它确实连接成功,但是到达此行时却给出错误:
ResultSet rs = stmt.executeQuery( "SELECT id FROM dan" );
And this is a picture of my database to to see if I am entering the right table and info: 这是我的数据库的图片,以查看我是否输入了正确的表和信息:
You are getting a null pointer exception because stmt is equal to null, and you are calling executeQuery() on it. 因为stmt等于null,并且您正在对其调用executeQuery(),所以您将获得null指针异常。 You can't call methods on a null object.
您不能在空对象上调用方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.