我正在尝试选择记录并在 JTable 中查看它。我的记录位于计算机本地的 Ms Access 数据库中。在尝试运行程序时,我收到空点异常错误,指向我的准备语句。下面是我的代码
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);
}
您正在打开一个新连接并分配给局部li
变量,但片刻之后您正在尝试private static Connection con
哪个是null
。使用来自li
变量的连接:
Connection li = DriverManager.getConnection(sourceURL, "Admin", "");
PreparedStatement pstm = li.prepareStatement("SELECT * FROM Milk");