A table of data representing a database result set, which is usually generated by executing a statement that queries the database. A ResultSet object maintains a cursor pointing to its current row of data.
Initially the cursor is positioned before the first row.
The next
method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet
object, it can be used in a while loop to iterate through the result set.
For example, the following code will iterate through the ResultSet
object rset
and will retrieve and print each entry of the first column, which is a string:
while ( rset.next( ) )
System.out.println( rset.getString( 1 ) );
A default ResultSet
object is not updatable and has a cursor that moves forward only. The ResultSet
interface provides getXXX
methods for retrieving column values from the current row.
Values can be retrieved using either one of the following two methods:
- The index number of the column
In general, using the column index will be more efficient.
Columns are numbered from 1.
For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.
- The name of the column
Column names used as input to getXXX
methods are case insensitive.
When a getXXX
method is called with a column name and several columns have the same name, the value of the first matching column will be returned.
The column name option is designed to be used when column names are used in the SQL query that generated the result set.