Title: Executing update queries
Course: CPS 189: Java Certification - Review, Practice & Prepare (Development) > Section: (OLDstyle) Learning Modules

Executing update queries

An update query requires:

A Statement object created from the connected database with the following parameters .

- TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE

- CONCUR_UPDATABLE

A string containing a query of the connected database.

A ResultSet object to contain any rows returned from the query.  Data in the result set may then be edited using ResultSet interface methods.

// Define statement and result set

Statement statement = con.createStatement(

ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

ResultSet resultSet = statement.executeQuery(<select-query-string>);

// Add row to table

resultSet.moveToInsertRow();  // Move to insert row

resultSet.updateString(<col-index>, <col-value>);  // Set string col to value

resultSet.updateInt(<col-index>, <col-value>);  // Set int col to value

resultSet.insertRow();  // Commit change

The ResultSet interface contains the following for updating and moving among data in a result set:

- Navigation methods for moving among rows.

- Update methods for changing column data.

- Test methods for testing row status.

For more information, see:

Processing SQL Statements with JDBC

Level 1 Study Materials: Unit 7

Level 1 Study Materials: Unit 8