# Scrollable result sets

JDBC 2.0 adds two new types of result sets which allows you to scroll in either direction or to move the cursor to a particular row. Derby supports one of these types: scrollable insensitive result sets (`ResultSet.TYPE_SCROLL_INSENSITIVE`).

When you use a result set of type of type `ResultSet.TYPE_SCROLL_INSENSITIVE`, Derby materializes rows from the first one in the result set up to the one with the biggest row number as the rows are requested. The materialized rows will be backed to disk if necessary, to avoid excessive memory usage.

Insensitive result sets, in contrast to sensitive result sets, are insensitive to changes made by others on the rows which have been materialized. Derby allows updates of scrollable insensitive result sets, please see Visibilty of changes, which also explains visibility of own changes.

Note: Derby does not support result sets of type ResultSet.TYPE_SCROLL_SENSITIVE.

    //autocommit does not have to be off because even if
    //we accidentally scroll past the last row, the implicit commit
    //on the the statement will not close the result set because result sets
    //are held over commit by default
    conn.setAutoCommit(false);
    Statement s4 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                        ResultSet.CONCUR_READ_ONLY);
    s4.execute("set schema 'SAMP'");
    ResultSet scroller=s4.executeQuery(
        "SELECT sales_person, region, sales FROM sales " +
        "WHERE sales > 8 ORDER BY sales DESC");
    if (scroller.first()) { // One row is now materialized
        System.out.println("The sales rep who sold the highest number" +
                           " of sales is " +
                           scroller.getString("SALES_PERSON"));
    } else {
        System.out.println("There are no rows.");
    }
    scroller.beforeFirst();
    scroller.afterLast();   // By calling afterlast(), all rows will be materialized
    scroller.absolute(3);
    if (!scroller.isAfterLast()) {
        System.out.println("The employee with the third highest number " +
                           "of sales is " +
                           scroller.getString("SALES_PERSON") + ", with " +
                           scroller.getInt("SALES") + " sales");
    }
    if (scroller.isLast()) {
        System.out.println("There are only three rows.");
    }
    if (scroller.last()) {
        System.out.println("The least highest number " +
                           "of sales of the top three sales is: " +
                           scroller.getInt("SALES"));
    }
    scroller.close();
    s4.close();
    conn.commit()
    conn.close();
    System.out.println("Closed connection");

Parent topic: Result set and cursor mechanisms
Related concepts
Updatable result sets
Result sets and auto-commit
Related reference
Simple non-updatable result sets
Holdable result sets