Class PreparedBatch
- All Implemented Interfaces:
Closeable, AutoCloseable, Configurable<PreparedBatch>, 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 Summary
ConstructorsConstructorDescriptionPreparedBatch(Handle handle, CharSequence sql) PreparedBatch(Handle handle, String sql) Backwards compatible constructor that takes an explicit string argument. -
Method Summary
Modifier and TypeMethodDescriptionadd()Add the current binding as a saved batch and clear the binding.Bind arguments positionally, add the binding as a saved batch, and then clear the current binding.Bind arguments from a Map, add the binding as a saved batch, then clear the current binding.final PreparedBatchRegisters with the handle for cleaning when the handle is closed.voidclose()int[]execute()Execute the batch and return the number of rows affected for each batch part.<R> Rexecute(ResultProducer<R> producer) Executes the batch, returning the result obtained from the givenResultProducer.Execute the batch and return the mod counts as inexecute, but as a Jdbi result iterator instead of an array.executeAndReturnGeneratedKeys(String... columnNames) Deprecated, for removal: This API element is subject to removal in a future version.executePreparedBatch(String... columnNames) Execute the batch and give access to any generated keys returned by the operation.Returns the configuration registry associated with this object.final StatementContextReturns the statement context associated with this statement.final Handle<R> RscanResultSet(ResultSetScanner<R> resultSetScanner) Invokes the mapper with a result set supplier, and returns the value returned by the mapper.intsize()Returns the number of bindings in this batch.Methods inherited from class SqlStatement
bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindASCIIStream, bindASCIIStream, bindBean, bindBean, bindBeanList, bindBinaryStream, bindBinaryStream, bindBySqlType, bindBySqlType, bindByType, bindByType, bindByType, bindByType, bindByType, bindByType, bindFields, bindFields, bindList, bindList, bindList, bindList, bindList, bindList, bindList, bindMap, bindMethods, bindMethods, bindMethodsList, bindNamedArgumentFinder, bindNull, bindNull, bindNVarchar, bindNVarchar, bindPojo, bindPojo, bindPojo, bindPojo, bindPojo, bindPojo, cleanupHandleCommit, cleanupHandleRollback, defineList, defineList, defineNamedBindings, setQueryTimeout, toStringModifier and TypeMethodDescriptionfinal PreparedBatchbind(int position, boolean value) Bind an argument positionallyfinal PreparedBatchbind(int position, byte value) Bind an argument positionallyfinal PreparedBatchbind(int position, byte[] value) Bind an argument positionallyfinal PreparedBatchbind(int position, char value) Bind an argument positionallyfinal PreparedBatchbind(int position, double value) Bind an argument positionallyfinal PreparedBatchbind(int position, float value) Bind an argument positionallyfinal PreparedBatchbind(int position, int value) Bind an argument positionallyfinal PreparedBatchbind(int position, long value) Bind an argument positionallyfinal PreparedBatchbind(int position, short value) Bind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchbind(int position, BigDecimal value) Bind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyfinal PreparedBatchBind an argument positionallyUsed if you need to have some exotic parameter bound.final PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchbind(String name, BigDecimal value) Bind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by namefinal PreparedBatchBind an argument by nameUsed if you need to have some exotic parameter bound.final PreparedBatchBind an Iterable as a SQL array.final PreparedBatchBind a Java array as a SQL array, casting each element to a new type.final PreparedBatchBind an Iterator as a SQL array.final <T> PreparedBatchbindArray(int pos, T... array) Bind a Java array as a SQL array.final PreparedBatchBind an Iterable as a SQL array.final PreparedBatchBind a Java array as a SQL array, casting each element to a new type.final PreparedBatchBind an Iterator as a SQL array.final <T> PreparedBatchBind a Java array as a SQL array.final PreparedBatchbindASCIIStream(int position, InputStream value, int length) Bind an argument positionallyfinal PreparedBatchbindASCIIStream(String name, InputStream value, int length) Bind an argument by nameBinds named parameters from JavaBean properties on the argument.Binds named parameters from JavaBean properties on the bean argument, with the given prefix.final PreparedBatchbindBeanList(String key, List<?> values, List<String> propertyNames) Bind a parameter for each value in the given list * number of property names, and defines an attribute as the comma-separated list of parameter references (using colon prefix).final PreparedBatchbindBinaryStream(int position, InputStream value, int length) Bind an argument positionallyfinal PreparedBatchbindBinaryStream(String name, InputStream value, int length) Bind an argument by namefinal PreparedBatchbindBySqlType(int position, Object value, int sqlType) Bind a value using a specific type fromjava.sql.Typesvia PreparedStatement#setObject(int, Object, int)final PreparedBatchbindBySqlType(String name, Object value, int sqlType) Bind a value using a specific type fromjava.sql.Typesvia PreparedStatement#setObject(int, Object, int)final PreparedBatchbindByType(int position, Object value, Type argumentType) Bind an argument dynamically by the type passed in.final PreparedBatchbindByType(int position, Object value, GenericType<?> argumentType) Bind an argument dynamically by the generic type passed in.final PreparedBatchbindByType(int position, Object value, QualifiedType<?> argumentType) Bind an argument dynamically by the qualified type passed in.final PreparedBatchbindByType(String name, Object value, Type argumentType) Bind an argument dynamically by the type passed in.final PreparedBatchbindByType(String name, Object value, GenericType<?> argumentType) Bind an argument dynamically by the generic type passed in.final PreparedBatchbindByType(String name, Object value, QualifiedType<?> argumentType) Bind an argument dynamically by the type passed in.bindFields(Object object) Binds public fields of the specified object as arguments for the query.bindFields(String prefix, Object object) Binds public fields of the specified object as arguments for the query.final PreparedBatchfinal PreparedBatchfinal PreparedBatchfinal PreparedBatchbindList(BiConsumer<SqlStatement, String> onEmpty, String key, Iterable<?> values) final PreparedBatchbindList(BiConsumer<SqlStatement, String> onEmpty, String key, Object... values) final PreparedBatchbindList(BiConsumer<SqlStatement, String> onEmpty, String key, Iterator<?> values) final PreparedBatchbindList(BiConsumer<SqlStatement, String> onEmpty, String key, List<?> values) Bind a parameter for each value in the given list, and defines an attribute as the comma-separated list of parameter references (using colon prefix).Binds named parameters from a map of String to Object instancesbindMethods(Object object) Binds methods with no parameters on the argument.bindMethods(String prefix, Object object) Binds methods with no parameters on the argument, with the given prefix.final PreparedBatchbindMethodsList(String key, Iterable<?> values, List<String> methodNames) For each value given, create a tuple by invoking each given method in order, and bind the tuple into aVALUES (...)format insert clause.bindNamedArgumentFinder(NamedArgumentFinder namedArgumentFinder) Binds a newNamedArgumentFinder.final PreparedBatchbindNull(int position, int sqlType) Bind NULL to be set for a given argument.final PreparedBatchBind NULL to be set for a given argument.final PreparedBatchbindNVarchar(int position, String value) Bind aStringargument positionally, asNVARCHARtype.final PreparedBatchbindNVarchar(String name, String value) Bind aStringargument by name, asNVARCHARtype.Binds named parameters from object properties on the argument.Binds named parameters from object properties on the argument.bindPojo(Object pojo, GenericType<?> type) Binds named parameters from object properties on the argument.Binds named parameters from object properties on the bean argument, with the given prefix.Binds named parameters from object properties on the bean argument, with the given prefix.bindPojo(String prefix, Object pojo, GenericType<?> type) Binds named parameters from object properties on the bean argument, with the given prefix.Transfer ownership of the handle to the statement: when the statement is closed, commit the handle's transaction (if one exists) and close the handle.When the statement is closed, roll it back then close the owning Handle.final PreparedBatchdefineList(String key, Object... values) Define an attribute as the comma-separatedStringfrom the elements of thevaluesargument.final PreparedBatchdefineList(String key, List<?> values) Define an attribute as the comma-separatedStringfrom the elements of thevaluesargument.Define all bound arguments that don't already have a definition with a boolean indicating their presence.setQueryTimeout(int seconds) Set the query timeout, in seconds, on the prepared statement.toString()Methods inherited from interface Configurable
addCustomizer, configure, define, getConfig, registerArgument, registerArgument, registerArrayType, registerArrayType, registerArrayType, registerArrayType, registerCodecFactory, registerCollector, registerCollector, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerExtension, registerRowMapper, registerRowMapper, registerRowMapper, registerRowMapper, setMapKeyColumn, setMapValueColumn, setSqlArrayArgumentStrategy, setSqlLogger, setSqlParser, setTemplateEngine, setTimingCollectorModifier and TypeMethodDescriptiondefault PreparedBatchaddCustomizer(StatementCustomizer customizer) default <C extends JdbiConfig<C>>
PreparedBatchPasses the configuration object of the given type to the configurer, then returns this object.default PreparedBatchConvenience method forgetConfig(SqlStatements.class).define(key, value)default <C extends JdbiConfig<C>>
CGets the configuration object of the given type, associated with this object.default PreparedBatchregisterArgument(ArgumentFactory factory) Convenience method forgetConfig(Arguments.class).register(factory)default PreparedBatchregisterArgument(QualifiedArgumentFactory factory) Convenience method forgetConfig(Arguments.class).register(factory)default PreparedBatchregisterArrayType(Class<?> elementType, String sqlTypeName) Convenience method forgetConfig(SqlArrayTypes.class).register(elementType, sqlTypeName)default <T> PreparedBatchregisterArrayType(Class<T> elementType, String sqlTypeName, Function<T, ?> conversion) Convenience method for registering an array type asSqlArrayTypeFactory.of(Class, String, Function).default PreparedBatchregisterArrayType(SqlArrayType<?> arrayType) Convenience method forgetConfig(SqlArrayTypes.class).register(arrayType)default PreparedBatchregisterArrayType(SqlArrayTypeFactory factory) Convenience method forgetConfig(SqlArrayTypes.class).register(factory)default PreparedBatchregisterCodecFactory(CodecFactory codecFactory) Convenience method to register aCodecFactory.default PreparedBatchregisterCollector(Type collectionType, Collector<?, ?, ?> collector) Convenience method forgetConfig(JdbiCollectors.class).register(CollectorFactory.collectorFactory(collectionType, collector))default PreparedBatchregisterCollector(CollectorFactory factory) Convenience method forgetConfig(JdbiCollectors.class).register(factory)default PreparedBatchregisterColumnMapper(Type type, ColumnMapper<?> mapper) Convenience method forgetConfig(ColumnMappers.class).register(type, mapper)default <T> PreparedBatchregisterColumnMapper(GenericType<T> type, ColumnMapper<T> mapper) Convenience method forgetConfig(ColumnMappers.class).register(type, mapper)default PreparedBatchregisterColumnMapper(ColumnMapper<?> mapper) Convenience method forgetConfig(ColumnMappers.class).register(mapper)default PreparedBatchregisterColumnMapper(ColumnMapperFactory factory) Convenience method forgetConfig(ColumnMappers.class).register(factory)default PreparedBatchConvenience method forgetConfig(ColumnMappers.class).register(factory)default <T> PreparedBatchregisterColumnMapper(QualifiedType<T> type, ColumnMapper<T> mapper) Convenience method forgetConfig(ColumnMappers.class).register(type, mapper)default PreparedBatchregisterExtension(ExtensionFactory factory) Convenience method forgetConfig(Extensions.class).register(factory)default PreparedBatchregisterRowMapper(Type type, RowMapper<?> mapper) Convenience method forgetConfig(RowMappers.class).register(type, mapper)default <T> PreparedBatchregisterRowMapper(GenericType<T> type, RowMapper<T> mapper) Convenience method forgetConfig(RowMappers.class).register(type, mapper)default PreparedBatchregisterRowMapper(RowMapper<?> mapper) Convenience method forgetConfig(RowMappers.class).register(mapper)default PreparedBatchregisterRowMapper(RowMapperFactory factory) Convenience method forgetConfig(RowMappers.class).register(factory)default PreparedBatchsetMapKeyColumn(String keyColumn) Convenience method forgetConfig(MapEntryMappers.class).setKeyColumn(keyColumn)default PreparedBatchsetMapValueColumn(String valueColumn) Convenience method forgetConfig(MapEntryMappers.class).setValueColumn(valueColumn)default PreparedBatchConvenience method forgetConfig(SqlArrayTypes.class).setArgumentStrategy(strategy)default PreparedBatchsetSqlLogger(SqlLogger sqlLogger) default PreparedBatchsetSqlParser(SqlParser parser) Convenience method forgetConfig(SqlStatements.class).setSqlParser(rewriter)default PreparedBatchsetTemplateEngine(TemplateEngine templateEngine) Convenience method forgetConfig(SqlStatements.class).setTemplateEngine(rewriter)default PreparedBatchsetTimingCollector(TimingCollector collector) Deprecated, for removal: This API element is subject to removal in a future version.useConfigurable.setSqlLogger(SqlLogger)insteadMethods inherited from interface ResultBearing
collectInto, collectInto, collectInto, collectRows, map, map, map, mapTo, mapTo, mapTo, mapTo, mapToBean, mapToMap, mapToMap, mapToMap, reduceResultSet, reduceRows, reduceRows, reduceRowsModifier and TypeMethodDescriptiondefault <R> RcollectInto(Class<R> containerType) Collect the results into a container of the given type.default ObjectcollectInto(Type containerType) Collect the results into a container of the given type.default <R> RcollectInto(GenericType<R> containerType) Collect the results into a container of the given generic type.default <A,R> R collectRows(Collector<RowView, A, R> collector) Collect the results using the given collector.default <T> ResultIterable<T> map(ColumnMapper<T> mapper) Maps this result set to aResultIterable, using the given column mapper.default <T> ResultIterable<T> Maps this result set to aResultIterable, using the given row mapper.default <T> ResultIterable<T> map(RowViewMapper<T> mapper) Maps this result set to aResultIterable, using the givenRowViewMapper.default <T> ResultIterable<T> Maps this result set to aResultIterableof the given element type.default ResultIterable<?> Maps this result set to aResultIterableof the given element type.default <T> ResultIterable<T> mapTo(GenericType<T> type) Maps this result set to aResultIterableof the given element type.default <T> ResultIterable<T> mapTo(QualifiedType<T> type) Maps this result set to aResultIterableof the given qualified element type.default <T> ResultIterable<T> Maps this result set to aResultIterableof the given element type, usingBeanMapper.default ResultIterable<Map<String, Object>> mapToMap()Maps this result set to aResultIterableofMap<String,Object>.default <T> ResultIterable<Map<String, T>> default <T> ResultIterable<Map<String, T>> mapToMap(GenericType<T> valueType) default <U> UreduceResultSet(U seed, ResultSetAccumulator<U> accumulator) Reduce the results.default <K,V> Stream <V> reduceRows(BiConsumer<Map<K, V>, RowView> accumulator) Reduce the result rows using aMap<K, V>as the result container.default <C,R> Stream <R> reduceRows(RowReducer<C, R> rowReducer) Reduce the result rows using the given row reducer.default <U> UreduceRows(U seed, BiFunction<U, RowView, U> accumulator) Reduce the results.
-
Constructor Details
-
PreparedBatch
-
PreparedBatch
-
-
Method Details
-
scanResultSet
Description copied from interface:ResultBearingInvokes the mapper with a result set supplier, and returns the value returned by the mapper.- Specified by:
scanResultSetin interfaceResultBearing- Type Parameters:
R- result type returned by the mapper.- Parameters:
resultSetScanner- 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 likeStatement.SUCCESS_NO_INFOorStatement.EXECUTE_FAILED.- Returns:
- the number of rows affected per batch part
- See Also:
-
executeAndGetModCount
Execute the batch and return the mod counts as inexecute, but as a Jdbi result iterator instead of an array.- Returns:
- the number of rows affected per batch part
- See Also:
-
executeAndReturnGeneratedKeys
@Deprecated(since="3.32.0", forRemoval=true) public ResultBearing executeAndReturnGeneratedKeys(String... columnNames) Deprecated, for removal: This API element is subject to removal in a future version.UseexecutePreparedBatch(String...)which has the same functionality but also returns the per-batch modified row counts.Execute the batch and give access to any generated keys returned by the operation.- Parameters:
columnNames- The column names for generated keys.- Returns:
- A
ResultBearingobject that can be used to access the results of the batch.
-
executePreparedBatch
Execute the batch and give access to any generated keys returned by the operation.- Parameters:
columnNames- The column names for generated keys.- Returns:
- A
BatchResultBearingobject that can be used to access the results of the batch and the per-batch modified row counts.
-
execute
Executes the batch, returning the result obtained from the givenResultProducer.- Type Parameters:
R- the type of the result- Parameters:
producer- the result producer.- Returns:
- value returned by the result producer.
-
add
Add the current binding as a saved batch and clear the binding.- Returns:
- this
-
add
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
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
-
getConfig
Description copied from interface:ConfigurableReturns the configuration registry associated with this object.- Specified by:
getConfigin interfaceConfigurable<This>- Returns:
- the configuration registry associated with this object.
-
getContext
Returns the statement context associated with this statement.- Returns:
- the statement context associated with this statement.
-
attachToHandleForCleanup
Registers with the handle for cleaning when the handle is closed.
There are some situations where Statements need to be cleaned up to avoid resource leaks. This method registers the current Statement it with the Handle. If the statement or the context are cleaned by themselves, it will automatically unregister, so in normal operations, resources should not pool for cleanup with the Handle.- Since:
- 3.35.0
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
executePreparedBatch(String...)which has the same functionality but also returns the per-batch modified row counts.