Java Reference
In-Depth Information
}
Implementing a scrollable ResultSet
As you can see from the code in
Listing 19-6
, it is relatively easy to implement the necessary methods
to create a scrollable
ResultSet
. All that is required to convert the
XMLResultSet
to a scrollable
ResultSet
is the addition of the following methods:
previous()
, which moves the cursor back one row at a time
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
These methods are pretty self-explanatory. The
absolute()
method 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
absolute()
method is used in
Listing 19-7
to handle all the other cursor movements by passing it
the computed value of the target row. For example, the method
first()
is implemented by calling the
absolute()
method with the argument 1. Similarly, the
relative()
method is implemented by
computing the target row and passing it in a call to
absolute()
.
Cross-
Reference
Scrollable
ResultSets
are described in more detail in
Chapter 4
.
Chapter
15
gives an example of the use of scrollable
ResultSets
.
Listing 19-7: Scrollable ResultSet methods
// scrollable ResultSet methods
public boolean previous(){
if(rows==null){
initialise();
rowIndex = rowCount;
}
if(--rowIndex < 0){
return false;
}else{
currentRow = (Element)rows.item(rowIndex);
return true;
}
}
public boolean absolute(int row) throws SQLException {
if(row == 0)throw new SQLException("invalid row number");
boolean onValidRow = true;