java.lang.NullPointerException 在 conn.prepareStatement(sql)
java.lang.NullPointerException on conn.prepareStatement(sql)
我正在开发一个将 postgresql 与 glassfish 结合使用的项目。当我执行语句 PreparedStatement pstmt = conn.prepareStatement(sql)
它给我 java.lang.NullPointerException.
这是我的代码:
String sql = "DELETE FROM blocklist WHERE ? >= time + ?;";
try(Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql))
{
pstmt.setLong(1, System.currentTimeMillis());
pstmt.setLong(2, blocktime);
pstmt.executeUpdate();
}
catch(SQLException ex)
{
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
connect() 函数在这里,在 Connection conn = this.connect();
:
被调用
private Connection connect()
{
Connection con = null;
try
{
con = DriverManager.getConnection(Config.dburl, Config.dbusername, Config.dbpassword);
}
catch(SQLException ex)
{
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
这是异常日志:
Severe: Exception in thread "AWT-EventQueue-0"
Severe: java.lang.NullPointerException
at urrsm.sng.RecordManager.unblockrun(RecordManager.java:91)
at urrsm.sng.WebScocketEnd.actionPerformed(WebScocketEnd.java:45)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
无法使用准备好的语句在查询中绑定实际的列名。只能绑定文字值。尝试按如下方式改写您的删除查询:
String sql = "DELETE FROM blocklist WHERE time <= ?;";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setLong(1, System.currentTimeMillis() - blocktime);
pstmt.executeUpdate();
}
catch (SQLException ex) {
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
我正在开发一个将 postgresql 与 glassfish 结合使用的项目。当我执行语句 PreparedStatement pstmt = conn.prepareStatement(sql)
它给我 java.lang.NullPointerException.
这是我的代码:
String sql = "DELETE FROM blocklist WHERE ? >= time + ?;";
try(Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql))
{
pstmt.setLong(1, System.currentTimeMillis());
pstmt.setLong(2, blocktime);
pstmt.executeUpdate();
}
catch(SQLException ex)
{
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
connect() 函数在这里,在 Connection conn = this.connect();
:
private Connection connect()
{
Connection con = null;
try
{
con = DriverManager.getConnection(Config.dburl, Config.dbusername, Config.dbpassword);
}
catch(SQLException ex)
{
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
这是异常日志:
Severe: Exception in thread "AWT-EventQueue-0"
Severe: java.lang.NullPointerException
at urrsm.sng.RecordManager.unblockrun(RecordManager.java:91)
at urrsm.sng.WebScocketEnd.actionPerformed(WebScocketEnd.java:45)
at javax.swing.Timer.fireActionPerformed(Timer.java:313)
at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
无法使用准备好的语句在查询中绑定实际的列名。只能绑定文字值。尝试按如下方式改写您的删除查询:
String sql = "DELETE FROM blocklist WHERE time <= ?;";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setLong(1, System.currentTimeMillis() - blocktime);
pstmt.executeUpdate();
}
catch (SQLException ex) {
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}