How do I create an updatable ResultSet?

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)
{
}
How do I insert an image file into a database?
Insert an image file - Upload all raw data-types (like binary documents) to the database as an array of bytes (bytes[]). Then you can,..
PreparedStatement - advantages of PreparedStatement
PreparedStatement - What is PreparedStatement?, What is the advantage of using a Prepared Statement?, difference between Statement and Prepared Statement...
Difference between TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE
TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE - Constant indicating that the type for a ResultSet object is scrollable but not sensitive to changes made by others...
Post your comment