Creating and modifying database objects using the Statement.executeUpdate method

The Statement.executeUpdate is one of the JDBC methods that you can use to update tables and call stored procedures.

About this task

You can use the Statement.executeUpdate method to do the following things:
  • Execute data definition statements, such as CREATE, ALTER, DROP, GRANT, REVOKE
  • Execute INSERT, UPDATE, DELETE, and MERGE statements that do not contain parameter markers.
  • With the IBM® Data Server Driver for JDBC and SQLJ, execute the CALL statement to call stored procedures that have no parameters and that return no result sets.

Procedure

To execute these SQL statements, you need to perform these steps:

  1. Invoke the Connection.createStatement method to create a Statement object.
  2. Invoke the Statement.executeUpdate method to perform the SQL operation.
  3. Invoke the Statement.close method to close the Statement object.

Example

Suppose that you want to execute this SQL statement:
UPDATE EMPLOYEE SET PHONENO='4657' WHERE EMPNO='000010'
The following code creates Statement object stmt, executes the UPDATE statement, and returns the number of rows that were updated in numUpd. The numbers to the right of selected statements correspond to the previously-described steps.
Figure 1. Using Statement.executeUpdate
Connection con;
Statement stmt;
int numUpd;
…
stmt = con.createStatement();                // Create a Statement object  1 
numUpd = stmt.executeUpdate(
  "UPDATE EMPLOYEE SET PHONENO='4657' WHERE EMPNO='000010'");              2 
                                             // Perform the update
stmt.close();                                // Close Statement object     3