How do I create an updatable ResultSet?
For the results to be updatable, the Statement object used to create the result sets must have the concurrency type ResultSet.CONCUR_UPDATABLE.
An updatable result set allows modification to data in a table through the result set. If the database does not support updatable result sets, the result sets returned from executeQuery() will be read-only.
Do as follows:
- Create a statement that will return updatable result sets
- Specify Primary key so that the result set is updatable
Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery("SELECT col_str FROM my_tab");
Note: An updatable result set query must specify the primary key as one of the selected columns and select from only one table.
For some drivers, `SELECT * FROM my_tab' returns a read-only result set, so do specify the column names.
How do I create an updatable ResultSet? Give an example
Modifications to result set can be done by using updatable result set. In order to obtain the updatable results, the Statement object that is used for creating result sets should have the concurrency type Resultset.CONCUR_UPDATABLE.
The query that obtains the rows for updatable must specify the primary key as one of the columns selected and from only one table. Ensure that the columns are specified in select statement explicitly instead of asterisk(*) in select statement.
Example: try
{
// Create a statement that will return updatable result sets
Statement stmt = connection.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// Primary key col_string must be specified so that the result set is updatable
ResultSet resultSet = stmt.executeQuery("SELECT empno FROM staff");
}
catch (SQLException e)
{
}