Insert Record using ResultSet in JDBC

We know that ResultSet objects are used to get the data that is retrieved using a Select statement.
ResultSet can also be used to insert or update records in the database.

Steps to remember when you insert Record using ResultSet in JDBC

1. ResultSet should be Updatable You can do this while creating Statement object

2. Get a ResultSet object after executeQuery()

3. Use ResultSet.moveToInsertRow() to initiate the insert process.

4. Add details of the record that needs to be added to the ResultSet object using updateXXX() methods

5. Use ResultSet.insertRow() to insert the record.

6. Use ResultSet.moveToCurrentRow() for any further processing on the ResultSet object.

Lets see an example on how to insert record using Resultset in Java in database.

Example

Output

You can fire a direct query to the database to check the records that were inserted.