java SQLSTATE 24000 - 无效的游标状态

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

SQLSTATE 24000 - Invalid Cursor State

javasqljdbcdb2

提问by Bastaix

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

我连接到 DB2 数据库并进行以下查询。我不明白为什么会收到错误消息:“无效的游标状态”。

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.

谢谢你。

回答by Chris Aldrich

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.

在 JDBC Javadocs 上找到了 Statement 接口:“用于执行静态 SQL 语句并返回它产生的结果的对象。

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

默认情况下,每个 Statement 对象只能同时打开一个 ResultSet 对象。因此,如果一个 ResultSet 对象的读取与另一个的读取交错,则每个对象都必须由不同的 Statement 对象生成。如果存在打开的对象,则 Statement 接口中的所有执行方法都会隐式关闭语句的当前 ResultSet 对象。" 见声明 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.

所以在我看来,如果你想同时打开两个 ResultSets,你需要两个不同的 Statements。或者您需要完成第一个 ResultSet 的处理并关闭它,以便您可以重新使用该语句来创建第二个 ResultSet。