SQLSTATE 24000 - Invalid Cursor State

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I connect to a DB2 database and makes the following query. I don't understand why I get the error: "invalid cursor state".

public static void blivPar() {             try {                 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                     ResultSet.CONCUR_UPDATABLE);                 stmt.setMaxRows(1000);                  ResultSet drenge = stmt.executeQuery("SELECT * FROM People WHERE sex='M'");                 ResultSet piger = stmt.executeQuery("SELECT * FROM People WHERE sex='F'");                 drenge.first();                 piger.first();                 int i=0;                 while(drenge.next()) {                     while(piger.next()) {                         i++;                         System.out.print(i);                         stmt.execute("INSERT INTO Couples Values ('"+drenge.getString(1) +                                 "','" + drenge.getString(2) +                                 "','" + piger.getString(1) +                                 "','" + piger.getString(2) + "')");                     }                 }             } catch (Exception ex) {                 ex.printStackTrace();             }          } 

Thank you.

回答1:

Found this on the JDBC Javadocs for the Statement interface: "The object used for executing a static SQL statement and returning the results it produces.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists. " see Statement javadoc

So it looks to me like you need two different Statements if you want two ResultSets open at the same time. Or you need to finish processing your first ResultSet and close it so you can re-use the Statement to create the second ResultSet.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!