1 16 17 package org.springframework.jdbc.core; 18 19 import java.io.StringWriter ; 20 import java.math.BigDecimal ; 21 import java.sql.PreparedStatement ; 22 import java.sql.SQLException ; 23 import java.sql.Types ; 24 import java.util.Arrays ; 25 import java.util.Calendar ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.springframework.util.ClassUtils; 33 34 54 public abstract class StatementCreatorUtils { 55 56 private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class); 57 58 private static final boolean charSequenceAvailable = 61 ClassUtils.isPresent("java.lang.CharSequence", StatementCreatorUtils.class.getClassLoader()); 62 63 64 73 public static void setParameterValue( 74 PreparedStatement ps, int paramIndex, SqlParameter declaredParam, Object inValue) 75 throws SQLException { 76 77 setParameterValue(ps, paramIndex, declaredParam.getSqlType(), declaredParam.getTypeName(), inValue); 78 } 79 80 92 public static void setParameterValue( 93 PreparedStatement ps, int paramIndex, int sqlType, String typeName, Object inValue) 94 throws SQLException { 95 96 if (logger.isDebugEnabled()) { 97 logger.debug("Setting SQL statement parameter value: column index " + paramIndex + 98 ", parameter value [" + inValue + 99 "], value class [" + (inValue != null ? inValue.getClass().getName() : "null") + 100 "], SQL type " + (sqlType == SqlTypeValue.TYPE_UNKNOWN ? "unknown" : Integer.toString(sqlType))); 101 } 102 103 if (inValue == null) { 104 if (sqlType == SqlTypeValue.TYPE_UNKNOWN) { 105 boolean useSetObject = false; 106 try { 107 useSetObject = (ps.getConnection().getMetaData().getDatabaseProductName().indexOf("Informix") != -1); 108 } 109 catch (Throwable ex) { 110 logger.debug("Could not check database product name", ex); 111 } 112 if (useSetObject) { 113 ps.setObject(paramIndex, null); 114 } 115 else { 116 ps.setNull(paramIndex, Types.NULL); 117 } 118 } 119 else if (typeName != null) { 120 ps.setNull(paramIndex, sqlType, typeName); 121 } 122 else { 123 ps.setNull(paramIndex, sqlType); 124 } 125 } 126 127 else { if (inValue instanceof SqlTypeValue) { 129 ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName); 130 } 131 else if (sqlType == Types.VARCHAR) { 132 ps.setString(paramIndex, inValue.toString()); 133 } 134 else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) { 135 if (inValue instanceof BigDecimal ) { 136 ps.setBigDecimal(paramIndex, (BigDecimal ) inValue); 137 } 138 else { 139 ps.setObject(paramIndex, inValue, sqlType); 140 } 141 } 142 else if (sqlType == Types.DATE) { 143 if (inValue instanceof java.util.Date ) { 144 if (inValue instanceof java.sql.Date ) { 145 ps.setDate(paramIndex, (java.sql.Date ) inValue); 146 } 147 else { 148 ps.setDate(paramIndex, new java.sql.Date (((java.util.Date ) inValue).getTime())); 149 } 150 } 151 else if (inValue instanceof Calendar ) { 152 Calendar cal = (Calendar ) inValue; 153 ps.setDate(paramIndex, new java.sql.Date (cal.getTime().getTime()), cal); 154 } 155 else { 156 ps.setObject(paramIndex, inValue, Types.DATE); 157 } 158 } 159 else if (sqlType == Types.TIME) { 160 if (inValue instanceof java.util.Date ) { 161 if (inValue instanceof java.sql.Time ) { 162 ps.setTime(paramIndex, (java.sql.Time ) inValue); 163 } 164 else { 165 ps.setTime(paramIndex, new java.sql.Time (((java.util.Date ) inValue).getTime())); 166 } 167 } 168 else if (inValue instanceof Calendar ) { 169 Calendar cal = (Calendar ) inValue; 170 ps.setTime(paramIndex, new java.sql.Time (cal.getTime().getTime()), cal); 171 } 172 else { 173 ps.setObject(paramIndex, inValue, Types.TIME); 174 } 175 } 176 else if (sqlType == Types.TIMESTAMP) { 177 if (inValue instanceof java.util.Date ) { 178 if (inValue instanceof java.sql.Timestamp ) { 179 ps.setTimestamp(paramIndex, (java.sql.Timestamp ) inValue); 180 } 181 else { 182 ps.setTimestamp(paramIndex, new java.sql.Timestamp (((java.util.Date ) inValue).getTime())); 183 } 184 } 185 else if (inValue instanceof Calendar ) { 186 Calendar cal = (Calendar ) inValue; 187 ps.setTimestamp(paramIndex, new java.sql.Timestamp (cal.getTime().getTime()), cal); 188 } 189 else { 190 ps.setObject(paramIndex, inValue, Types.TIMESTAMP); 191 } 192 } 193 else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) { 194 if (isStringValue(inValue)) { 195 ps.setString(paramIndex, inValue.toString()); 196 } 197 else if (isDateValue(inValue)) { 198 ps.setTimestamp(paramIndex, new java.sql.Timestamp (((java.util.Date ) inValue).getTime())); 199 } 200 else if (inValue instanceof Calendar ) { 201 Calendar cal = (Calendar ) inValue; 202 ps.setTimestamp(paramIndex, new java.sql.Timestamp (cal.getTime().getTime())); 203 } 204 else { 205 ps.setObject(paramIndex, inValue); 207 } 208 } 209 else { 210 ps.setObject(paramIndex, inValue, sqlType); 212 } 213 } 214 } 215 216 219 private static boolean isStringValue(Object inValue) { 220 if (charSequenceAvailable) { 221 return (inValue instanceof CharSequence || inValue instanceof StringWriter ); 223 } 224 else { 225 return (inValue instanceof String || inValue instanceof StringBuffer || inValue instanceof StringWriter ); 227 } 228 } 229 230 234 private static boolean isDateValue(Object inValue) { 235 return (inValue instanceof java.util.Date && !(inValue instanceof java.sql.Date || 236 inValue instanceof java.sql.Time || inValue instanceof java.sql.Timestamp )); 237 } 238 239 246 public static void cleanupParameters(Object [] paramValues) { 247 if (paramValues != null) { 248 cleanupParameters(Arrays.asList(paramValues)); 249 } 250 } 251 252 259 public static void cleanupParameters(Collection paramValues) { 260 if (paramValues != null) { 261 for (Iterator it = paramValues.iterator(); it.hasNext();) { 262 Object inValue = it.next(); 263 if (inValue instanceof DisposableSqlTypeValue) { 264 ((DisposableSqlTypeValue) inValue).cleanup(); 265 } 266 } 267 } 268 } 269 270 } 271 | Popular Tags |