DBA > Job Interview Questions > DERBY Java Database FAQs

How to Scroll Insensitive ResultSets?

More DBA job interview questions and answers at http://dba.fyicenter.com/Interview-Questions/

(Continued from previous question...)

How to Scroll 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");

(Continued on next question...)

Other Job Interview Questions