The ResultSet interface is used to store and process the result returned from a database query. It helps in reading data row by row from the database.
In this chapter, you will learn about the ResultSet interface, its methods, and how to retrieve and navigate data from a database.
The ResultSet object maintains a cursor that points to a row of a table. Initially, the cursor is placed before the first row.
By default, a ResultSet moves only in the forward direction and is not updatable. However, we can make it scrollable and updatable by using special parameters in the createStatement() method.
To use the ResultSet interface, we need to import it from the java.sql package.
Here is the syntax to use ResultSet interface:
Or we can use:
The following table lists the important methods of the ResultSet interface.
| Method | Description |
|---|---|
| public boolean next(): | Moves the cursor to the next row. |
| public boolean previous(): | Moves the cursor to the previous row. |
| public boolean first(): | Moves the cursor to the first row. |
| public boolean last(): | Moves the cursor to the last row. |
| public boolean absolute(int row): | Moves the cursor to the specified row number. |
| public boolean relative(int row): | Moves the cursor relative to the current position. |
| public int getInt(int columnIndex): | Returns data of the given column index as integer. |
| public int getInt(String columnName): | Returns data of the given column name as integer. |
| public String getString(int columnIndex): | Returns data of the given column index as String. |
| public String getString(String columnName): | Returns data of the given column name as String. |
The ResultSet interface provides constants such as TYPE_SCROLL_INSENSITIVE, TYPE_SCROLL_SENSITIVE, and CONCUR_UPDATABLE to control cursor movement and update behavior.
The following example demonstrates how to retrieve data from the 3rd row using a scrollable ResultSet.
Output:
101 Rahul 30000
Explanation:
This example shows how to use ResultSet to fetch data from a specific row. The cursor is moved to the 3rd row using absolute(), and the data is displayed.
We request you to subscribe our newsletter for upcoming updates.