【发布时间】: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。
【问题讨论】: