Example usage for org.springframework.batch.item.database JdbcParameterUtils countParameterPlaceholders

List of usage examples for org.springframework.batch.item.database JdbcParameterUtils countParameterPlaceholders

Introduction

In this page you can find the example usage for org.springframework.batch.item.database JdbcParameterUtils countParameterPlaceholders.

Prototype

public static int countParameterPlaceholders(String sql, List<String> namedParameterHolder) 

Source Link

Document

Count the occurrences of the character placeholder in an SQL string sql.

Usage

From source file:org.springframework.batch.item.database.JdbcBatchItemWriter.java

/**
 * Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a
 * parameter source./*from w  w w  . j  a  v  a  2  s.c o m*/
 */
@Override
public void afterPropertiesSet() {
    Assert.notNull(namedParameterJdbcTemplate, "A DataSource or a NamedParameterJdbcTemplate is required.");
    Assert.notNull(sql, "An SQL statement is required.");
    List<String> namedParameters = new ArrayList<String>();
    parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql, namedParameters);
    if (namedParameters.size() > 0) {
        if (parameterCount != namedParameters.size()) {
            throw new InvalidDataAccessApiUsageException(
                    "You can't use both named parameters and classic \"?\" placeholders: " + sql);
        }
        usingNamedParameters = true;
    }
    if (!usingNamedParameters) {
        Assert.notNull(itemPreparedStatementSetter,
                "Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter");
    }
}