Class PreparedBatch

All Implemented Interfaces:
Closeable, AutoCloseable, Configurable<PreparedBatch>, ResultBearing

public class PreparedBatch extends SqlStatement<PreparedBatch> implements ResultBearing
Represents a prepared batch statement. Multiple bindings are added to the compiled statement and then executed in a single operation. This is, generally, a very efficient way to execute large numbers of the same statement where the statement only varies by the arguments bound to it. The statement starts with an empty binding. You bind a single batch of parameters with the usual SqlStatement binding methods, and then call add() to add the current binding as a batch and then clear it. An entire batch can be bound and added in one go with add(Map) or add(Object...).
  • Constructor Details

    • PreparedBatch

      public PreparedBatch(Handle handle, String sql)
  • Method Details

    • scanResultSet

      public <R> R scanResultSet(ResultSetScanner<R> mapper)
      Description copied from interface: ResultBearing
      Invokes the mapper with a result set supplier, and returns the value returned by the mapper.
      Specified by:
      scanResultSet in interface ResultBearing
      Type Parameters:
      R - result type returned by the mapper.
      Parameters:
      mapper - result set scanner
      Returns:
      the value returned by the mapper.
    • execute

      public int[] execute()
      Execute the batch and return the number of rows affected for each batch part. Note that some database drivers might return special values like Statement.SUCCESS_NO_INFO or Statement.EXECUTE_FAILED.
      Returns:
      the number of rows affected per batch part
      See Also:
    • executeAndGetModCount

      public ResultIterator<Integer> executeAndGetModCount()
      Execute the batch and return the mod counts as in execute, but as a Jdbi result iterator instead of an array.
      Returns:
      the number of rows affected per batch part
      See Also:
    • executeAndReturnGeneratedKeys

      public ResultBearing executeAndReturnGeneratedKeys(String... columnNames)
    • execute

      public <R> R execute(ResultProducer<R> producer)
      Executes the batch, returning the result obtained from the given ResultProducer.
      Type Parameters:
      R - the type of the result
      Parameters:
      producer - the result producer.
      Returns:
      value returned by the result producer.
    • add

      public PreparedBatch add()
      Add the current binding as a saved batch and clear the binding.
      Returns:
      this
    • add

      public PreparedBatch add(Object... args)
      Bind arguments positionally, add the binding as a saved batch, and then clear the current binding.
      Parameters:
      args - the positional arguments to bind
      Returns:
      this
    • add

      public PreparedBatch add(Map<String,?> args)
      Bind arguments from a Map, add the binding as a saved batch, then clear the current binding.
      Parameters:
      args - map to bind arguments from for named parameters on the statement
      Returns:
      this
    • size

      public int size()
      Returns the number of bindings in this batch.
      Returns:
      the number of bindings in this batch.
    • getHandle

      public Handle getHandle()
    • getConfig

      public ConfigRegistry getConfig()
      Description copied from interface: Configurable
      Returns the configuration registry associated with this object.
      Specified by:
      getConfig in interface Configurable<This>
      Returns:
      the configuration registry associated with this object.
    • getContext

      public final StatementContext getContext()
      Returns the statement context associated with this statement.
      Returns:
      the statement context associated with this statement.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable