Modifying Data and Databases

The Statement and PreparedStatement classes described in the previous section can be used to perform updates and database modifications in addition to queries.

Performing Updates

To change data already in a database (that is, to perform an SQL INSERT, UPDATE, or DELETE), the executeUpdate method of Statement and PreparedStatement objects must be used instead of executeQuery. The executeUpdate() method is similar to executeQuery in that a statement is sent to the database for execution, but is different in that it does not return a ResultSet. Instead, it returns the number of rows affected by the INSERT, UPDATE, or DELETE. Example 7-3 illustrates using executeUpdate to perform a DELETE.

Creating and Modifying Database Objects

SQL CREATE and DROP statements can be executed with either the execute method or the executeUpdate method. The execute method is like executeQuery and executeUpdate, but it returns nothing at all. Example 7-4 shows dropping a table using the Statement's execute method.