Example usage for org.springframework.jdbc.core PreparedStatementCreatorFactory setGeneratedKeysColumnNames

List of usage examples for org.springframework.jdbc.core PreparedStatementCreatorFactory setGeneratedKeysColumnNames

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementCreatorFactory setGeneratedKeysColumnNames.

Prototype

public void setGeneratedKeysColumnNames(String... names) 

Source Link

Document

Set the column names of the auto-generated keys.

Usage

From source file:org.sakaiproject.imagegallery.springutil.BaseJdbcTemplate.java

/**
 * The default implementation assumes that the DB vendor and table definition
 * support JDBC 3.0 generated keys. Non-JDBC-3.0 DBs will need to hack another
 * implementation of this method./*  w w w.j a v a2 s . c om*/
 * 
 * @param pscf object that provides SQL and the types of any required parameters
 * @param keyColumnName not used by some non-JDBC-3.0 DBs; needed for Oracle
 * even if there's only one candidate column since otherwise ROWNUM is returned
 * @param args the parameters for the query
 * @return the generated ID for the new record
 */
public Long insertAndReturnGeneratedId(PreparedStatementCreatorFactory pscf, String keyColumnName,
        Object... args) {
    pscf.setGeneratedKeysColumnNames(new String[] { keyColumnName });
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcOperations().update(pscf.newPreparedStatementCreator(args), keyHolder);
    return new Long(keyHolder.getKey().longValue());
}

From source file:io.kahu.hawaii.util.call.sql.AbortableQuery.java

/**
 * 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/*  www . j  a va2 s .c o m*/
 * @param paramSource container of arguments to bind
 * @return the corresponding PreparedStatementCreator
 */
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);

    pscf.setGeneratedKeysColumnNames(idColumn);

    return pscf.newPreparedStatementCreator(params);
}