Example usage for org.springframework.jdbc.core JdbcOperations execute

List of usage examples for org.springframework.jdbc.core JdbcOperations execute

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcOperations execute.

Prototype

@Nullable
<T> T execute(String callString, CallableStatementCallback<T> action) throws DataAccessException;

Source Link

Document

Execute a JDBC data access operation, implemented as callback action working on a JDBC CallableStatement.

Usage

From source file:com.nortal.petit.core.dialect.OracleSqlDialect.java

@SuppressWarnings("unchecked")
@Override// w w w . j  a v  a2s  . c  o m
public <B> B insertReturningId(JdbcOperations jdbcOperations, String sql, String idColumn,
        final Object... params) {
    final String actualSql = new StringBuilder("BEGIN ").append(sql)
            .append(" RETURNING " + idColumn + " INTO ?; END;").toString();
    try {
        return (B) jdbcOperations.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {
                CallableStatement cs = con.prepareCall(actualSql);
                ArgPreparedStatementSetter.setValues(cs, params, 1);
                cs.registerOutParameter(params.length + 1, Types.DECIMAL);
                return cs;
            }
        }, new CallableStatementCallback<B>() {
            @Override
            public B doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
                cs.execute();
                BigDecimal bd = cs.getBigDecimal(params.length + 1);
                return (B) Long.valueOf(bd.longValue());
            }
        });
    } catch (RuntimeException e) {
        LOG.error("Error processing SQL '" + sql + "' with parameters: " + StringUtils.join(params, "; "));
        throw e;
    }
}

From source file:org.danann.cernunnos.sql.StatementTask.java

public void perform(TaskRequest req, TaskResponse res) {
    final DataSource dataSource = DataSourceRetrievalUtil.getDataSource(dataSourcePhrase, connectionPhrase, req,
            res);/* ww w.j av a 2  s.co m*/

    final SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    final JdbcOperations jdbcOperations = jdbcTemplate.getJdbcOperations();

    //Setup the callback class to do the PreparedStatement parameter binding and statement execution
    final PreparedStatementCallback preparedStatementCallback = new PhraseParameterPreparedStatementCallback(
            this.parameters, req, res);

    //Get the SQL and execute it in a PreparedStatement
    final String fSql = (String) sql.evaluate(req, res);
    jdbcOperations.execute(fSql, preparedStatementCallback);
}