SQLSTATE 24000 - 无效的游标状态 [英] SQLSTATE 24000 - Invalid Cursor State

查看:3007
本文介绍了SQLSTATE 24000 - 无效的游标状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到一个DB2数据库并进行以下查询。我不明白为什么我得到错误:无效光标状态。

  public static void blivPar(){
try {
语句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语句并返回其生成的结果的对象。 / p>

默认情况下,每个Statement对象只能有一个ResultSet对象同时打开,因此如果读取一个ResultSet对象与读取另一个,每个都必须由不同的Statement对象生成。 Statement接口中的所有执行方法隐式关闭一个状态当前的ResultSet对象(如果存在一个open)。
请参阅声明javadoc



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


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.

解决方案

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.

这篇关于SQLSTATE 24000 - 无效的游标状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