English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The SQL statement that reads data from a database query returns data in the result set. The SELECT statement is the standard method for selecting rows from a database and viewing them in the result set. The Sql.ResultSet interface represents the result set of a database query.
The ResultSet object maintains a cursor pointing to the current row in the result set. The term 'result set' refers to the row and column data contained in the ResultSet object.
The methods of the ResultSet interface can be divided into three categories-
Navigation methods: Used to move the cursor left and right.
Get method: Used to view data in the columns of the current row pointed to by the cursor.
Update method:Used to update data in each column of the current row. Then, the update can also be reflected in the underlying database.
The cursor can move according to the properties of the ResultSet. These properties are specified when creating the corresponding Statement that generates the ResultSet.
JDBC provides the following connection methods to create statements with the required ResultSet.-
createStatement(int RSType, int RSConcurrency);
prepareStatement(String SQL, int RSType, int RSConcurrency);
prepareCall(String sql, int RSType, int RSConcurrency);
The first parameter represents the type of the ResultSet object, and the second parameter is one of the two ResultSet constants, used to specify whether the result set is read-only or updatable.
The possible RSTypes are as follows. If you do not specify any ResultSet type, you will automatically obtain a result set of TYPE_FORWARD_ONLY type.
Type | Description |
---|---|
ResultSet.TYPE_FORWARD_ONLY | The cursor can only move forward within the result set. |
ResultSet.TYPE_SCROLL_INSENSITIVE | The cursor can scroll forward and backward, and the result set is insensitive to changes made to the database by others after the result set was created. |
ResultSet.TYPE_SCROLL_SENSITIVE. | The cursor can scroll forward and backward, and the result set is very sensitive to changes made to the database by others after the result set is created. |
Possible RSConcurrency is given below. If you do not specify any concurrency type, you will automatically obtain the CONCUR_READ_ONLY type.
Concurrency | Description |
---|---|
ResultSet.CONCUR_READ_ONLY | Create a read-only result set. This is the default value |
ResultSet.CONCUR_UPDATABLE | Create an updatable result set. |
So far, all the examples we have written can be written as follows, which initializes a Statement object to create a read-only ResultSet object-
try { Statement stmt = conn.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); } catch(Exception ex) { .... } finally { .... }
There are several methods involving cursor movement in the ResultSet interface, including-
Serial Number | Methods and Descriptions |
---|---|
1 | public void beforeFirst() throws SQLException Move the cursor to the row before the first one. |
2 | public void afterLast() throws SQLException Move the cursor to the row after the last one. |
3 | public boolean first() throws SQLException Move the cursor to the first row. |
4 | public void last() throws SQLException Move the cursor to the last row. |
5 | public boolean absolute(int row) throws SQLException Move the cursor to the specified row. |
6 | public boolean relative(int row) throws SQLException Move the cursor forward or backward by the specified number of rows from its current position. |
7 | public boolean previous() throws SQLException Move the cursor to the previous row. If the previous row is not in the result set, this method returns false. |
8 | public boolean next() throws SQLException Moves the cursor to the next row. If there are no more rows in the result set, this method returns false. |
9 | public int getRow() throws SQLException Returns the row number pointed to by the cursor. |
10 | public void moveToInsertRow() throws SQLException Moves the cursor to a special row in the result set that can be used to insert a new row into the database. Remember the current cursor position. |
11 | public void moveToCurrentRow() throws SQLException If the cursor is currently at the insert row, move the cursor back to the current row; otherwise, this method does nothing |
To better understand, let's study "Navigation-Sample code.
The ResultSet interface contains many methods for obtaining data in the current row.
Each possible data type has a get method, and each get method has two versions-
A column with a column name.
An index with a column index.
For example, if the column you are interested in viewing contains an int, you need to use one of the getInt() methods of ResultSet.
Serial Number | Methods and Descriptions |
---|---|
1 | public int getInt(String columnName) throws SQLException Returns the int in the current row of the column named columnName. |
2 | public int getInt(int columnIndex) throws SQLException Returns the integer value in the current row at the specified column index. Column index starts from1Starting, which means the first column of the row is1The second column of the row is2and so on. |
Similarly, in the ResultSet interface, for each of the eight Java basic types, as well as common types (such as java.lang.String, java.lang.Object, and java.net.URL), there is a get method.
There are also methods to obtain SQL data types java.sql.Date, java.sql.Time, java.sql.Timestamp, java.sql.Clob, and java.sql.Blob. View the documentation for more information on using these SQL data types.
To better understand, let's studyView-Example Code.
The ResultSet interface contains a collection of update methods used to update the result set data.
Like the get method, each data type has two update methods.-
A column with a column name.
An index with a column index.
For example, to update the current row's String column in the result set, you can use one of the following updateString() methods.-
Serial Number | Methods and Descriptions |
---|---|
1 | public void updateString(int columnIndex, String s) throws SQLException Change the value of the specified column to the value of s. |
2 | public void updateString(String columnName, String s) throws SQLException Similar to the previous methods, but the columns are specified by name instead of index. |
There are eight primitive data types and update methods for String, Object, URL, and SQL data types in the java.sql package.
Updating a row in the result set changes the columns of the current row in the ResultSet object but does not change the row in the underlying database. To update changes to a row in the database, you need to call one of the following methods.
Serial Number | Methods and Descriptions |
---|---|
1 | public void updateRow() Update the current row by updating the corresponding row in the database. |
2 | public void deleteRow() Delete the current row from the database |
3 | public void refreshRow() Refresh the data in the result set to reflect any recent changes in the database. |
4 | public void cancelRowUpdates() Cancel any updates made to the current row. |
5 | public void insertRow() Insert a row in the database. This method can only be called when the cursor points to the row to be inserted. |
To better understand, let's study "Update Example Code.