Thiv
Thiv

Reputation: 9

Java & Access Database

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

Upvotes: 0

Views: 69

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

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

Upvotes: 1

Related Questions