Processing Data in a Result Set
Setting a Result Set Type
Different types of result sets apply to different application scenarios. Applications select proper types of result sets based on requirements. Before running an SQL statement, you must create a statement object. Some methods of creating statement objects can set the type of a result set. Table 1 lists result set parameters. The related Connection methods are as follows:
// Create a Statement object. This object will generate a ResultSet object with a specified type and concurrency.
createStatement(int resultSetType, int resultSetConcurrency);
// Create a PreparedStatement object. This object will generate a ResultSet object with a specified type and concurrency.
prepareStatement(String sql, int resultSetType, int resultSetConcurrency);
// Create a CallableStatement object. This object will generate a ResultSet object with a specified type and concurrency.
prepareCall(String sql, int resultSetType, int resultSetConcurrency);
Table 1 Result set types
Positioning a Cursor in a Result Set
ResultSet objects include a cursor pointing to the current data row. The cursor is initially positioned before the first row. The next method moves the cursor to the next row from its current position. When a ResultSet object does not have a next row, a call to the next method returns false. Therefore, this method is used in the while loop for result set iteration. However, the JDBC driver provides more cursor positioning methods for scrollable result sets, which allows positioning cursor in the specified row. Table 2 describes these methods.
Table 2 Methods for positioning a cursor in a result set
Moves the row specified by the forward parameter (that is, the value of is 1, which is equivalent to next()) or backward (that is, the value of is -1, which is equivalent to previous()). |
Obtaining the Cursor Position from a Result Set
This cursor positioning method will be used to change the cursor position for a scrollable result set. The JDBC driver provides a method to obtain the cursor position in a result set. Table 3 describes these methods.
Table 3 Methods for obtaining a cursor position in a result set
Obtaining Data from a Result Set
ResultSet objects provide a variety of methods to obtain data from a result set. Table 4 describes the common methods for obtaining data. If you want to know more about other methods, see JDK official documents.
Table 4 Common methods for obtaining data from a result set