Spring JDBC - What is PreparedStatementSetter?

1. What is PreparedStatementSetter?

It is a callback interface used by JdbcTemplate after PreparedStatement is created to set the values in the statement object.

2. Example usage

PreparedStatementSetter is also functional interface, so we will use lambda expression in this example to demonstrate PreparedStatementSetter usage. We will use it in update method of JdbcTemplate.

int updateCount = jdbcTemplate.update("insert into product(name, category, description) values(?,?,?)", ps -> {
ps.setString(1, "Lenovo Bag");
ps.setString(2, "bag");
ps.setString(3, "Handcrafted bags by Lenovo");

log.info(() -> String.format("Product inserted: %d", updateCount));

You can see the full example code on Github.

Tags: Spring Framework, Spring JDBC, PreparedStatementSetter example with JdbcTemplate

← Back home