001/* 002 * Copyright 2006-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.item.database.support; 017 018import java.sql.PreparedStatement; 019import java.sql.SQLException; 020import java.util.List; 021 022import org.springframework.batch.item.database.JdbcCursorItemReader; 023import org.springframework.beans.factory.InitializingBean; 024import org.springframework.jdbc.core.PreparedStatementSetter; 025import org.springframework.jdbc.core.SqlTypeValue; 026import org.springframework.jdbc.core.StatementCreatorUtils; 027import org.springframework.util.Assert; 028 029/** 030 * Implementation of the {@link PreparedStatementSetter} interface that accepts 031 * a list of values to be set on a PreparedStatement. This is usually used in 032 * conjunction with the {@link JdbcCursorItemReader} to allow for the replacement 033 * of bind variables when generating the cursor. The order of the list will be 034 * used to determine the ordering of setting variables. For example, the first 035 * item in the list will be the first bind variable set. (i.e. it will 036 * correspond to the first '?' in the SQL statement) 037 * 038 * @author Lucas Ward 039 * 040 */ 041public class ListPreparedStatementSetter implements 042PreparedStatementSetter, InitializingBean { 043 044 private List<?> parameters; 045 046 public ListPreparedStatementSetter() {} 047 048 public ListPreparedStatementSetter(List<?> parameters) { 049 this.parameters = parameters; 050 } 051 052 @Override 053 public void setValues(PreparedStatement ps) throws SQLException { 054 for (int i = 0; i < parameters.size(); i++) { 055 StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, parameters.get(i)); 056 } 057 } 058 059 /** 060 * The parameter values that will be set on the PreparedStatement. 061 * It is assumed that their order in the List is the order of the parameters 062 * in the PreparedStatement. 063 * 064 * @param parameters list containing the parameter values to be used. 065 * @deprecated In favor of the constructor 066 */ 067 @Deprecated 068 public void setParameters(List<?> parameters) { 069 this.parameters = parameters; 070 } 071 072 @Override 073 public void afterPropertiesSet() throws Exception { 074 Assert.notNull(parameters, "Parameters must be provided"); 075 } 076}