我试图选择记录并在JTable中查看它。我的记录在我计算机本地的Ms Access数据库中。尝试运行该程序时,我收到一个Null point Exception错误,指向我的Prepared语句。 下面是我的代码
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);
}
答案 0 :(得分:1)
您正在打开一个新连接并分配给本地li
变量,但稍后,您尝试尝试private static Connection con
的{{1}}。使用来自null
变量的连接:
li