Java tutorial
/* * Copyright 2006-2007 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package lcn.module.batch.core.item.database.support; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Map; import lcn.module.batch.core.reflection.ReflectionSupport; import org.springframework.util.ReflectionUtils; /** * MethodMapItemPreparedStatementSetter * ItemPreparedStatementSetter ?? * param? ? Method Value? Map? * PreparedStatementSetter? . * * @author * @since 2012.07.20 * @version 1.0 * @param <T> * @see <pre> * ?(Modification Information) * * ? ? * --------- ----------- --------------------------- * 2012.07.20 ? * 2012.07.30 ?? * </pre> */ public class MethodMapItemPreparedStatementSetter<T> extends EgovItemPreparedStatementSetter<T> { /** * params ? ? sqlType PreparedStatement? ?? */ public void setValues(T item, PreparedStatement ps, String[] params, String[] sqlTypes, Map<String, Method> methodMap) throws SQLException { ReflectionSupport<T> reflector = new ReflectionSupport<T>(); for (int i = 0; i < params.length; i++) { try { if (sqlTypes[i].equals("String")) { ps.setString(i + 1, (String) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("int")) { ps.setInt(i + 1, (Integer) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("double")) { ps.setDouble(i + 1, (Double) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("Date")) { ps.setDate(i + 1, (Date) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("byte")) { ps.setByte(i + 1, (Byte) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("short")) { ps.setShort(i + 1, (Short) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("boolean")) { ps.setBoolean(i + 1, (Boolean) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("long")) { ps.setLong(i + 1, (Long) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("Float")) { ps.setFloat(i + 1, (Float) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("BigDecimal")) { ps.setBigDecimal(i + 1, (BigDecimal) reflector.invokeGettterMethod(item, params[i], methodMap)); } else if (sqlTypes[i].equals("byte[]")) { ps.setBytes(i + 1, (byte[]) reflector.invokeGettterMethod(item, params[i], methodMap)); } else { throw new SQLException(); } } catch (IllegalArgumentException e) { ReflectionUtils.handleReflectionException(e); } } } }