Processing Data in a Result Set
Setting a Result Set Type
Different types of result sets are applicable to different application scenarios. Applications select proper types of result sets based on requirements. Before executing 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:
1 2 3 4 5 6 7 8 |
//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); |
Parameter |
Description |
---|---|
resultSetType |
Indicates the type of a result set. There are three types of result sets:
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. To obtain up-to-date data of the record pointed by the cursor from the database, call the refreshRow() method in a ResultSet object. |
resultSetConcurrency |
Indicates the concurrency type of a result set. There are two types of concurrency.
|
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 lists these methods.
Method |
Description |
---|---|
next() |
Moves 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
This cursor positioning method will be used to change the cursor position for a scrollable result set. JDBC driver provides a method to obtain the cursor position in a result set. Table 3 lists the method.
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
ResultSet objects provide a variety of methods to obtain data from a result set. Table 4 lists the common methods for obtaining data. If you want to know more about other methods, see JDK official documents.
Method |
Description |
---|---|
int getInt(int columnIndex) |
Retrieves the value of the column designated by a column index in the current row as an int. |
int getInt(String columnLabel) |
Retrieves the value of the column designated by a column label in the current row as an int. |
String getString(int columnIndex) |
Retrieves the value of the column designated by a column index in the current row as a String. |
String getString(String columnLabel) |
Retrieves the value of the column designated by a column label in the current row as a String. |
Date getDate(int columnIndex) |
Retrieves the value of the column designated by a column index in the current row as a Date. |
Date getDate(String columnLabel) |
Retrieves the value of the column designated by a column name in the current row as a Date. |
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