Search code examples
javasqlms-accessresultsetjdbc-odbc

Ms access "invalid record set status" error


I connect to my access database successfully and my sql is works and after these i have a result set. Code is below:

            Statement stmt = con.createStatement();
            String sqlStr ="select max(ID) from GuestBook ";
            ResultSet rset = stmt.executeQuery(sqlStr);

But when i want to get value from resultset like this;

            int id = rset.getInt(1);

or

            int id = rset.getInt("ID");//or "max(ID)"

I have a sql exception.

Exception is "invalid record set status"

How can i solve this problem?


Solution

  • A column name of "Expr1000" will be generated for max(ID). But I think that it is better to use an As clause to give it a well defined alias.

    You must call rset.next() in order to move to the first row, because initially, the cursor is positioned before the first row.

    Statement stmt = con.createStatement();
    String sqlStr ="SELECT max(ID) As LastID FROM GuestBook";
    ResultSet rset = stmt.executeQuery(sqlStr);
    int id = 0;
    if (rset.next()) {
        id = rset.getInt("LastID");
        // OR
        id = rset.getInt(1);
    }