問題描述
我連接到一個 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 - 游標狀態無效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!