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); |
| Parameter | Description |
|---|---|
| resultSetType | Type of a result set. The options are as follows:
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:
|
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.
| 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.
| 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.
| 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. |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot