结果集未打开。验证自动提交是否关闭。 Apache Debry

时间:2010-10-29 07:06:50

标签: java sql derby

我正在使用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是否关闭时,我从Derby文档中发现默认情况下AUTOCOMMIT已打开为任何连接。所以,我使用dbconn.setAutoCommit(false)将其关闭。仍然,错误被抛出。

错误前的输出说明结果集是在没有任何错误的情况下获取的。另请注意,即使我没有将AutoCommit设置为false,也会抛出相同的错误。之间,我在日食上跑德比。

3 个答案:

答案 0 :(得分:16)

问题是您在读取结果集之前已经关闭了查询。关闭查询,关闭结果集,从而导致“ResultSet未打开”错误。您应该在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");
        }
   }
}

答案 1 :(得分:0)

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

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

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

,然后用null检查ex。

getConnect().createStatement();

答案 2 :(得分:0)

您可以通过创建另一个用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变量执行第二个查询。 面向初学者的解决方案。