A ResultSet
object contains a table of data representing a database result set, which is generated by executing a statement that queries the database.
A cursor points to the current row of data in a ResultSet
object. Initially, it is positioned before the first row. Use the next
method of the ResultSet
object to move the cursor to the next row in the result set. It returns false
when there are no more rows in the ResultSet
object. Typically, the contents of a ResultSet
object are read by using the next
method within a loop until it returns false
.
The ResultSet
interface provides accessor methods (getBoolean
, getLong
, getInt
, and so on) for retrieving column values from the current row. Values can be retrieved by using either the index number of the column or the name of the column.
By default, only one ResultSet
object per Statement
object can be open at the same time. Therefore, to read data from multiple ResultSet
objects, you must use multiple Statement
objects. A ResultSet
object is automatically closed when the Statement
object that generated it is closed, rerun, or used to retrieve the next result from a sequence of multiple results.
Related Topics
Scrollability refers to the ability to move backward as well as forward through a result set. You can also move to any particular position in the result set, through either relative positioning or absolute positioning. Relative positioning lets you move a specified number of rows forward or backward from the current row. Absolute positioning lets you move to a specified row number, counting from either the beginning or the end of the result set.
When creating a scrollable or positionable result set, you must also specify sensitivity. This refers to the ability of a result set to detect and reveal changes made to the underlying database from outside the result set. A sensitive result set can see changes made to the database while the result set is open, providing a dynamic view of the underlying data. Changes made to the underlying column values of rows in the result set are visible. Updatability refers to the ability to update data in a result set and then copy the changes to the database. This includes inserting new rows into the result set or deleting existing rows. A result set may be updatable or read-only.
Scrollability and sensitivity are independent of updatability, and the three result set types and two concurrency types combine for the following six result set categories:
Forward-only/read-only
Forward-only/updatable
Scroll-sensitive/read-only
Scroll-sensitive/updatable
Scroll-insensitive/read-only
Scroll-insensitive/updatable
Example 4-2 demonstrates how to declare a scroll-sensitive and read-only ResultSet
object.
Note:
A forward-only updatable result set has no provision for positioning at a particular row within the ResultSet
object. You can update rows only as you iterate through them using the next
method.
Example 4-2 Declaring a Scroll-Sensitive, Read-Only ResultSet Object
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);