Modifying data using INSERT, UPDATE, and DELETE

You can perform SQL data modification using the execute method of a PreparedStatement. A PreparedStatement queries the database with a user-defined SQL statement.

When applying a SQL statement to a PreparedStatement, query parameters are indicated by the ? character. For any INSERT, UPDATE, or DELETE statement, each ? parameter is referenced according to its ordinal position in the statement. For example, the first ? is referenced as parameter one, and the second as parameter two.

 INSERT a row in a table
  1. Prepare a new SQL statement as a String.

    String sql_string = 
        "INSERT INTO Department(dept_no, name) VALUES( ?, ? )";
  2. Pass the String to the PreparedStatement.

    PreparedStatement inserter = 
        conn.prepareStatement(sql_string);
    
  3. Pass input values to the PreparedStatement using the set method.

    This example sets 101 for the dept_no, referenced as parameter 1, and "Electronics" for the name, referenced as parameter 2.

    inserter.set(1, 101);
    inserter.set(2, "Electronics");
  4. Execute the statement.

    inserter.execute();
  5. Close the PreparedStatement to free resources.

    inserter.close();
  6. Commit all changes to the database.

    conn.commit();

Steps 3 and 4 can be repeated as many times as needed.

Alternatively, you can prepare, execute, and close an INSERT statement when you only need to insert a single entry, as illustrated in the following example:

INSERT INTO Department(dept_no, name) VALUES(2, 'Electronics');

The statement can also be created using Java String concatenation.

 UPDATE a row in a table
  1. Prepare a new SQL statement as a String.

    String sql_string = 
        "UPDATE Department SET dept_no = ? WHERE dept_no = ?";
  2. Pass the String to the PreparedStatement.

    PreparedStatement updater = 
        conn.prepareStatement(sql_string);
    
  3. Pass input values to the PreparedStatement using the set method.

    updater.set(1, 102);
    updater.set(2, 101);

    The example above is the equivalent of executing the following SQL statement:

    UPDATE Department SET dept_no = 102 WHERE dept_no = 101;
  4. Execute the statement.

    updater.execute();
  5. Close the PreparedStatement to free resources.

    updater.close();
  6. Commit all changes to the database.

    conn.commit();

Steps 3 and 4 can be repeated as many times as needed.

Alternatively, the following statement could be prepared, executed and closed when only a single update is to be performed:

UPDATE Department SET dept_no = 102 WHERE dept_no = 101;

This statement can also be created using Java String concatenation.

 DELETE a row in a table
  1. Prepare a new SQL statement as a String.

    String sql_string = 
        "DELETE FROM Department WHERE dept_no = ?";
  2. Pass the String to the PreparedStatement.

    PreparedStatement deleter = 
        conn.prepareStatement(sql_string);
    
  3. Pass input values to the PreparedStatement using the set method.

    deleter.set(1, 102);

    The example above is the equivalent of executing the following SQL statement:

    DELETE FROM Department WHERE dept_no = 102;
  4. Execute the statement.

    deleter.execute();
  5. Close the PreparedStatement to free resources.

    deleter.close();
  6. Commit all changes to the database.

    conn.commit();
  7. Steps 3 and 4 can be repeated as many times as needed.

    Alternatively, the following statement could be prepared, executed and closed as described in the section above when only a single delete is to be performed:

    DELETE FROM Department WHERE dept_no = 102;

    This statement can also be created using Java String concatenation.

 Example