问题描述
我连接到 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();
}
}
谢谢.
推荐答案
在 Statement 接口的 JDBC Javadocs 上找到了这个:用于执行静态 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 对象与读取另一个对象交错进行,则每个对象都必须由不同的 Statement 对象生成. Statement 接口中的所有执行方法都会隐式关闭一个语句的当前 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
所以在我看来,如果您想同时打开两个 ResultSets,您需要两个不同的 Statements.或者您需要完成第一个 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 - 无效的游标状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!