Processing a Result Set
Setting the Result Set Type
Different types of result sets have their own application scenarios, and an application needs to select the appropriate result set type based on the actual situation. During SQL statement execution, the corresponding statement object must be created first, and some methods for creating statement objects provide the functionality to set the result set type. Table 1 describes the specific parameter settings. The involved Connection methods are as follows:
// Create a Statement object that will generate ResultSet objects with the given type and concurrency.
createStatement(int resultSetType, int resultSetConcurrency);
// Create a PreparedStatement object that will generate ResultSet objects with the given type and concurrency.
prepareStatement(String sql, int resultSetType, int resultSetConcurrency);
// Create a CallableStatement object that will generate ResultSet objects with the given type and concurrency.
prepareCall(String sql, int resultSetType, int resultSetConcurrency);Table 1 Result set types
Positioning in a Result Set
A result set object has 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 next row; because this method returns false when there are no more rows in the result set object, it can be used in a while loop to iterate through the result set. However, for a scrollable result set, the JDBC driver provides more positioning methods to point the result set to a specific row. Table 2 describes the positioning methods.
Table 2 Methods for positioning in a result set
Moves forward (set to 1, equivalent to next()) or backward (set to -1, equivalent to previous()) by the number of rows specified by the parameter. |
Obtaining the Cursor Position in a Result Set
For scrollable result sets, positioning methods may be called to change the cursor position. The JDBC driver provides methods for obtaining the cursor position in a result set. Table 3 describes the methods for obtaining the cursor position.
Table 3 Obtaining the cursor position in the result set
Fetching Data from a Result Set
Result set objects provide a rich set of methods for fetching data from a result set. Table 4 lists the common methods for fetching data. For other methods, refer to the official JDK documentation.
Table 4 Common methods for fetching data