Java Reference
In-Depth Information
Note
If you specify a
ResultSet
type, you must also specify whether the
ResultSet
is
read-only or updateable.
You can check the type of
ResultSet
you have using the
ResultSet.getType()
method, as shown
here:
if(rs.getType()==ResultSet.TYPE_FORWARD_ONLY)
System.out.println("FORWARD_ONLY");
else
System.out.println("SCROLLABLE");
Moving the Cursor in a Scrollable ResultSet
Once you have a scrollable
ResultSet
object, you can move the cursor both backward and forward in
the
ResultSet
by using these methods:
ResultSet.next()
, which moves the cursor forwards to the next row
ResultSet.previous()
, which moves the cursor back one row
Both methods return
false
when the cursor goes beyond the result set, so you can easily use these
methods in a
while
loop.
In addition to using the
next()
and
previous()
methods to scroll forward and backward, you can
move the cursor to a designated row using these methods:
first()
, which moves the cursor to the first row
last()
, which moves the cursor to the last row
beforeFirst()
, which moves the cursor to a point just before the first row
afterLast()
, which moves the cursor to a point just after the last row
absolute(int rowNumber)
, which moves the cursor to the specified row
relative(int rowNumber)
, which moves the cursor the specified number of rows
The method
absolute(int rowNumber)
moves the cursor to the row number indicated in the
argument. If the number is positive, the cursor moves to the given row number from the beginning. If the
number is negative, the cursor moves to the given row number from the end, so
absolute(1)
moves
the cursor to the first row, and
absolute(-1)
moves it to the last row.
The method
relative(int rowNumber)
lets you specify how many rows to move from the current
row and in which direction to move. A positive number moves the cursor forward the given number of
rows; a negative number moves the cursor backward the given number of rows. The effect of the first
four of these is apparent from the method names.
Note
As with the default
ResultSet
that is not scrollable, a scrollable
ResultSet's
cursor
is initially positioned before the first row.
Using Scrollable Re sultSets to Create a Search Page
In the course of the last couple of chapters, you have learned how to use servlets and JSP pages to
handle HTML forms and to save form data, images, and documents to a database. This chapter
concentrates on retrieving data from the database and presenting it as a Web page.
Chapter 13
illustrates how to create and handle HTML forms using JSP pages. The chapter goes on to
develop examples showing how to handle a simple registration form. This chapter extends these
concepts to a database driven web site which members can use to buy and sell vehicles.
The Web site features a search capability, allowing members to enter search criteria and scroll through
a formatted
ResultSet
. Clicking a selection takes the user to a detail page displaying more
information about an item in the database