Use spring batch to call stored proc for adding/updating records via jdbc

24 views Asked by At

I have a task to schedule and trigger a database call, and the data returned from this database call must be sent in batches to call a stored proc. How can I achieve this using spring batch with JDBC template?

I've gone through few tutorials on how to achieve this, but none have been specific to use spring batch and call a stored procedure to update data.

1

There are 1 answers

0
Francisco Valadares On

Here's an example created by Mkyoun on the website mkyong.com

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;

    public int[][] batchUpdate(List<Book> books, int batchSize) {

        int[][] updateCounts = jdbcTemplate.batchUpdate(
                "update books set price = ? where id = ?",
                books,
                batchSize,
                new ParameterizedPreparedStatementSetter<Book>() {
                    public void setValues(PreparedStatement ps, Book argument) 
                        throws SQLException {
                        ps.setBigDecimal(1, argument.getPrice());
                        ps.setLong(2, argument.getId());
                    }
                });
        return updateCounts;

    }