Java Reference
In-Depth Information
7.10
Scrollable ResultSet s
In all our examples so far, movement through a ResultSet object has been confi ned
to the forward direction only, and even that has been restricted to moving by one
row at a time. In fact, JDBC 1 did not allow any other kind of movement, since the
only method available for moving through a ResultSet was next . With the emergence
of JDBC 2 in Java 2, however, a great deal more fl exibility was made available to
Java programmers by the introduction of the following ResultSet methods:
￿ boolean fi rst()
￿ boolean last()
￿ boolean previous()
￿ boolean relative (int <rows>)
￿ boolean absolute(int <rows>)
As with method next , the return value in each case indicates whether or not there
is data at the specifi ed position. The purposes of most of these methods are pretty
well self-evident from their names, but the last two probably need a little explanation.
Method relative takes a signed argument and moves forwards/backwards the
specifi ed number of rows. For example:
results.relative(-3); //Move back 3 rows.
Method absolute also takes a signed argument and moves to the specifi ed
absolute position, counting either from the start of the ResultSet (for a positive
argument) or from the end of the ResultSet (for a negative argument). For
example:
results.absolute(3);
//Move to row 3 (from start of ResultSet ).
Before any of these new methods can be employed, however, it is necessary to
create a scrollable ResultSet. This is achieved by using an overloaded form of the
Connection method createStatement that takes two integer arguments. Here is the
signature for this method:
Statement createStatement(int <resultSetType>,
int <resultSetConcurrency>)
There are three possible values that the fi rst argument can take to specify the type
of ResultSet object that is to be created. These three values are identifi ed by the
following static constants in interface ResultSet :
￿ TYPE_FORWARD_ONLY
￿ TYPE_SCROLL_INSENSITIVE
￿ TYPE_SCROLL_SENSITIVE
As might be guessed, the fi rst option allows only forward movement through the
ResultSet . The second and third options allow movement of the ResultSet 's cursor
Search WWH ::




Custom Search