Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE 24000 - Invalid Cursor State

Tags:

java

sql

jdbc

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();
            }

        }

Thank you.

like image 916
Bastaix Avatar asked Oct 22 '25 03:10

Bastaix


1 Answers

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.

like image 53
Chris Aldrich Avatar answered Oct 23 '25 17:10

Chris Aldrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!