org.h2.jdbc.JdbcSQLNonTransientException:对象已关闭 [90007-200]

Posted

技术标签:

【中文标题】org.h2.jdbc.JdbcSQLNonTransientException:对象已关闭 [90007-200]【英文标题】:org.h2.jdbc.JdbcSQLNonTransientException: The object is already closed [90007-200] 【发布时间】:2021-11-12 17:13:25 【问题描述】:

从 h2 数据库中遇到这个错误,它工作正常,直到现在我明白连接已关闭如何防止这种情况。

org.h2.jdbc.JdbcSQLNonTransientException: 对象已经关闭 [90007-200]

    ResultSet rest = st.executeQuery("SELECT * FROM monthly_summary WHERE id=(SELECT MAX(id) FROM monthly_summary);");
    rest.next();
    if(rest.getInt(3) == month && rset.getInt(4) == year) 
        double oldAmount = rest.getDouble(2);
        st.execute("UPDATE monthly_summary SET amount =" + (oldAmount + amount)  + ";");
     else 
        st.execute("INSERT INTO monthly_summary(amount, `month`, `year`) VALUES (" + amount + "," + month + "," + year + ");");
    

【问题讨论】:

此链接应该可以帮助您了解ResultSet.next() 的行为方式。 https://www.tutorialspoint.com/java-resultset-next-method-with-example 【参考方案1】:

ResultSet::next 需要在 if 语句中:

ResultSet rest = st.executeQuery("SELECT * FROM monthly_summary WHERE id=(SELECT MAX(id) FROM monthly_summary);");
if (rest.next()) 
    if(rest.getInt(3) == month && rset.getInt(4) == year) 
        double oldAmount = rest.getDouble(2);
        st.execute("UPDATE monthly_summary SET amount =" + (oldAmount + amount)  + ";");
     else 
        st.execute("INSERT INTO monthly_summary(amount, `month`, `year`) VALUES (" + amount + "," + month + "," + year + ");");
    

【讨论】:

请解释一下这段代码究竟是如何解决这个问题的。 我不太清楚如何解释只是 ResultSet 需要在 if 语句中是很常见的错误。如果你有更好的解释,我会很高兴阅读。

以上是关于org.h2.jdbc.JdbcSQLNonTransientException:对象已关闭 [90007-200]的主要内容,如果未能解决你的问题,请参考以下文章