You retrieved records from database earlier in Select Records Example by iterating the records with the ResultSet object as follows. In the same example, you also have seen the ResultSet cursor movement in while loop.
System.out.println(res.getInt(1) + "\t" + res.getString(2) + "\t" + res.getDouble(3));
The above loop prints the records from top to bottom (from 1st record to the last). Can you print the records from bottom to top in the reverse order? No, not possible with JDBC 1.0; but possible with JDBC 2.0. JDBC 2.0 ResultSet provides some more methods (apart existing next() method) like previous(), relative() and absolute() etc. which can be used to place the cursor on any particular record. When the cursor is placed on a particular record (known as current record), the getXXX() methods read the current record fields. When the iterations in the above while loop are over, the cursor is positioned after the last record (not on the last record). With previous() method, the cursor can be moved from bottom to top printing the elements in the reverse order. Before going into the programming details, let us see few methods of ResultSet interface.
With the following methods, the cursor can be moved forward or backward or placed on a specific record.
|first()||Places the cursor on the first record|
|last()||Places the cursor on the last record|
|next()||Moves the cursor to the next record|
|previous()||Moves the cursor to the previous record|
|absolute(int row)||Moves the cursor to the specific row whose number is passed as parameter|
|relative(int rows)||Moves the cursor to the relative position (passed as parameter) from the current position. Parameter accepts both positive and negative values|
|afterLast()||Positions the cursor after the last record (where there is no record)|
|beforeFirst()||Positions the cursor before the first record (where there is no record)|
|refreshRow()||Refreshes the current row with the values set with updateXXX() methods|
|deleteRow()||Deletes the current row|
|cancelRowUpdates()||Cancels the updates made to the current row earlier with updateXXX() methods earlier|
|moveToInsertRow()||Moves the cursor to the insert row (insert row is that one where a new record can be inserted)|
|getRow()||Returns the current row position as int data type|