Java Reference
In-Depth Information
Chapter 15:
Using JSPs, XSL, and Scrollable
ResultSets to Display Data
In This Chapter
One of the limitations of the JDBC
ResultSet
is that the user is restricted to scrolling forwards through
the rows. The JDBC 2.0 API adds the ability to define a
ResultSet
as scrollable so you can move the
cursor in either direction or to a particular row.
This enhancement is particularly when you need to add a graphical user interface to the
ResultSet
.
The ability to move through a
ResultSet
in only one direction would be very restrictive.
Scrollable ResultSets
In the
ResultSet
object defined in the JDBC Core API, the only way to scroll through the rows was to
use the
next()
method, which moves the cursor forward to the next row. One of the features added in
the JDBC 2.0 API is the ability to define a
ResultSet
as scrollable. Unlike the basic
ResultSet
,
which only lets you move the cursor forward, the scrollable
ResultSet
lets you move the cursor in
either direction or to a particular row. In addition, the scrollable
ResultSet
lets you get the cursor
position.
Creating a Scrollable ResultSet
The type of
ResultSet
a
java.sql.Statement
object returns is defined when the
Statement
is
created by the
Connection.createStatement
method. There are two forms of the
Connection.createStatement
method.
This basic version of
createStatement()
gets you a nonscrollable default
ResultSet
:
public Statement
createStatement
()
The second variant allows you to create scrollable and updateable
ResultSets
, as shown here:
public Statement
createStatement
(int rsType, int
rsConcurrency)
The first argument,
rsType
, must be one of the three following constants added to the
ResultSet
interface to indicate the type of a
ResultSet
object:
TYPE_FORWARD_ONLY
TYPE_SCROLL_INSENSITIVE
TYPE_SCROLL_SENSITIVE
If you want a scrollable
ResultSet
object, you must specify either
TYPE_SCROLL_INSENSITIVE
or
TYPE_SCROLL_SENSITIVE
. A
ResultSet
defined using
TYPE_SCROLL_INSENSITIVE
does not
reflect changes made while it is still open. A
TYPE_SCROLL_SENSITIVE ResultSet
does
reflect
changes made while it is still open. Of course, you can always see changes, regardless of the type of
ResultSet
by closing the
ResultSet
and then reopening it.
If you specify
TYPE_FORWARD_ONLY
, you will get a nonscrollable result set, where the cursor moves
forward only. If you also specify
CONCUR_READ_ONLY
for the second argument, you will get the default
ResultSet
identical to the
ResultSet
created with the no argument variant.
The second argument must be one of the two following
ResultSet
constants for specifying whether a
ResultSet
is read-only or updateable:
CONCUR_READ_ONLY
CONCUR_UPDATABLE
.