org.springframework.jdbc.core.PreparedStatementCreatorFactory Java Examples

The following examples show how to use org.springframework.jdbc.core.PreparedStatementCreatorFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NamedParameterJdbcTemplate.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example #2
Source File: BatchJdbcTemplateTest.java    From buffer-slayer with Apache License 2.0 6 votes vote down vote up
@Test
public void updateWithPreparedStatementCreator() {
  reporter = AsyncReporter.builder(new JdbcTemplateSender(underlying))
      .pendingMaxMessages(2)
      .bufferedMaxMessages(2)
      .messageTimeout(0, TimeUnit.MILLISECONDS)
      .build();
  batchJdbcTemplate = new BatchJdbcTemplate(underlying, reporter);

  PreparedStatementCreatorFactory creatorFactory = new PreparedStatementCreatorFactory(INSERTION);
  creatorFactory.addParameter(new SqlParameter(Types.VARCHAR));
  creatorFactory.addParameter(new SqlParameter(Types.TIMESTAMP));

  PreparedStatementCreator creator = creatorFactory
      .newPreparedStatementCreator(new Object[]{randomString(), new Date()});

  batchJdbcTemplate.update(creator);
  batchJdbcTemplate.update(creator);

  reporter.flush();
  int rowCount = batchJdbcTemplate.queryForObject("SELECT COUNT(1) FROM test;", Integer.class);
  assertEquals(2, rowCount);
}
 
Example #3
Source File: JdbcDemonstratingRepository.java    From Spring-Boot-2-Fundamentals with MIT License 6 votes vote down vote up
/**
 * Demonstrate how to insert and retrieve a generated key.
 * <p>
 * The {@code id} column of the {@code short_message} table can generated ids.
 * In order to retrieve the newly generated id, we need an update that returns
 * some data (a JDBC 3 feature). Spring supports this with a keyholder.
 * <p>
 * Unfortunately, {@link JdbcTemplate#update(PreparedStatementCreator, KeyHolder)}
 * needs a {@link PreparedStatementCreator} which is a bit overwhelming
 * here.
 */
public void insertMessage(Author author, String text) {
    String sql = "INSERT INTO short_message(author_id, posted_time, message_text)" +
            " VALUES(?, ?, ?)";
    Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

    PreparedStatementCreatorFactory statementCreatorFactory =
            new PreparedStatementCreatorFactory(sql,
                    Types.INTEGER, Types.TIMESTAMP, Types.VARCHAR);
    statementCreatorFactory.setReturnGeneratedKeys(true);
    statementCreatorFactory.setGeneratedKeysColumnNames("id");
    PreparedStatementCreator preparedStatementCreator =
            statementCreatorFactory.newPreparedStatementCreator(
                    new Object[]{author.getId(), currentTimestamp, text});

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int update = jdbcTemplate.update(preparedStatementCreator, keyHolder);
    log.info("auto-insert created {} row with key {}", update, keyHolder.getKey());
}
 
Example #4
Source File: JdbcDemonstratingRepository.java    From Spring-Boot-2-Fundamentals with MIT License 6 votes vote down vote up
/**
 * Demonstrate how to insert and retrieve a generated key.
 * <p>
 * The {@code id} column of the {@code short_message} table can generated ids.
 * In order to retrieve the newly generated id, we need an update that returns
 * some data (a JDBC 3 feature). Spring supports this with a keyholder.
 * <p>
 * Unfortunately, {@link JdbcTemplate#update(PreparedStatementCreator, KeyHolder)}
 * needs a {@link PreparedStatementCreator} which is a bit overwhelming
 * here.
 */
public void insertMessage(Author author, String text) {
    String sql = "INSERT INTO short_message(author_id, posted_time, message_text)" +
            " VALUES(?, ?, ?)";
    Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

    PreparedStatementCreatorFactory statementCreatorFactory =
            new PreparedStatementCreatorFactory(sql,
                    Types.INTEGER, Types.TIMESTAMP, Types.VARCHAR);
    statementCreatorFactory.setReturnGeneratedKeys(true);
    statementCreatorFactory.setGeneratedKeysColumnNames("id");
    PreparedStatementCreator preparedStatementCreator =
            statementCreatorFactory.newPreparedStatementCreator(
                    new Object[]{author.getId(), currentTimestamp, text});

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int update = jdbcTemplate.update(preparedStatementCreator, keyHolder);
    log.info("auto-insert created {} row with key {}", update, keyHolder.getKey());
}
 
Example #5
Source File: JdbcTacoRepository.java    From spring-in-action-5-samples with Apache License 2.0 6 votes vote down vote up
private long saveTacoInfo(Taco taco) {
  taco.setCreatedAt(new Date());
  PreparedStatementCreator psc =
      new PreparedStatementCreatorFactory(
          "insert into Taco (name, createdAt) values (?, ?)",
          Types.VARCHAR, Types.TIMESTAMP
      ).newPreparedStatementCreator(
          Arrays.asList(
              taco.getName(),
              new Timestamp(taco.getCreatedAt().getTime())));

  KeyHolder keyHolder = new GeneratedKeyHolder();
  jdbc.update(psc, keyHolder);

  return keyHolder.getKey().longValue();
}
 
Example #6
Source File: NamedParameterJdbcTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example #7
Source File: NamedParameterJdbcTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
	if (batchArgs.length == 0) {
		return new int[0];
	}

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, batchArgs[0]);

	return getJdbcOperations().batchUpdate(
			pscf.getSql(),
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					pscf.newPreparedStatementSetter(values).setValues(ps);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example #8
Source File: NamedParameterJdbcTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public int update(
		String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
		throws DataAccessException {

	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	if (keyColumnNames != null) {
		pscf.setGeneratedKeysColumnNames(keyColumnNames);
	}
	else {
		pscf.setReturnGeneratedKeys(true);
	}
	return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
 
Example #9
Source File: NamedParameterJdbcTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
	if (batchArgs.length == 0) {
		return new int[0];
	}

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, batchArgs[0]);

	return getJdbcOperations().batchUpdate(
			pscf.getSql(),
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					pscf.newPreparedStatementSetter(values).setValues(ps);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example #10
Source File: ApplicationDaoImpl.java    From Spring-MVC-Blueprints with MIT License 5 votes vote down vote up
@Override
public void setStudentAccount(Application application) {
	
	String sql = "INSERT INTO tblStudentUser (username, password) VALUES (?, ?)";
	PreparedStatementCreatorFactory psCreateFactory = new PreparedStatementCreatorFactory(sql, new int[]{Types.VARCHAR, Types.VARCHAR});
	jdbcTemplate.update(psCreateFactory.newPreparedStatementCreator(new Object[]{application.getUsername(),application.getPassword()}));
	
}
 
Example #11
Source File: DatasourceServletAction.java    From ureport with Apache License 2.0 5 votes vote down vote up
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
	ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #12
Source File: SpringPreparedStatementCreatorFactory.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void queryUnsafe(String input) {
    String sql = "select * from Users where name = '" + input + "' id=?";

    new PreparedStatementCreatorFactory(sql);
    new PreparedStatementCreatorFactory(sql, new int[] {Types.INTEGER});
    new PreparedStatementCreatorFactory(sql, new ArrayList<SqlParameter>());
}
 
Example #13
Source File: SqlOperation.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example #14
Source File: NamedParameterJdbcTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Build a PreparedStatementCreator based on the given SQL and named parameters.
 * <p>Note: Not used for the {@code update} variant with generated key handling.
 * @param sql SQL to execute
 * @param paramSource container of arguments to bind
 * @return the corresponding PreparedStatementCreator
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #15
Source File: SqlOperation.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example #16
Source File: NamedParameterJdbcTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Build a PreparedStatementCreator based on the given SQL and named parameters.
 * <p>Note: Not used for the {@code update} variant with generated key handling.
 * @param sql SQL to execute
 * @param paramSource container of arguments to bind
 * @return the corresponding PreparedStatementCreator
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #17
Source File: SqlOperation.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}
	this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

	onCompileInternal();
}
 
Example #18
Source File: NamedParameterJdbcTemplate.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * Build a PreparedStatementCreator based on the given SQL and named parameters.
 * <p>Note: Not used for the {@code update} variant with generated key handling.
 * @param sql SQL to execute
 * @param paramSource container of arguments to bind
 * @return the corresponding PreparedStatementCreator
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
	ParsedSql parsedSql = getParsedSql(sql);
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #19
Source File: BaseRepository.java    From fish-admin with MIT License 5 votes vote down vote up
public long insert(List<String> columns, List<Object> args, int[] types) {
    String insertColumns = primaryKeyName();
    String insertPlaceholders = "nextval('"+tableName()+"_id_seq')";
    for (String column : columns) {
        insertColumns += "," + column;
        insertPlaceholders += ",?";
    }

    insertColumns += ", " + createdAtName();
    insertPlaceholders += ", now()";
    insertColumns += ", " + updatedAtName();
    insertPlaceholders += ", now()";

    String sql = "insert into " + tableName() +
            " ("+insertColumns+") values ("+insertPlaceholders+")";

    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sql, types);
    pscf.setReturnGeneratedKeys(true);
    try {
        logger.info("insert.sql: {}, args: {}", sql, jsonMapper.writeValueAsString(args));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    writeJdbcTemplate.update(pscf.newPreparedStatementCreator(args), keyHolder);
    if (keyHolder.getKeyList() != null && !keyHolder.getKeyList().isEmpty()) {
        return (long) keyHolder.getKeyList().get(0).get(primaryKeyName());
    }
    return -1;
}
 
Example #20
Source File: DefaultMessageStore.java    From qmq with Apache License 2.0 5 votes vote down vote up
private PreparedStatementCreatorFactory createFactory() {
    List<SqlParameter> parameters = new ArrayList<>();
    parameters.add(new SqlParameter("content", Types.LONGVARCHAR));
    parameters.add(new SqlParameter("create_time", Types.TIMESTAMP));
    PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(sqlStatementProvider.getInsertSql(), parameters);
    factory.setReturnGeneratedKeys(true);
    return factory;
}
 
Example #21
Source File: SqlOperation.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(resolveSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}

	onCompileInternal();
}
 
Example #22
Source File: JdbcSpittleRepository.java    From Project with Apache License 2.0 5 votes vote down vote up
public void update(Spittle spittle) {
 String sql = "update Spittle set message=?, created_at=?, latitude=?, longitude=? "
 		+ "where id=?";
 PreparedStatementCreatorFactory creatorFactory = 
	  new PreparedStatementCreatorFactory(sql, Types.VARCHAR, Types.TIMESTAMP, Types.DOUBLE, Types.DOUBLE, Types.INTEGER);
 PreparedStatementCreator creator = creatorFactory.newPreparedStatementCreator(new Object[] {
	  spittle.getMessage(),
	  spittle.getTime(),
	  spittle.getLatitude(),
	  spittle.getLongitude(),
	  spittle.getId()
 });
 jdbc.update(creator);
}
 
Example #23
Source File: JdbcSpittleRepository.java    From Project with Apache License 2.0 5 votes vote down vote up
public Spittle save(Spittle spittle) {
  
  System.out.println("--> " + spittle.getMessage());
  
  
  String sql = "insert into Spittle (message, created_at, latitude, longitude)" +
      " values (?, ?, ?, ?)";
  PreparedStatementCreatorFactory statementCreatorFactory = new PreparedStatementCreatorFactory(sql, Types.VARCHAR, Types.TIMESTAMP, Types.DOUBLE, Types.DOUBLE);
  statementCreatorFactory.setReturnGeneratedKeys(true);
  PreparedStatementCreator creator = statementCreatorFactory.newPreparedStatementCreator(new Object[] {
      spittle.getMessage(),
      spittle.getTime(),
      spittle.getLatitude(),
      spittle.getLongitude()
  });

  
  GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
  jdbc.update(creator, keyHolder);
  return new Spittle(
      keyHolder.getKey().longValue(), 
      spittle.getMessage(), 
      spittle.getTime(), 
      spittle.getLongitude(), 
      spittle.getLatitude());
  
  
}
 
Example #24
Source File: SqlOperation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Overridden method to configure the PreparedStatementCreatorFactory
 * based on our declared parameters.
 */
@Override
protected final void compileInternal() {
	this.preparedStatementFactory = new PreparedStatementCreatorFactory(resolveSql(), getDeclaredParameters());
	this.preparedStatementFactory.setResultSetType(getResultSetType());
	this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());
	this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());
	if (getGeneratedKeysColumnNames() != null) {
		this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());
	}

	onCompileInternal();
}
 
Example #25
Source File: TransactionImpl.java    From gemini with Apache License 2.0 5 votes vote down vote up
private PreparedStatement getPreparedStatement(String sql, @Nullable Map<String, ?> parameters, boolean returnKeys) throws SQLException {
    SqlParameterSource paramSource = new MapSqlParameterSource(parameters);
    ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
    Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
    PreparedStatementCreatorFactory psCreatorFactory = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
    psCreatorFactory.setReturnGeneratedKeys(returnKeys);
    PreparedStatementCreator psCreator = psCreatorFactory.newPreparedStatementCreator(params);
    PreparedStatement preparedStatement = psCreator.createPreparedStatement(connection);
    logger.debug(preparedStatement.unwrap(PreparedStatement.class).toString());
    return preparedStatement;
}
 
Example #26
Source File: NamedParameterJdbcTemplate.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreatorFactory} based on the given SQL and named parameters.
 * @param parsedSql parsed representation of the given SQL statement
 * @param paramSource container of arguments to bind
 * @return the corresponding {@link PreparedStatementCreatorFactory}
 * @since 5.1.3
 * @see #getPreparedStatementCreator(String, SqlParameterSource, Consumer)
 * @see #getParsedSql(String)
 */
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
		ParsedSql parsedSql, SqlParameterSource paramSource) {

	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	return new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
}
 
Example #27
Source File: NamedParameterJdbcTemplate.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreator} based on the given SQL and named parameters.
 * <p>Note: Used for the {@code update} variant with generated key handling, and also
 * delegated from {@link #getPreparedStatementCreator(String, SqlParameterSource)}.
 * @param sql the SQL statement to execute
 * @param paramSource container of arguments to bind
 * @param customizer callback for setting further properties on the
 * {@link PreparedStatementCreatorFactory} in use), applied before the
 * actual {@code newPreparedStatementCreator} call
 * @return the corresponding {@link PreparedStatementCreator}
 * @since 5.0.5
 * @see #getParsedSql(String)
 * @see PreparedStatementCreatorFactory#PreparedStatementCreatorFactory(String, List)
 * @see PreparedStatementCreatorFactory#newPreparedStatementCreator(Object[])
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource,
		@Nullable Consumer<PreparedStatementCreatorFactory> customizer) {

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource);
	if (customizer != null) {
		customizer.accept(pscf);
	}
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #28
Source File: NamedParameterJdbcTemplate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreatorFactory} based on the given SQL and named parameters.
 * @param parsedSql parsed representation of the given SQL statement
 * @param paramSource container of arguments to bind
 * @return the corresponding {@link PreparedStatementCreatorFactory}
 * @since 5.1.3
 * @see #getPreparedStatementCreator(String, SqlParameterSource, Consumer)
 * @see #getParsedSql(String)
 */
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
		ParsedSql parsedSql, SqlParameterSource paramSource) {

	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
	List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
	return new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
}
 
Example #29
Source File: NamedParameterJdbcTemplate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreator} based on the given SQL and named parameters.
 * <p>Note: Used for the {@code update} variant with generated key handling, and also
 * delegated from {@link #getPreparedStatementCreator(String, SqlParameterSource)}.
 * @param sql the SQL statement to execute
 * @param paramSource container of arguments to bind
 * @param customizer callback for setting further properties on the
 * {@link PreparedStatementCreatorFactory} in use), applied before the
 * actual {@code newPreparedStatementCreator} call
 * @return the corresponding {@link PreparedStatementCreator}
 * @since 5.0.5
 * @see #getParsedSql(String)
 * @see PreparedStatementCreatorFactory#PreparedStatementCreatorFactory(String, List)
 * @see PreparedStatementCreatorFactory#newPreparedStatementCreator(Object[])
 */
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource,
		@Nullable Consumer<PreparedStatementCreatorFactory> customizer) {

	ParsedSql parsedSql = getParsedSql(sql);
	PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory(parsedSql, paramSource);
	if (customizer != null) {
		customizer.accept(pscf);
	}
	Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
	return pscf.newPreparedStatementCreator(params);
}
 
Example #30
Source File: NamedParameterJdbcTemplate.java    From sqlhelper with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Build a {@link PreparedStatementCreatorFactory} based on the given SQL and named parameters.
 *
 * @param parsedSql   parsed representation of the given SQL statement
 * @param paramSource container of arguments to bind
 * @return the corresponding {@link PreparedStatementCreatorFactory}
 * @see #getParsedSql(String)
 * @since Spring 5.1.3
 */
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory(
        ParsedSql parsedSql, SqlParameterSource paramSource) {
    String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
    List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
    return new NamedParameterPreparedStatementCreatorFactory(sqlToUse, declaredParameters);
}