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

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

Introduction

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

Prototype

public PreparedStatementCreator newPreparedStatementCreator(@Nullable Object[] params) 

Source Link

Document

Return a new PreparedStatementCreator for the given parameters.

Usage

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

/**
 * Since hsqldb does not yet implement JDBC 3.0 generated keys,
 * this does a very rough simulation via auto-generated ID
 * and the vendor-specific "call identity()" statement.
 *
 * WARNING: The key column name won't be checked against the
 * actual name of the identity column. This method simply
 * assumes it's correct.//from  w w w  .ja  v a2s . co m
 * 
 * @param keyColumnName not used in this implementation and can be null
 */
@Override
public Long insertAndReturnGeneratedId(PreparedStatementCreatorFactory pscf, String keyColumnName,
        Object... args) {
    getJdbcOperations().update(pscf.newPreparedStatementCreator(args));
    return queryForLong("call identity()");
}

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.//from   w  ww .j av a  2s  .c  o  m
 * 
 * @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/* w  w  w  .  j a  v a 2  s  . c om*/
 * @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);
}

From source file:org.gridobservatory.greencomputing.dao.TimeseriesDao.java

public BigInteger insert(T timeseriesType) {
    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreatorFactory preparedStatementCreatorFactory = new PreparedStatementCreatorFactory(
            "insert into time_series (constant_value,start_date,end_date,acquisition_count ) "
                    + "values (?, ?, ?, ?)",
            new int[] { Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT });

    PreparedStatementCreator newPreparedStatementCreator = preparedStatementCreatorFactory
            .newPreparedStatementCreator(new Object[] {
                    timeseriesType.getConstantValue() != null ? timeseriesType.getConstantValue().getValue()
                            : "",
                    new Date(timeseriesType.getStartDate().longValue() * 1000),
                    new Date(timeseriesType.getEndDate().longValue() * 1000),
                    timeseriesType.getAcquisitionCount() });

    preparedStatementCreatorFactory.setReturnGeneratedKeys(true);
    this.getJdbcTemplate().update(newPreparedStatementCreator, keyHolder);
    BigInteger timeSeriesId = BigInteger.valueOf(keyHolder.getKey().longValue());

    insertTimeSeriesAcquisitions(timeSeriesId, timeseriesType.getA());

    return timeSeriesId;
}

From source file:com.danidemi.jlubricant.springbatch.NamedJdbcCursorItemReader.java

@Override
protected void openCursor(Connection con) {

    //MyParsedSqlDel parsedSql = getParsedSql(sql);

    //String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql.getDelegate(), paramSource);

    //String theSql = sqlToUse;

    try {//from  w w  w . j ava  2 s.c o  m
        //         if (isUseSharedExtendedConnection()) {
        //            preparedStatement = con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY,
        //                  ResultSet.HOLD_CURSORS_OVER_COMMIT);
        //         }
        //         else {
        //            preparedStatement = con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        //         }
        //         applyStatementSettings(preparedStatement);
        //         if (this.preparedStatementSetter != null) {
        //            
        //            
        //            preparedStatementSetter.setValues(preparedStatement);
        //            
        //            
        //            
        //            
        //            
        //         }
        //         this.rs = preparedStatement.executeQuery();

        ParsedSql parsedSql1 = this.getParsedSql(sql).getDelegate();
        String sqlToUse1 = NamedParameterUtils.substituteNamedParameters(parsedSql1, paramSource);
        Object[] params = NamedParameterUtils.buildValueArray(parsedSql1, paramSource, null);
        List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql1,
                paramSource);
        PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse1,
                declaredParameters);
        pscf.setResultSetType(ResultSet.TYPE_FORWARD_ONLY);
        pscf.setUpdatableResults(false);
        PreparedStatementCreator preparedStatementCreator = pscf.newPreparedStatementCreator(params);

        //con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

        preparedStatement = preparedStatementCreator.createPreparedStatement(con);
        this.rs = preparedStatement.executeQuery();

        handleWarnings(preparedStatement);
    } catch (SQLException se) {
        close();
        throw getExceptionTranslator().translate("Executing query", getSql(), se);
    }

}

