问题描述
我连接到一个 DB2 数据库并进行以下查询.我不明白为什么会出现错误:无效的游标状态".
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();
}
}
谢谢.
推荐答案
在 JDBC Javadocs 上找到了 Statement 接口的这个:用于执行静态 SQL 语句并返回它产生的结果的对象.
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.
默认情况下,每个 Statement 对象只能同时打开一个 ResultSet 对象.因此,如果一个 ResultSet 对象的读取与另一个 ResultSet 对象的读取交错,则每个都必须由不同的 Statement 对象生成. Statement 接口中的所有执行方法都会隐式关闭一个 statment 的当前 ResultSet 对象,如果存在打开的对象."请参阅 声明 javadoc
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
所以在我看来,如果您希望同时打开两个结果集,您需要两个不同的语句.或者您需要完成第一个 ResultSet 的处理并关闭它,以便您可以重新使用 Statement 来创建第二个 ResultSet.
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.
这篇关于SQLSTATE 24000 - 游标状态无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!