java 无效操作:结果集已关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10993975/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:19:55  来源:igfitidea点击:

Invalid operation: result set is closed

javajakarta-eejdbc

提问by happy

When my resultset data is large I get com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][4.9.78] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=nullexception but not when I try with less amount of data in resultset

当我的结果集数据很大时,我会遇到 com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][4.9.78] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null异常,但当我尝试在结果集中使用较少的数据时不会

Below is my code snippet

下面是我的代码片段

ResultSet rs=null;
String sql_query="select * from exception_main;select * from m_roles"
String query1=sql_query.toUpperCase();
String[] results=query1.split(";");         
CSVWriter writer = new CSVWriter(new FileWriter(csv_file_path + csv_file_name), ',',CSVWriter.NO_QUOTE_CHARACTER);                     
for(int i=0;i<results.length;i++)                         
{                             
  if(results[i].startsWith("SELECT"))                             
  {                                 
    System.out.println("Inside select"+ results[i]);                             

    ps = conn1.prepareStatement(results[i].toString());                             
    rs = ps.executeQuery();             

    ...                        

    //writing to csv file                          
    System.out.println("Count..." + rs.getRow());                        
    writer.writeAll(rs, true);                         

    while(rs.next()){                                  
      rs.deleteRow();                                
    }                          
    System.out.println("Count...3:::::::" + rs1.getRow());     
  }     
}
writer.close(); 
rs.close();

回答by anirudha

Keep

保持

stmt = conn1.createStatement(); 

before the for loop.

在 for 循环之前。

回答by Kazekage Gaara

From the Java docs:

Java 文档

A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.

当 Statement 对象关闭、重新执行或用于从多个结果的序列中检索下一个结果时,ResultSet 对象会被生成它的 Statement 对象自动关闭。

Hence your error.

因此你的错误。

Your stmtStatement object is being re-executed everytime in your forloop.

stmtStatement对象是在你重新执行每次for循环。

Also, from here:

此外,从这里

For non-held cursors, when the driver finishes stepping through the 1st ResultSet, the driver will flow commit if autoCommit is on, and that will close everything else. If you want the other ResultSet remain open, you may want to use the with hold cursor.

对于非持有游标,当驱动程序完成第一个 ResultSet 的单步执行时,如果 autoCommit 处于打开状态,则驱动程序将流提交,这将关闭其他所有内容。如果您希望其他 ResultSet 保持打开状态,您可能需要使用 with hold 光标。



EDIT:

编辑:

(to the OP - kindly post one snippet. Don't keep changing snippets all the time) The answer still is the same : you are using two queries on the same ResultSet, in your forloop. That is not going to work. The ResultSetwill be closed if you re-execute the same Statementobject with different queries. Either use different Statementobjects, or use a different approach and drop this forloop idea.

(给 OP - 请发布一个片段。不要一直更改片段)答案仍然相同:您ResultSetfor循环中对同一个,使用两个查询。那是行不通的。在ResultSet将被关闭,如果你重新执行相同Statement与不同的查询对象。要么使用不同的Statement对象,要么使用不同的方法并放弃这个for循环想法。