From source file:anyframe.core.query.impl.jdbc.PagingJdbcTemplate.java

/**
 * Generic method to execute the update given arguments. All other update()
 * methods invoke this method.//from   w  w w . j  av a2s.c  o m
 * 
 * @return the number of rows affected by the update
 */
// 2008.05.08 - add for Handling Lob of Oracle 8i
public int update(String sql, Object[] values, LobHandler lobHandler, String lobStatement, String[] lobTypes,
        Object[] lobKeys, Object[] lobValues) {
    int updateCount = update(sql, values);
    LinkedList lobParameters = null;
    lobParameters = new LinkedList();
    for (int i = 0; i < lobTypes.length; i++) {
        int type = SQLTypeTransfer.getSQLType(lobTypes[i].toUpperCase());
        lobParameters.add(new SqlParameter(type));
    }

    PreparedStatementCreatorFactory preparedStatementFactory = new PreparedStatementCreatorFactory(lobStatement,
            lobParameters);

    query(preparedStatementFactory.newPreparedStatementCreator(lobKeys),
            new Oracle8iResultSetExtractor((AnyframeOracle8iLobHandler) lobHandler, lobValues));

    return updateCount;
}

From source file:org.easyrec.plugin.arm.store.dao.impl.RuleminingItemAssocDAOMysqlImpl.java

@Override
public int insertOrUpdateItemAssoc(ItemAssocVO<Integer, Integer> itemAssoc) {
    // validate input parameters
    if (itemAssoc == null) {
        throw new IllegalArgumentException("missing 'itemAssoc'");
    }//from w w  w .j  ava2  s  . c o  m

    // validate unique key
    validateUniqueKey(itemAssoc);
    validateAssocValue(itemAssoc);
    validateViewType(itemAssoc);

    if (logger.isDebugEnabled()) {
        logger.debug("inserting 'itemAssoc': " + itemAssoc);
    }

    // @HINT: maybe use UniqueIdService later (instead of auto_imcrement)
    StringBuilder sqlString = new StringBuilder("INSERT INTO ");
    sqlString.append(DEFAULT_TABLE_NAME);
    sqlString.append(" (");
    sqlString.append(DEFAULT_TENANT_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_VALUE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_INFO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_VIEW_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    if (itemAssoc.isActive() != null) {
        sqlString.append(DEFAULT_ACTIVE_COLUMN_NAME);
        sqlString.append(", ");
    }
    sqlString.append(DEFAULT_CHANGE_DATE_COLUMN_NAME);
    if (itemAssoc.getChangeDate() == null) {
        itemAssoc.setChangeDate(new Date(System.currentTimeMillis()));
    }
    if (itemAssoc.isActive() != null) {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    } else {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    }

    sqlString.append(" ON DUPLICATE KEY UPDATE ");
    sqlString.append(DEFAULT_ASSOC_VALUE_COLUMN_NAME);
    sqlString.append("=?, ");
    sqlString.append(DEFAULT_VIEW_TYPE_COLUMN_NAME);
    sqlString.append("=?, ");
    sqlString.append(DEFAULT_SOURCE_INFO_COLUMN_NAME);
    sqlString.append("=?, ");
    if (itemAssoc.isActive() != null) {
        sqlString.append(DEFAULT_ACTIVE_COLUMN_NAME);
        sqlString.append("=?, ");
    }
    sqlString.append(DEFAULT_CHANGE_DATE_COLUMN_NAME);
    sqlString.append("=?");

    Object[] args;
    int[] argTypes;
    if (itemAssoc.isActive() != null) {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.isActive(),
                itemAssoc.getChangeDate(), itemAssoc.getAssocValue(), itemAssoc.getViewType(),
                itemAssoc.getSourceInfo(), itemAssoc.isActive(), itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.BOOLEAN,
                Types.TIMESTAMP, Types.DOUBLE, Types.INTEGER, Types.VARCHAR, Types.BOOLEAN, Types.TIMESTAMP, };
    } else {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.getChangeDate(),
                itemAssoc.getAssocValue(), itemAssoc.getViewType(), itemAssoc.getSourceInfo(),
                itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP,
                Types.DOUBLE, Types.INTEGER, Types.VARCHAR, Types.TIMESTAMP, };
    }
    PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(sqlString.toString(),
            argTypes);

    int rowsAffected = getJdbcTemplate().update(factory.newPreparedStatementCreator(args));

    return rowsAffected;
}

