Hi,

>Statement s=cn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
>           if (!rs.isFirst()) rs.previous(); 
>              %><tr> 
>              <td><%=rs.getString(1)%></td><%
>           }

>       [Microsoft][ODBC Driver Manager] Invalid cursor state

You never close your result set.  If you try executing this more than on the same 
connection, it will fail.  For 
some drivers (specifically NOT ODBC) you can use any number of new ResultSet objects 
over the same 
connection, but not reuse any one unless you close the result set.

MSAccess in particular, only supports one cursor per connection, and JDBC ResultSets 
attempt 
(unsuccessfully) to share that cursor.  Hence the "Invalid cursor state".

Also be careful with the cursor position (as reflected in the ResultSet): the initial 
position is before the first row, 
and the final position (if iterating forwards) will be after the last row.  Use 
rs.first() to move to the beginning of the 
ResultSet instead of iterating to get there.

Correct JDBC logic:

        Class.forName(DRIVER_CLASS);
        Connection con = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
                ResultSet.CONCUR_READ_ONLY);
        { // This can be done in a loop
                ResultSet r = stmt.executeQuery("SELECT * FROM MYTABLE");
                if (r != null) {
                        while (r.next()) {
                                // Do something with r.getString(1)
                        }
                        r.close();
                }
        }
        stmt.close();
        con.close();

Alternative for inner loop:
                ResultSet r = stmt.executeQuery("SELECT * FROM MYTABLE");
                if (r != null && r.last()) {
                        while (r.previous()) {
                                // Do something with r.getString(1)
                        }
                        r.close();
                }

Reply via email to