Skip to main content

Posts

Showing posts with the label BatchPreparedStatementSetter example

Spring JDBC performing batch update example

There are many different variants of #batchUpdate methods available in JdbcTemplate . We will specifically look into those who uses BatchPreparedStatementSetter and ParameterizedPreparedStatementSetter . What is BatchPreparedStatementSetter? It is an interface used by JdbcTemplate to execute batch updates. This has methods to determine the batch size and method to set parameters in the PreparedStatement. Using this, JdbcTemplate will run only execute single batch based on the batch size returned by implementation this interface. How to use BatchPreparedStatementSetter? Let's create a ProductBatchPreparedStatementSetter which can set parameters in the statement. public class ProductBatchPreparedStatementSetter implements BatchPreparedStatementSetter { private final List products; public ProductBatchPreparedStatementSetter(List products) { Objects.requireNonNull(products); // Ideally you should do a defensive copy of this list. // this.products = ne