Java Reference
In-Depth Information
Working with Result Sets
The SQL statements that read data from a database query return the data in a
result set . The SELECT statement is the standard way to select rows from a
database and view them in a result set. The java.sql.ResultSet interface repre-
sents the result set of a database query.
A ResultSet object maintains a cursor that points to the current row in the
result set. The methods of the ResultSet interface can be broken down into
three categories:
Navigational methods used to move the cursor around.
■■
Get methods that are used to view the data in the columns of the cur-
rent row being pointed to by the cursor.
■■
Update methods that update the data in the columns of the current row.
The updates can then be updated in the underlying database as well.
■■
Let's look at some of these methods in the ResultSet interface because work-
ing with result sets is an essential task of any database application. We'll start
with the methods that let you move the cursor around.
Navigating a Result Set
The cursor is movable based on the properties of the ResultSet. These proper-
ties are designated when the corresponding Statement that generated the
ResultSet is created. (See the createStatement() methods in the section Simple
Statements .) The possible properties of a result set cursor are:
ResultSet.TYPE_FORWARD_ONLY.
The cursor can only move forward
in the result set.
ResultSet.TYPE_SCROLL_INSENSITIVE. The cursor can scroll for-
wards and backwards, and the result set is not sensitive to changes made
by others to the database that occur after the result set was created.
ResultSet.TYPE_SCROLL_SENSITIVE. The cursor can scroll forwards
and backwards, and the result set is sensitive to changes made by others
to the database that occur after the result set was created.
If the type is forward only, you can only navigate through the result set once,
and only moving the cursor forward. Scrollable cursors can be moved forward
and backward in the result set. If a result set is marked as insensitive, the result
set is a snapshot of the corresponding data in the database at the time the
result set was created, and subsequent changes to the database do not affect
Search WWH ::




Custom Search