【问题标题】:Resultset not open. Verify Autocommit is OFF. Apache Debry结果集未打开。验证自动提交是否关闭。阿帕奇德布里
【发布时间】:2011-05-02 06:31:43
【问题描述】:

我正在为我的数据库使用 apache derby。我能够对数据库执行插入操作。以下是尝试显示我唯一的表“MAINTAB”的内容的代码摘录。 java.sql.Connection 的实例是'dbconn'。

    ResultSet word;

    Statement query;

    String getData="SELECT THEWORD FROM MAINTAB";
    try{
        System.out.println(dbconn.getAutoCommit());
        query = dbconn.createStatement();
        word = query.executeQuery(getData);
        query.close();

        dbconn.setAutoCommit(false);
        System.out.println(dbconn.getAutoCommit());

        for(;word.next();)
            System.out.println(word.getString(1));

    }catch(Throwable e){
        System.out.println("Table fetch failed or result data failed");}

以下是输出。

org.apache.derby.jdbc.EmbeddedDriver loaded.
Database testDB connected
true
false
Table fetch failed or result data failed

---SQLException Caught---

SQLState:   XCL16
Severity: 20000
Message:  ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF.
java.sql.SQLException: ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedResultSet.checkIfClosed(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedResultSet.getString(Unknown Source)
Closed connection
    at test.ShowData.main(ShowData.java:30)
Caused by: java.sql.SQLException: ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is OFF.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(



Unknown Source)
    ... 9 more
Database shut down normally

当它首先要求验证 AUTOCOMMIT 是否为 OFF 时,我从 Derby 文档中发现 AUTOCOMMIT 在默认情况下对任何连接都是打开的。因此,我使用 dbconn.setAutoCommit(false) 将其关闭。尽管如此,还是会抛出错误。

错误前的输出说明结果集被提取而没有任何错误。另外,请注意即使我没有将 AutoCommit 设置为 false,也会引发相同的错误。之间,我在 Eclipse 上运行 derby。

【问题讨论】:

    标签: java sql derby


    【解决方案1】:

    问题是您在读取结果集之前关闭了查询。关闭查询,关闭结果集,因此会出现“ResultSet not open”错误。您应该在最后关闭查询,在 finally 块中:

    ResultSet word;
    
    Statement query=null;
    
    String getData="SELECT THEWORD FROM MAINTAB";
    try{
        System.out.println(dbconn.getAutoCommit());
        query = dbconn.createStatement();
        word = query.executeQuery(getData);
    
    
        dbconn.setAutoCommit(false);
        System.out.println(dbconn.getAutoCommit());
    
        for(;word.next();)
            System.out.println(word.getString(1));
    
    }catch(Throwable e){
        System.out.println("Table fetch failed or result data failed");
    } finally{
        if(query!=null) {
            try {
                 query.close();
            }
            catch(SQLException ex) {
                  System.out.println("Could not close query");
            }
       }
    }
    

    【讨论】:

    • 是的,现在可以使用了!非常感谢。我挣扎了半天!
    • 哦,所以,我是这样理解的——关闭查询,关闭结果集。这就是错误提示结果集未打开的原因。
    【解决方案2】:

    对我来说,关闭的是 Connection 对象。所以下次考虑使用现有的 Connection 对象。

    相反,我每次进行新查询时都会使用它。

     private Connection getConnect() {
        try {
            return DriverManager.getConnection(Utils.getDatabaseConnection(), null);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    

    然后对 ex 进行空检查。

    getConnect().createStatement();
    

    【讨论】:

      【解决方案3】:

      你可以通过创建另一个语句变量来初始化它 con.createStatement();

      Statement stmt = con.createStatement();
      Statement stmt2 = con.createStatement();
      stmt.executeQuery(" your first query goes here ");
      stmt2.executeQuery("your second query goes here");
      

      使用第二个 Statement 变量执行第二个查询。 适合初学者的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多