0

I am trying to select records and view it in a JTable. My records are in Ms Access database locally in my computer. While trying to run the program i am getting an Null point Exception error, pointing my Prepared Statement. Below is my code

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String sourceURL =
                "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:/My Documents/NetBeansProjects/VictoriaMilk/3.accdb;";
    Connection li = DriverManager.getConnection(sourceURL, "Admin", "");
    System.out.println("Connection is: "+li);
    PreparedStatement pstm = conn.prepareStatement("SELECT * FROM Milk");
    // SLNO, NAMES, QTY, RATE, Deductns, BalPaid
    ResultSet Rs = pstm.executeQuery();

    while (Rs.next()) {
        model.addRow(new Object[]{Rs.getInt(1), Rs.getString(2), Rs.getString(3), Rs.getString(4), Rs.getString(5),
        Rs.getString(6), Rs.getString(7)});
    }

} catch (ClassNotFoundException ex) {
    Logger.getLogger(ResultTable.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException sqle) {
    System.out.println(sqle);
}
10
  • 2
    Where is conn defined? I see li which is what I would have expected you to use. Commented Jan 7, 2019 at 15:13
  • please share complete error log Commented Jan 7, 2019 at 15:14
  • @stdunbar suggesting making an answer out of that Commented Jan 7, 2019 at 15:14
  • I've put like this private static Connection conn; Commented Jan 7, 2019 at 15:15
  • Ok, then where is conn assigned? Commented Jan 7, 2019 at 15:16

1 Answer 1

1

You are opening a new connection and assigning to local li variable but moment later you are attempting to private static Connection con which is null. Use the connection from li variable:

Connection li = DriverManager.getConnection(sourceURL, "Admin", "");

PreparedStatement pstm = li.prepareStatement("SELECT * FROM Milk");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.