Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.h2.jdbc.JdbcSQLException:对象已关闭_Java_H2_Resultset - Fatal编程技术网

Java org.h2.jdbc.JdbcSQLException:对象已关闭

Java org.h2.jdbc.JdbcSQLException:对象已关闭,java,h2,resultset,Java,H2,Resultset,就我的一生而言,我看不出它是如何“已经关闭”的 以下是输出: success. querying database for latest values... 103 Exception in thread "main" org.h2.jdbc.JdbcSQLException: The object is already closed [90007-196] at org.h2.message.DbException.getJdbcSQLException(DbException.ja

就我的一生而言,我看不出它是如何“已经关闭”的

以下是输出:

success.  querying database for latest values...
103
Exception in thread "main" org.h2.jdbc.JdbcSQLException: The object is already closed [90007-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.message.DbException.get(DbException.java:144)
    at org.h2.jdbc.JdbcResultSet.checkClosed(JdbcResultSet.java:3208)
    at org.h2.jdbc.JdbcResultSet.next(JdbcResultSet.java:130)
    at RsetTest2.main(RsetTest2.java:22)
其中22对应于“while(rset.next()){”行

DB正在返回值,请参阅println语句,它给出了103

更奇怪的是,如果我//注释掉executeUpdate行,它会正常完成

线程“main”org.h2.jdbc.JdbcSQLException中出现异常:对象已关闭[90007-196]

您的问题是在
while
循环中重用SQL
语句
。只要在循环中调用
qry.executeUpdate(…)
方法,与上一条语句关联的
ResultSet rset
就会关闭,因此会出现错误。这是
while(rset.next())
语句,在失败的循环中的第一个
executeUpdate(…)
之后调用

如果在循环中使用新语句,那么它应该可以工作

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

您可以考虑保留必要的更新集合,然后在循环的末尾发布更新。< /P> 更奇怪的是,如果我//注释掉executeUpdate行,它会正常完成


是的,听起来不错。一点也不奇怪。:-

为了简化代码,您可以使用尝试使用
java.lang.AutoCloseable
接口,因此您可以去掉以下行:

    rset.close();
    qry.close();  
整个街区可能看起来像这样:

        try (ResultSet rset = conn.createStatement().executeQuery("select id from CONSTITUENTS where manager = 'abc' limit 1")) {
            String insertSQL = "insert into PAYREQUESTS (constituent, inblock) values ('%d', 238)";
            while (rset.next()) {
                int id = rset.getInt("id");
                Savepoint savePoint = conn.setSavepoint("beforeInsert");
                try {
                    conn.createStatement().executeUpdate(String.format(insertSQL, id));
                    conn.commit();
                } catch (SQLException ex) {
                    conn.rollback(savePoint);
                    //log exception
                }
            }
        } catch (SQLException e) {
            //log exception
        }

由于您的连接的自动提交模式等于
false
,可能对可能的有害操作有意义。

,在关闭方法中,如果它已经关闭,则它应该是不可操作的。因此,可能是驱动器实现问题。这不是关闭@Yogesh\D,而是关闭后他仍在使用
rset
d、 明白了。while(rset.next())是导致问题的原因,而不是结束。谢谢!
        try (ResultSet rset = conn.createStatement().executeQuery("select id from CONSTITUENTS where manager = 'abc' limit 1")) {
            String insertSQL = "insert into PAYREQUESTS (constituent, inblock) values ('%d', 238)";
            while (rset.next()) {
                int id = rset.getInt("id");
                Savepoint savePoint = conn.setSavepoint("beforeInsert");
                try {
                    conn.createStatement().executeUpdate(String.format(insertSQL, id));
                    conn.commit();
                } catch (SQLException ex) {
                    conn.rollback(savePoint);
                    //log exception
                }
            }
        } catch (SQLException e) {
            //log exception
        }