gpt4 book ai didi

conn.prepareStatement(sql) 上的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 10:47:40 24 4
gpt4 key购买 nike

我正在开发一个使用 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$1.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.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.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)

最佳答案

无法使用准备好的语句在查询中绑定(bind)实际的列名称。只能绑定(bind)文字值。尝试按如下方式重新表述您的删除查询:

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

关于conn.prepareStatement(sql) 上的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52435614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com