java 结果集未打开。验证自动提交已关闭。阿帕奇德布里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4049705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resultset not open. Verify Autocommit is OFF. Apache Debry
提问by Sundeep
I am using apache derby for my database. I am able to perform inserts into the database. The following is the excerpt from the code that attempts to display the contents of my only table 'MAINTAB'. The instance of java.sql.Connection is 'dbconn'.
我正在为我的数据库使用 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");}
And the following is the output.
以下是输出。
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
When, it first asked to verify if AUTOCOMMIT is OFF, I have found from the Derby Documentation that AUTOCOMMIT is turned ON by default to any connection. So, I've turned it off using dbconn.setAutoCommit(false). Still, the error is thrown.
当它第一次要求验证 AUTOCOMMIT 是否关闭时,我从 Derby 文档中发现,默认情况下,任何连接的 AUTOCOMMIT 都是打开的。因此,我已使用 dbconn.setAutoCommit(false) 将其关闭。尽管如此,还是会抛出错误。
The output before the error explains that the result set was fetched without any error. Also, please note that the same error is thrown even if I do not set the AutoCommit to false. Between, I am running derby on eclipse.
错误前的输出解释了结果集被获取而没有任何错误。另外,请注意,即使我没有将 AutoCommit 设置为 false,也会引发相同的错误。在此期间,我正在 Eclipse 上运行 derby。
回答by dogbane
The problem is that you have closed your query beforereading your resultset. Closing the query, closes the resultset, hence why you get the "ResultSet not open" error. You should close the query right at the end, in a finally
block:
问题是您在阅读结果集之前关闭了查询。关闭查询会关闭结果集,因此您会收到“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");
}
}
}
回答by vikas kumar
for me, it was the Connection object that got closed. so next time think about using your existing Connection object.
对我来说,是 Connection 对象关闭了。所以下次考虑使用现有的 Connection 对象。
instead, I Use this everytime I make a new Query.
相反,我每次创建新查询时都会使用它。
private Connection getConnect() {
try {
return DriverManager.getConnection(Utils.getDatabaseConnection(), null);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
and then do whatever with null check for ex.
然后对ex进行空检查。
getConnect().createStatement();
回答by Faisal Mushtaq
You can do this by creating another statement variable initializing it with con.createStatement();
您可以通过创建另一个语句变量来实现这一点,使用 con.createStatement(); 对其进行初始化;
Statement stmt = con.createStatement();
Statement stmt2 = con.createStatement();
stmt.executeQuery(" your first query goes here ");
stmt2.executeQuery("your second query goes here");
Execute second query with second Statement variable. A solution for beginners.
使用第二个 Statement 变量执行第二个查询。初学者的解决方案。