liveBook · Manning

PreparedStatementCreator

As explained in Spring in Action, Sixth Edition

A Spring JDBC callback object that creates a configured SQL prepared statement with specified parameter values.

A PreparedStatementCreator is a Spring JDBC callback object that creates a configured SQL prepared statement from a query and supplied parameter values. A PreparedStatementCreatorFactory can produce one for an insert operation. section 3.1.4

A typical insert workflow uses it to:

  • Describe the insert SQL and parameter types with PreparedStatementCreatorFactory. section 3.1.4
  • Enable generated-key retrieval when the database assigns the record ID. section 3.1.4
  • Create the PreparedStatementCreator with values from the object being persisted. section 3.1.4
  • Pass the creator and a GeneratedKeyHolder to JdbcOperations.update(), then copy the generated ID back to the domain object. section 3.1.4

Saving a taco with its generated ID

A repository creates a factory for inserting Taco fields, enables generated keys, and calls newPreparedStatementCreator() with the taco name, creation date, order ID, and order key. It then passes the resulting creator to jdbcOperations.update(psc, keyHolder), reads the generated key, and assigns it to the Taco object before saving ingredient references. section 3.1.4

package tacos.data;
 
import java.sql.Types;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
 
import org.springframework.asm.Type;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
 
import tacos.IngredientRef;
import tacos.Taco;
import tacos.TacoOrder;
 
@Repository
public class JdbcOrderRepository implements OrderRepository {
 
  private JdbcOperations jdbcOperations;
 
  public JdbcOrderRepository(JdbcOperations jdbcOperations) {
    this.jdbcOperations = jdbcOperations;
  }
 
  @Override
  @Transactional
  public TacoOrder save(TacoOrder order) {
    PreparedStatementCreatorFactory pscf =
      new PreparedStatementCreatorFactory(
        "insert into Taco_Order "
        + "(delivery_name, delivery_street, delivery_city, "
        + "delivery_state, delivery_zip, cc_number, "
        + "cc_expiration, cc_cvv, placed_at) "
        + "values (?,?,?,?,?,?,?,?,?)",
        Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
        Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
        Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP
    );
    pscf.setReturnGeneratedKeys(true);
 
    order.setPlacedAt(new Date());
    PreparedStatementCreator psc =
        pscf.newPreparedStatementCreator(
            Arrays.asList(
                order.getDeliveryName(),
                order.getDeliveryStreet(),
                order.getDeliveryCity(),
                order.getDeliveryState(),
                order.getDeliveryZip(),
                order.getCcNumber(),
                order.getCcExpiration(),
                order.getCcCVV(),
                order.getPlacedAt()));
 
    GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcOperations.update(psc, keyHolder);
    long orderId = keyHolder.getKey().longValue();
    order.setId(orderId);
 
    List<Taco> tacos = order.getTacos();
    int i=0;
    for (Taco taco : tacos) {
      saveTaco(orderId, i++, taco);
    }
 
    return order;
  }
}

Browse the repository listing showing PreparedStatementCreatorFactory, generated-key configuration, creator construction, and JdbcOperations.update(). section 3.1.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains factory configuration, parameter binding, update execution, and generated-key copying, without detailing JDBC statement internals.
Examples
Demonstrates inserting TacoOrder and Taco records, retrieving generated IDs, and saving ingredient references.
What it assumes
Assumes basic SQL inserts and domain objects while introducing Spring JDBC factories, callbacks, and key holders through an implementation.
What you can do after
Apply it via Spring JDBC to insert records and transfer database-generated IDs into domain objects.
How it connects
Connects PreparedStatementCreatorFactory with JdbcOperations, GeneratedKeyHolder, JDBC, and database-generated keys.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.