tags:

views:

62

answers:

1

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.

+1  A: 

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.

Chris Aldrich