From source file:org.easyrec.plugin.profileduke.store.dao.impl.ProfileSimilarityItemAssocDAOMysqlImpl.java

public int insertOrUpdateItemAssoc(ItemAssocVO<Integer, Integer> itemAssoc) {
    // validate input parameters
    if (itemAssoc == null) {
        throw new IllegalArgumentException("missing 'itemAssoc'");
    }/*from w  w  w. jav a2s .com*/

    // validate unique key
    validateUniqueKey(itemAssoc);
    validateAssocValue(itemAssoc);
    validateViewType(itemAssoc);

    if (logger.isDebugEnabled()) {
        logger.debug("inserting 'itemAssoc': " + itemAssoc);
    }

    // @HINT: maybe use UniqueIdService later (instead of auto_imcrement)
    StringBuilder sqlString = new StringBuilder("INSERT INTO ");
    sqlString.append(DEFAULT_TABLE_NAME);
    sqlString.append(" (");
    sqlString.append(DEFAULT_TENANT_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_VALUE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_INFO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_VIEW_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    if (itemAssoc.isActive() != null) {
        sqlString.append(DEFAULT_ACTIVE_COLUMN_NAME);
        sqlString.append(", ");
    }
    sqlString.append(DEFAULT_CHANGE_DATE_COLUMN_NAME);
    if (itemAssoc.getChangeDate() == null) {
        itemAssoc.setChangeDate(new Date(System.currentTimeMillis()));
    }
    if (itemAssoc.isActive() != null) {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    } else {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    }

    sqlString.append(" ON DUPLICATE KEY UPDATE ");
    sqlString.append(DEFAULT_ASSOC_VALUE_COLUMN_NAME);
    sqlString.append("=?, ");
    sqlString.append(DEFAULT_VIEW_TYPE_COLUMN_NAME);
    sqlString.append("=?, ");
    sqlString.append(DEFAULT_SOURCE_INFO_COLUMN_NAME);
    sqlString.append("=?, ");
    if (itemAssoc.isActive() != null) {
        sqlString.append(DEFAULT_ACTIVE_COLUMN_NAME);
        sqlString.append("=?, ");
    }
    sqlString.append(DEFAULT_CHANGE_DATE_COLUMN_NAME);
    sqlString.append("=?");

    Object[] args;
    int[] argTypes;
    if (itemAssoc.isActive() != null) {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.isActive(),
                itemAssoc.getChangeDate(), itemAssoc.getAssocValue(), itemAssoc.getViewType(),
                itemAssoc.getSourceInfo(), itemAssoc.isActive(), itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.BOOLEAN,
                Types.TIMESTAMP, Types.DOUBLE, Types.INTEGER, Types.VARCHAR, Types.BOOLEAN, Types.TIMESTAMP, };
    } else {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.getChangeDate(),
                itemAssoc.getAssocValue(), itemAssoc.getViewType(), itemAssoc.getSourceInfo(),
                itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP,
                Types.DOUBLE, Types.INTEGER, Types.VARCHAR, Types.TIMESTAMP, };
    }
    PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(sqlString.toString(),
            argTypes);

    int rowsAffected = getJdbcTemplate().update(factory.newPreparedStatementCreator(args));

    return rowsAffected;
}