Updated on 2026-07-02 GMT+08:00

Processing Data in a Result Set

Setting a Result Set Type

Each result set type suits specific scenarios. Select an appropriate result set type based on your application's needs. Before executing SQL statements, you must first create a statement object. Some methods for creating statement objects allow you to set a result set type. Table 1 lists result set parameters. The following Connection methods are involved:

1
2
3
4
5
6
7
8
//Create a Statement object that will generate a ResultSet object with the given type and concurrency.
createStatement(int resultSetType, int resultSetConcurrency);

//Create a PreparedStatement object that will generate a ResultSet object with the given type and concurrency.
prepareStatement(String sql, int resultSetType, int resultSetConcurrency);

//Create a CallableStatement object that will generate a ResultSet object with the given type and concurrency.
prepareCall(String sql, int resultSetType, int resultSetConcurrency);
Table 1 Result set types

Parameter

Description

resultSetType

Type of a result set. The options are as follows:

  • ResultSet.TYPE_FORWARD_ONLY (default value): The ResultSet object can only be navigated forward.
  • ResultSet.TYPE_SCROLL_SENSITIVE: You can view the modified result by scrolling to the modified row.
  • ResultSet.TYPE_SCROLL_INSENSITIVE: The ResultSet object is insensitive to changes in the underlying data source.
NOTE:

After a result set has obtained data from the database, the result set is insensitive to data changes made by other transactions, even if the result set type is ResultSet.TYPE_SCROLL_SENSITIVE. You can call the refreshRow() method of ResultSet to access the database and obtain the latest data of the record pointed to by the cursor.

resultSetConcurrency

Concurrency type of a result set. The options are as follows:

  • ResultSet.CONCUR_READ_ONLY: The data in a result set cannot be updated except that an updated statement has been created in the result set data.
  • ResultSet.CONCUR_UPDATABLE: The result set can be changed. The concurrency type for a result set object can be updated if the result set is scrollable.

Positioning a Cursor in a Result Set

ResultSet objects include a cursor pointing to the current data row. Initially, the cursor is positioned before the first row. The next method moves the cursor to the following row. Since this method returns false when there are no further rows in the ResultSet object, it can be utilized within a while loop to iterate over the result set. However, the JDBC driver provides more cursor positioning methods for scrollable result sets, which allows positioning the cursor in the specified row. Table 2 lists these methods.

Table 2 Methods for positioning a cursor in a result set

Method

Description

next()

Moves the cursor to the next row from its current position.

previous()

Moves cursor to the previous row from its current position.

beforeFirst()

Places cursor before the first row.

afterLast()

Places cursor after the last row.

first()

Places cursor to the first row.

last()

Places cursor to the last row.

absolute(int)

Places cursor to a specified row.

relative(int)

Moves cursor forward or backward a specified number of rows.

Obtaining the cursor position from a result set

For scrollable result sets, positioning methods might be invoked to alter the cursor's location. The JDBC driver supplies methods to ascertain the cursor's position within a result set. Methods for determining the cursor's position are detailed in Table 3.

Table 3 Method for obtaining the cursor position in a result set

Method

Description

isFirst()

Checks whether the cursor is in the first row.

isLast()

Checks whether the cursor is in the last row.

isBeforeFirst()

Checks whether the cursor is before the first row.

isAfterLast()

Checks whether the cursor is after the last row.

getRow()

Gets the current row number of the cursor.

Obtaining Data from a Result Set

The ResultSet object provides an array of methods to extract data from a result set. Commonly used methods for data retrieval are listed in Table 4 and additional methods are available in the JDK official documentation.

Table 4 Common methods for obtaining data from a result set

Method

Description

int getInt(int columnIndex)

Retrieves the integer data by column index.

int getInt(String columnLabel)

Retrieves the integer data by column label.

String getString(int columnIndex)

Retrieves the string data by column index.

String getString(String columnLabel)

Retrieves the string data by column label.

Date getDate(int columnIndex)

Retrieves the date data by column index.

Date getDate(String columnLabel)

Retrieves the date data by column label.