1 16 17 package org.springframework.jdbc.core; 18 19 import java.math.BigDecimal ; 20 import java.sql.Blob ; 21 import java.sql.Clob ; 22 import java.sql.ResultSet ; 23 import java.sql.ResultSetMetaData ; 24 import java.sql.SQLException ; 25 26 import org.springframework.dao.TypeMismatchDataAccessException; 27 import org.springframework.jdbc.IncorrectResultSetColumnCountException; 28 import org.springframework.jdbc.support.JdbcUtils; 29 import org.springframework.util.NumberUtils; 30 31 45 public class SingleColumnRowMapper implements RowMapper { 46 47 private Class requiredType; 48 49 50 54 public SingleColumnRowMapper() { 55 } 56 57 61 public SingleColumnRowMapper(Class requiredType) { 62 this.requiredType = requiredType; 63 } 64 65 70 public void setRequiredType(Class requiredType) { 71 this.requiredType = requiredType; 72 } 73 74 75 84 public Object mapRow(ResultSet rs, int rowNum) throws SQLException { 85 ResultSetMetaData rsmd = rs.getMetaData(); 87 int nrOfColumns = rsmd.getColumnCount(); 88 if (nrOfColumns != 1) { 89 throw new IncorrectResultSetColumnCountException(1, nrOfColumns); 90 } 91 92 Object result = getColumnValue(rs, 1, this.requiredType); 94 if (result != null && this.requiredType != null && !this.requiredType.isInstance(result)) { 95 try { 97 return convertValueToRequiredType(result, this.requiredType); 98 } 99 catch (IllegalArgumentException ex) { 100 throw new TypeMismatchDataAccessException( 101 "Type mismatch affecting row number " + rowNum + " and column type '" + 102 rsmd.getColumnTypeName(1) + "': " + ex.getMessage()); 103 } 104 } 105 return result; 106 } 107 108 130 protected Object getColumnValue(ResultSet rs, int index, Class requiredType) throws SQLException { 131 if (requiredType != null) { 132 Object value = null; 133 boolean wasNullCheck = false; 134 135 if (String .class.equals(requiredType)) { 137 value = rs.getString(index); 138 } 139 else if (Boolean .class.equals(requiredType)) { 140 value = (rs.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE); 141 wasNullCheck = true; 142 } 143 else if (Byte .class.equals(requiredType)) { 144 value = new Byte (rs.getByte(index)); 145 wasNullCheck = true; 146 } 147 else if (Short .class.equals(requiredType)) { 148 value = new Short (rs.getShort(index)); 149 wasNullCheck = true; 150 } 151 else if (Integer .class.equals(requiredType)) { 152 value = new Integer (rs.getInt(index)); 153 wasNullCheck = true; 154 } 155 else if (Long .class.equals(requiredType)) { 156 value = new Long (rs.getLong(index)); 157 wasNullCheck = true; 158 } 159 else if (Float .class.equals(requiredType)) { 160 value = new Float (rs.getFloat(index)); 161 wasNullCheck = true; 162 } 163 else if (Double .class.equals(requiredType) || Number .class.equals(requiredType)) { 164 value = new Double (rs.getDouble(index)); 165 wasNullCheck = true; 166 } 167 else if (byte[].class.equals(requiredType)) { 168 value = rs.getBytes(index); 169 } 170 else if (java.sql.Date .class.equals(requiredType)) { 171 value = rs.getDate(index); 172 } 173 else if (java.sql.Time .class.equals(requiredType)) { 174 value = rs.getTime(index); 175 } 176 else if (java.sql.Timestamp .class.equals(requiredType) || java.util.Date .class.equals(requiredType)) { 177 value = rs.getTimestamp(index); 178 } 179 else if (BigDecimal .class.equals(requiredType)) { 180 value = rs.getBigDecimal(index); 181 } 182 else if (Blob .class.equals(requiredType)) { 183 value = rs.getBlob(index); 184 } 185 else if (Clob .class.equals(requiredType)) { 186 value = rs.getClob(index); 187 } 188 else { 189 value = rs.getObject(index); 191 } 192 193 if (wasNullCheck && value != null && rs.wasNull()) { 196 value = null; 197 } 198 return value; 199 } 200 201 else { 202 return getColumnValue(rs, index); 204 } 205 } 206 207 220 protected Object getColumnValue(ResultSet rs, int index) throws SQLException { 221 return JdbcUtils.getResultSetValue(rs, index); 222 } 223 224 238 protected Object convertValueToRequiredType(Object value, Class requiredType) { 239 if (String .class.equals(this.requiredType)) { 240 return value.toString(); 241 } 242 else if (Number .class.isAssignableFrom(this.requiredType)) { 243 if (value instanceof Number ) { 244 return NumberUtils.convertNumberToTargetClass(((Number ) value), this.requiredType); 246 } 247 else { 248 return NumberUtils.parseNumber(value.toString(), this.requiredType); 250 } 251 } 252 else { 253 throw new IllegalArgumentException ( 254 "Value [" + value + "] is of type [" + value.getClass().getName() + 255 "] and cannot be converted to required type [" + this.requiredType.getName() + "]"); 256 } 257 } 258 259 } 260 | Popular Tags |