Example usage for org.springframework.jdbc.core PreparedStatementSetter setValues

List of usage examples for org.springframework.jdbc.core PreparedStatementSetter setValues

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementSetter setValues.

Prototype

void setValues(PreparedStatement ps) throws SQLException;

Source Link

Document

Set parameter values on the given PreparedStatement.

Usage

From source file:com.baidu.rigel.demo.dao.BaseDao.java

/**
 * Insert and get key./*w w  w .j  a  v a  2s.  co m*/
 * 
 * @param sql
 *            the sql
 * @param params
 *            the params
 * @return the object
 */
public Object insertAndGetKey(final String sql, final Object... params) {
    final KeyHolder key = new GeneratedKeyHolder();

    simpleJdbcTemplate.getJdbcOperations().update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            PreparedStatementSetter pss = new ArgPreparedStatementSetter(params);
            try {
                if (pss != null) {
                    pss.setValues(ps);
                }
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
            return ps;
        }
    }, key);
    return key.getKey();
}

From source file:cc.tooyoung.common.db.JdbcTemplate.java

protected int update(final PreparedStatementCreator psc, final PreparedStatementSetter pss)
        throws DataAccessException {

    if (ApiLogger.isTraceEnabled()) {
        ApiLogger.trace("Executing prepared SQL update");
    }//from  ww  w .  ja  v  a 2s .c  o m

    Integer result = (Integer) execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            try {
                if (pss != null) {
                    pss.setValues(ps);
                }
                int rows = ps.executeUpdate();
                if (ApiLogger.isTraceEnabled()) {
                    ApiLogger.trace("SQL update affected " + rows + " rows");
                }
                return new Integer(rows);
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    }, true);
    return result.intValue();
}

From source file:cc.tooyoung.common.db.JdbcTemplate.java

/**
 * Query using a prepared statement, allowing for a PreparedStatementCreator
 * and a PreparedStatementSetter. Most other query methods use this method,
 * but application code will always work with either a creator or a setter.
 * @param psc Callback handler that can create a PreparedStatement given a
 * Connection//from  w ww .  ja  v  a 2s .c  o  m
 * @param pss object that knows how to set values on the prepared statement.
 * If this is null, the SQL will be assumed to contain no bind parameters.
 * @param rse object that will extract results.
 * @return an arbitrary result object, as returned by the ResultSetExtractor
 * @throws DataAccessException if there is any problem
 */
public Object query(PreparedStatementCreator psc, final PreparedStatementSetter pss,
        final ResultSetExtractor rse) throws DataAccessException {

    Assert.notNull(rse, "ResultSetExtractor must not be null");
    if (ApiLogger.isTraceEnabled())
        ApiLogger.trace("Executing prepared SQL query");

    return execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            ResultSet rs = null;
            try {
                if (pss != null) {
                    pss.setValues(ps);
                }
                rs = ps.executeQuery();
                ResultSet rsToUse = rs;
                if (nativeJdbcExtractor != null) {
                    rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
                }
                return rse.extractData(rsToUse);
            } finally {
                JdbcUtils.closeResultSet(rs);
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    }, false);
}

From source file:org.opoo.oqs.spring.SpringQuery.java

protected Object doCall() throws QueryException {
    final PreparedStatementSetter pss = new ArgTypePreparedStatementSetter(valueArray(), typeArray());
    final ResultSetExtractor rse = createResultSetExtractor(createListResultSetHandler());
    return jdbcTemplate.execute(getSql(), new CallableStatementCallback() {
        public Object doInCallableStatement(CallableStatement callableStatement)
                throws SQLException, DataAccessException {

            if (getQueryTimeout() > 0) {
                callableStatement.setQueryTimeout(getQueryTimeout());
            }/*from   w w w .j a va2 s  . c  om*/

            pss.setValues(callableStatement);

            boolean retVal = callableStatement.execute();
            int updateCount = callableStatement.getUpdateCount();
            if (log.isDebugEnabled()) {
                log.debug("CallableStatement.execute() returned '" + retVal + "'");
                log.debug("CallableStatement.getUpdateCount() returned " + updateCount);
            }

            ResultSet rs = callableStatement.getResultSet();
            try {
                if (rs != null && rse != null) {
                    return rse.extractData(rs);
                }
            } finally {
                JdbcUtils.closeResultSet(rs);
            }

            if (updateCount > 0) {
                return new Integer(updateCount);
            }
            return null;
        }
    });
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

/**
 * Query using a prepared statement, allowing for a PreparedStatementCreator
 * and a PreparedStatementSetter. Most other query methods use this method,
 * but application code will always work with either a creator or a setter.
 * @param psc Callback handler that can create a PreparedStatement given a
 * Connection/*from ww  w.j av  a2 s.  c  o m*/
 * @param pss object that knows how to set values on the prepared statement.
 * If this is null, the SQL will be assumed to contain no bind parameters.
 * @param rse object that will extract results.
 * @return an arbitrary result object, as returned by the ResultSetExtractor
 * @throws DataAccessException if there is any problem
 */
protected Object query(PreparedStatementCreator psc, final PreparedStatementSetter pss,
        final ResultSetExtractor rse) throws DataAccessException {
    if (logger.isDebugEnabled()) {
        String sql = getSql(psc);
        logger.debug("Executing SQL query" + (sql != null ? " [" + sql + "]" : ""));
    }
    return execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            ResultSet rs = null;
            try {
                if (pss != null) {
                    pss.setValues(ps);
                }
                if (getFetchSize() > 0) {
                    ps.setFetchSize(getFetchSize());
                }
                rs = ps.executeQuery();
                ResultSet rsToUse = rs;
                if (nativeJdbcExtractor != null) {
                    rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
                }
                //??pojo
                return rse.extractData(rsToUse);
            } finally {
                JdbcUtils.closeResultSet(rs);
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    });
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

protected int update(final PreparedStatementCreator psc, final PreparedStatementSetter pss)
        throws DataAccessException {
    if (logger.isDebugEnabled()) {
        String sql = getSql(psc);
        logger.debug("Executing SQL update" + (sql != null ? " [" + sql + "]" : ""));
    }/*w w w .  ja v a2  s  . c  om*/
    Integer result = (Integer) execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            try {
                if (pss != null) {
                    //PreparedStatement?
                    pss.setValues(ps);
                }
                int rows = ps.executeUpdate();
                if (logger.isDebugEnabled()) {
                    logger.debug("SQL update affected " + rows + " rows");
                }
                return new Integer(rows);
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    });
    return result.intValue();
}