org.h2.jdbc.JdbcSQLException: The object is already closed

♀尐吖头ヾ 提交于 2019-12-06 16:34:12

Exception in thread "main" org.h2.jdbc.JdbcSQLException: The object is already closed [90007-196]

Your problem is that you are reusing the SQL Statement inside of your while loop. As soon as you call the qry.executeUpdate(...) method in the loop, the ResultSet rset associated with the previous statement is closed, hence the error. It is the while(rset.next()) statement that is called after the first executeUpdate(...) in the loop that fails.

If you use a new statement in the loop then it should work.

Statement qry = conn.createStatement();
String sql = "select id from CONSTITUENTS where manager = 'abc' limit 1";
ResultSet rset = qry.executeQuery(sql);
while (rset.next()) {
    int id = rset.getInt("id");
    System.out.println(id);
    // we can't reuse the same Statement here so we need to create a new one
    conn.createStatement().executeUpdate("insert into PAYREQUESTS ...");
}

You might consider keeping a collection of necessary updates and then issue the updates at the end of the loop.

and even weirder, if I // comment out the executeUpdate line, it all completes normally

Yep, that sounds right. Not weird at all. :-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!