Say that a returned ResultSet has 100 rows. After looping through 60 rows, I want to return to row number 40 without querying the database again. Is it possible?

Nicola Ken Barozzi

To access rows in a database ResultSet you must use a JDBC 2.0 capable driver. If the version number is lower you can only go forward; to go backwards or jump to a specific row you must cache the results in a data structure on the client and use that (if the table is big you might need a lot of memory, and things can get slow).
If you know how many rows you will be getting you can use an array of arrays, otherwise a Vector of arrays (you know how many columns you have but not how many rows).
If you need to do it because you need to show table data you can insert all the rows in a DefaultTableModel (or your implementation of the TableModel interface) with this DefaultTableModel method:

public void addRow(Object[] rowData)
Anyway here are some methods of Resultset you can use.

Javadoc:

/***************************************************/
public boolean next()
             throws SQLException

Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the
first row; the first call to next makes the first row the current row; the second call makes the second row the
current row, and so on.

If an input stream is open for the current row, a call to the method next will implicitly close it. The ResultSet's
warning chain is cleared when a new row is read.

/***************************************************/
public boolean previous()
                 throws SQLException

JDBC 2.0

Moves the cursor to the previous row in the result set.

/***************************************************/
public boolean relative(int rows)
                 throws SQLException

JDBC 2.0

Moves the cursor a relative number of rows, either positive or negative. Attempting to move beyond the
first/last row in the result set positions the cursor before/after the the first/last row. Calling relative(0) is
valid, but does not change the cursor position.

/***************************************************/
public boolean first()
              throws SQLException

JDBC 2.0

Moves the cursor to the first row in the result set.


/***************************************************/
public boolean last()
             throws SQLException

JDBC 2.0

Moves the cursor to the last row in the result set.

/***************************************************/
public boolean absolute(int row)
                 throws SQLException

JDBC 2.0

Moves the cursor to the given row number in the result set.

If the row number is positive, the cursor moves to the given row number with respect to the beginning of the
result set. The first row is row 1, the second is row 2, and so on.

If the given row number is negative, the cursor moves to an absolute row position with respect to the end of
the result set. For example, calling absolute(-1) positions the cursor on the last row, absolute(-2)
indicates the next-to-last row, and so on.

An attempt to position the cursor beyond the first/last row in the result set leaves the cursor before/after the
first/last row, respectively.

Note: Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling
last().

/***************************************************/

Refer to the java.sql.Resultset documentation for details and other methods.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact