samedi 11 avril 2015

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.


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" );


And this is a picture of my database to to see if I am entering the right table and info:image


Aucun commentaire:

Enregistrer un commentaire