apache > db
Apache DB Project
 
Font size:      

Scrolling Insensitive ResultSets

Scrolling Insensitive ResultSets

JDBC 2.0 adds a new kind of ResultSet, one that allows you to scroll in either direction or to move the cursor to a particular row. Derby implements scrolling insensitive ResultSets. When you use a scroll insensitive ResultSets cursor to facilitate the insensitive scrolling action, Derby materializes in memory all rows from the first one in the result set up to the one with the biggest row number.

//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())
    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();
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.close();
System.out.println("Closed connection");

Previous Page
Next Page
Table of Contents
Index