Interface JdbcUpdate

  • All Superinterfaces:
    AutoCloseable

    public interface JdbcUpdate
    extends AutoCloseable
    An SQL update operation.

    This API uses the builder pattern to allow an update operation to be easily configured and executed, using a fluent expression.

    An instance of this type is created using the update method on the JdbcOperations interface. The update operation must be configured with a statement to execute via the using method.

    Additionally, the update operation can be configured for single execution (default) or repeated execution. When an update is configured for repeated execution (using repeatedly()), the underlying statement and connection objects remain open after the execute(Parameter...), method is invoked, allowing the update operation to be executed again with different parameter values. An update operation that is configured for repeated execution must be closed when it is no longer needed, by invoking the close() method explicitly or by enclosing it in a try-with-resources construct.

    After the update operation has been configured, it can be executed using the execute(Parameter...) method.

    Examples:

    Updating many rows with a single statement execution:

     
     int count = sqlTemplate.update()
         .using("DELETE FROM person WHERE status = ?")
         .execute(Parameter.with("INACTIVE");
     

    Repeatedly executing the same update operation with different parameters:

     
     try (JdbcUpdate updater = sqlTemplate.update()
         .using("UPDATE person SET age = age + 1 WHERE id = ?")
         .repeatedly()) {
       updater.execute(Parameter.with(1L));
       updater.execute(Parameter.with(4L));
     }
     
    Author:
    Carl Harris
    • Method Detail

      • using

        JdbcUpdate using​(String sql)
        Configures this update to execute the given SQL statement.
        Parameters:
        sql - the SQL statement to execute
        Returns:
        this update object
      • using

        JdbcUpdate using​(SQLSource source)
        Configures this update to execute the given SQL statement.
        Parameters:
        source - for the SQL statement to execute
        Returns:
        this update object
      • repeatedly

        JdbcUpdate repeatedly()
        Configures this update for repeated execution.

        An update that is configured as repeatable must be explicitly closed by the caller using close() when no longer needed.

        Returns:
        this query
      • execute

        int execute​(Parameter... parameters)
        Executes this update.
        Parameters:
        parameters - values for placeholders in the SQL statement
        Returns:
        number of rows affected by the update
      • close

        void close()
        Closes the JDBC resources associated with this update.

        After an update is closed, its execute(Parameter...) method may not be subsequently invoked.

        Specified by:
        close in interface AutoCloseable