Updated on 2025-08-25 GMT+08:00

Processing Result Sets

Positioning Within a Result Set

A ResultSet object contains a cursor that points to its 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 through the result set. However, for scrollable result sets, the JDBC driver offers additional positioning methods to direct the ResultSet to specific rows. These positioning methods are outlined in Table 1.

Table 1 Positioning methods in a result set

Method

Description

next()

Advances the ResultSet by one row.

previous()

Retreats the ResultSet by one row.

beforeFirst()

Positions the ResultSet before the first row.

afterLast()

Places the ResultSet after the last row.

first()

Directs the ResultSet to the first row.

last()

Navigates the ResultSet to the last row.

absolute(int)

Relocates the ResultSet to the row number specified by the parameter.

Obtaining the Cursor Position in the 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 2.

Table 2 Methods for obtaining the cursor position in a result set

Method

Description

isFirst()

Determines if at the first row.

isLast()

Checks if at the last row.

isBeforeFirst()

Verifies if before the first row.

isAfterLast()

Confirms if after the last row.

getRow()

Fetches the current row number.

Obtaining Data in 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 3, with additional methods available in the JDK official documentation.

Table 3 Common methods for obtaining data from a result set

Method

Description

int getInt(int columnIndex)

Retrieves integer data by column index.

int getInt(String columnLabel)

Retrieves integer data by column label.

String getString(int columnIndex)

Retrieves string data by column index.

String getString(String columnLabel)

Retrieves string data by column label.

Date getDate(int columnIndex)

Retrieves date data by column index.

Date getDate(String columnLabel)

Retrieves date data by column label.