本文整理汇总了Java中org.springframework.jdbc.core.SqlParameter类的典型用法代码示例。如果您正苦于以下问题:Java SqlParameter类的具体用法?Java SqlParameter怎么用?Java SqlParameter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SqlParameter类属于org.springframework.jdbc.core包,在下文中一共展示了SqlParameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateNamedParameters
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Validate the named parameters passed to an execute method based on declared parameters.
* Subclasses should invoke this method before every {@code executeQuery()} or
* {@code update()} method.
* @param parameters parameter Map supplied. May be {@code null}.
* @throws InvalidDataAccessApiUsageException if the parameters are invalid
*/
protected void validateNamedParameters(Map<String, ?> parameters) throws InvalidDataAccessApiUsageException {
checkCompiled();
Map<String, ?> paramsToUse = (parameters != null ? parameters : Collections.<String, Object> emptyMap());
int declaredInParameters = 0;
for (SqlParameter param : this.declaredParameters) {
if (param.isInputValueProvided()) {
if (!supportsLobParameters() &&
(param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) {
throw new InvalidDataAccessApiUsageException(
"BLOB or CLOB parameters are not allowed for this kind of operation");
}
if (param.getName() != null && !paramsToUse.containsKey(param.getName())) {
throw new InvalidDataAccessApiUsageException("The parameter named '" + param.getName() +
"' was not among the parameters supplied: " + paramsToUse.keySet());
}
declaredInParameters++;
}
}
validateParameterCount(paramsToUse.size(), declaredInParameters);
}
示例2: updateWithPreparedStatementCreator
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
@Test
public void updateWithPreparedStatementCreator() {
reporter = AsyncReporter.builder(new JdbcTemplateSender(underlying))
.pendingMaxMessages(2)
.bufferedMaxMessages(2)
.messageTimeout(0, TimeUnit.MILLISECONDS)
.build();
batchJdbcTemplate = new BatchJdbcTemplate(underlying, reporter);
PreparedStatementCreatorFactory creatorFactory = new PreparedStatementCreatorFactory(INSERTION);
creatorFactory.addParameter(new SqlParameter(Types.VARCHAR));
creatorFactory.addParameter(new SqlParameter(Types.TIMESTAMP));
PreparedStatementCreator creator = creatorFactory
.newPreparedStatementCreator(new Object[]{randomString(), new Date()});
batchJdbcTemplate.update(creator);
batchJdbcTemplate.update(creator);
reporter.flush();
int rowCount = batchJdbcTemplate.queryForObject("SELECT COUNT(1) FROM test;", Integer.class);
assertEquals(2, rowCount);
}
示例3: callInsert
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
private void callInsert(String tokenKey, List<SqlParameter> paramList, byte[] compressedBytes, String procedureSecurestoreInsert) {
jdbcTemplate.call(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection connection) throws SQLException {
CallableStatement callableStatement = connection.prepareCall(procedureSecurestoreInsert);
callableStatement.setString(1, OAUTH_ACCESS_TOKEN_STORE);
callableStatement.setBoolean(2, FOR_XS_APPLICATIONUSER);
callableStatement.setString(3, tokenKey);
callableStatement.setBytes(4, compressedBytes);
return callableStatement;
}
}, paramList);
}
示例4: readAccessTokenFromSecureStore
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
private OAuth2AccessToken readAccessTokenFromSecureStore(String tokenKey) {
if (tokenKey.length() > TOKEN_KEY_MAX_LENGTH) {
throw new IllegalArgumentException(Messages.TOKEN_KEY_FORMAT_NOT_VALID);
}
SqlParameter storeNameParam = new SqlParameter(Types.VARCHAR);
SqlParameter forXsApplicationUserParam = new SqlParameter(Types.BOOLEAN);
SqlParameter keyParam = new SqlParameter(Types.VARCHAR);
SqlOutParameter valueParam = new SqlOutParameter("VALUE", Types.VARBINARY);
List<SqlParameter> paramList = Arrays.asList(storeNameParam, forXsApplicationUserParam, keyParam, valueParam);
Map<String, Object> result = null;
try {
result = callRetrieve(tokenKey, paramList, PROCEDURE_SECURESTORE_RETRIEVE);
} catch (BadSqlGrammarException e) {
throwIfShouldNotIgnore(e, RETRIEVE_PROCEDURE_NAME);
result = callRetrieve(tokenKey, paramList, PROCEDURE_SECURESTORE_RETRIEVE_LEGACY);
}
byte[] tokenBytes = (byte[]) result.get("VALUE");
if (tokenBytes == null) {
throw new IllegalArgumentException(Messages.TOKEN_NOT_FOUND_IN_SECURE_STORE);
}
byte[] decompressedBytes = CompressUtil.decompress(tokenBytes);
OAuth2AccessToken accessToken = TokenUtil.createToken(new String(decompressedBytes, StandardCharsets.UTF_8));
return accessToken;
}
示例5: validateParameters
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Validate the parameters passed to an execute method based on declared parameters.
* Subclasses should invoke this method before every {@code executeQuery()}
* or {@code update()} method.
* @param parameters parameters supplied (may be {@code null})
* @throws InvalidDataAccessApiUsageException if the parameters are invalid
*/
protected void validateParameters(Object[] parameters) throws InvalidDataAccessApiUsageException {
checkCompiled();
int declaredInParameters = 0;
for (SqlParameter param : this.declaredParameters) {
if (param.isInputValueProvided()) {
if (!supportsLobParameters() &&
(param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) {
throw new InvalidDataAccessApiUsageException(
"BLOB or CLOB parameters are not allowed for this kind of operation");
}
declaredInParameters++;
}
}
validateParameterCount((parameters != null ? parameters.length : 0), declaredInParameters);
}
示例6: update
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
@Override
public int update(
String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
throws DataAccessException {
ParsedSql parsedSql = getParsedSql(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);
if (keyColumnNames != null) {
pscf.setGeneratedKeysColumnNames(keyColumnNames);
}
else {
pscf.setReturnGeneratedKeys(true);
}
return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
示例7: addDeclaredParameter
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Add a declared parameter to the list of parameters for the call.
* Only parameters declared as {@code SqlParameter} and {@code SqlInOutParameter}
* will be used to provide input values. This is different from the {@code StoredProcedure} class
* which for backwards compatibility reasons allows input values to be provided for parameters declared
* as {@code SqlOutParameter}.
* @param parameter the {@link SqlParameter} to add
*/
public void addDeclaredParameter(SqlParameter parameter) {
Assert.notNull(parameter, "The supplied parameter must not be null");
if (!StringUtils.hasText(parameter.getName())) {
throw new InvalidDataAccessApiUsageException(
"You must specify a parameter name when declaring parameters for \"" + getProcedureName() + "\"");
}
this.declaredParameters.add(parameter);
if (logger.isDebugEnabled()) {
logger.debug("Added declared parameter for [" + getProcedureName() + "]: " + parameter.getName());
}
}
示例8: compileInternal
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Method to perform the actual compilation. Subclasses can override this template method to perform
* their own compilation. Invoked after this base class's compilation is complete.
*/
protected void compileInternal() {
this.callMetaDataContext.initializeMetaData(getJdbcTemplate().getDataSource());
// iterate over the declared RowMappers and register the corresponding SqlParameter
for (Map.Entry<String, RowMapper<?>> entry : this.declaredRowMappers.entrySet()) {
SqlParameter resultSetParameter =
this.callMetaDataContext.createReturnResultSetParameter(entry.getKey(), entry.getValue());
this.declaredParameters.add(resultSetParameter);
}
this.callMetaDataContext.processParameters(this.declaredParameters);
this.callString = this.callMetaDataContext.createCallString();
if (logger.isDebugEnabled()) {
logger.debug("Compiled stored procedure. Call string is [" + this.callString + "]");
}
this.callableStatementFactory =
new CallableStatementCreatorFactory(getCallString(), this.callMetaDataContext.getCallParameters());
this.callableStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());
onCompileInternal();
}
示例9: compileInternal
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Delegate method to perform the actual compilation.
* <p>Subclasses can override this template method to perform their own compilation.
* Invoked after this base class's compilation is complete.
*/
protected void compileInternal() {
this.callMetaDataContext.initializeMetaData(getJdbcTemplate().getDataSource());
// Iterate over the declared RowMappers and register the corresponding SqlParameter
for (Map.Entry<String, RowMapper<?>> entry : this.declaredRowMappers.entrySet()) {
SqlParameter resultSetParameter =
this.callMetaDataContext.createReturnResultSetParameter(entry.getKey(), entry.getValue());
this.declaredParameters.add(resultSetParameter);
}
this.callMetaDataContext.processParameters(this.declaredParameters);
this.callString = this.callMetaDataContext.createCallString();
if (logger.isDebugEnabled()) {
logger.debug("Compiled stored procedure. Call string is [" + this.callString + "]");
}
this.callableStatementFactory =
new CallableStatementCreatorFactory(getCallString(), this.callMetaDataContext.getCallParameters());
this.callableStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());
onCompileInternal();
}
示例10: testQueryWithoutEnoughParams
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
@Test
public void testQueryWithoutEnoughParams() {
MappingSqlQuery<Integer> query = new MappingSqlQuery<Integer>() {
@Override
protected Integer mapRow(ResultSet rs, int rownum) throws SQLException {
return rs.getInt(1);
}
};
query.setDataSource(dataSource);
query.setSql(SELECT_ID_WHERE);
query.declareParameter(new SqlParameter(COLUMN_NAMES[0], COLUMN_TYPES[0]));
query.declareParameter(new SqlParameter(COLUMN_NAMES[1], COLUMN_TYPES[1]));
query.compile();
thrown.expect(InvalidDataAccessApiUsageException.class);
query.execute();
}
示例11: testQueryWithMissingMapParams
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
@Test
public void testQueryWithMissingMapParams() {
MappingSqlQuery<Integer> query = new MappingSqlQuery<Integer>() {
@Override
protected Integer mapRow(ResultSet rs, int rownum) throws SQLException {
return rs.getInt(1);
}
};
query.setDataSource(dataSource);
query.setSql(SELECT_ID_WHERE);
query.declareParameter(new SqlParameter(COLUMN_NAMES[0], COLUMN_TYPES[0]));
query.declareParameter(new SqlParameter(COLUMN_NAMES[1], COLUMN_TYPES[1]));
query.compile();
thrown.expect(InvalidDataAccessApiUsageException.class);
query.executeByNamedParam(Collections.singletonMap(COLUMN_NAMES[0], "value"));
}
示例12: TemplateStoredProcedure
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
public TemplateStoredProcedure(JdbcTemplate jdbcTemplate, Template template) {
this.template = template;
setDataSource(jdbcTemplate.getDataSource());
setSql(template.getProcedureName());
for (Object parameter : template.getParameterList()) {
if (parameter instanceof InputParameter) {
InputParameter inputParameter = (InputParameter) parameter;
declareParameter(new SqlParameter(inputParameter.getName(), inputParameter.getSqlType()));
inputParameterList.add(inputParameter);
} else if (parameter instanceof OutParameter) {
OutParameter outParameter = (OutParameter) parameter;
declareParameter(new SqlOutParameter(outParameter.getOutValueMapKey(), outParameter.getSqlType()));
setFunction(false);
}
}
LOG.debug("Compiling stored procedure: {}", template.getProcedureName());
compile();
}
示例13: createParams
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
private List<SqlParameter> createParams() {
List<SqlParameter> params = new ArrayList<>();
for (Object parameter : template.getParameterList()) {
if (parameter instanceof InputParameter) {
InputParameter inputParameter = (InputParameter) parameter;
params.add(new SqlParameter(inputParameter.getName(), inputParameter.getSqlType()));
} else {
throw new UnsupportedOperationException("Only IN parameters supported by batch!");
}
}
return params;
}
示例14: updateNamed
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
public <T> int updateNamed(String namedSql, T bean) {
String sql = NamedParameterUtils.parseSqlStatementIntoString(namedSql);
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(namedSql);
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(bean);
List<SqlParameter> params = NamedParameterUtils.buildSqlParameterList(parsedSql, source);
Object[] args = NamedParameterUtils.buildValueArray(parsedSql, source, params);
return jdbcTemplate.update(sql, args);
}
示例15: insert
import org.springframework.jdbc.core.SqlParameter; //导入依赖的package包/类
/**
* Method 'insert'
*
* @param dto
* @return GoodreceiveDetailPk
*/
public GoodreceiveDetailPk insert(GoodreceiveDetail dto)
{
SqlUpdate su = new SqlUpdate( dataSource, "INSERT INTO " + getTableName() + " ( grnumber, productcode, qtyreal, status, expirydate, remark, lotid, qtygood, qtydmg, producttype ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
su.declareParameter( new SqlParameter( java.sql.Types.VARCHAR) );
su.declareParameter( new SqlParameter( java.sql.Types.VARCHAR) );
su.declareParameter( new SqlParameter( java.sql.Types.DECIMAL) );
su.declareParameter( new SqlParameter( java.sql.Types.VARCHAR) );
su.declareParameter( new SqlParameter( java.sql.Types.TIMESTAMP) );
su.declareParameter( new SqlParameter( java.sql.Types.VARCHAR) );
su.declareParameter( new SqlParameter( java.sql.Types.INTEGER) );
su.declareParameter( new SqlParameter( java.sql.Types.DECIMAL) );
su.declareParameter( new SqlParameter( java.sql.Types.DECIMAL) );
su.declareParameter( new SqlParameter( java.sql.Types.VARCHAR) );
su.compile();
su.update( new Object[] { dto.getGrnumber(),dto.getProductcode(),dto.getQtyreal(),dto.getStatus(),dto.getExpirydate(),dto.getRemark(),dto.getLotid(),dto.getQtygood(),dto.getQtydmg(),dto.getProducttype()} );
GoodreceiveDetailPk pk = new GoodreceiveDetailPk();
pk.setId( jdbcTemplate.queryForInt("select @@IDENTITY") );
return pk;
}