Example usage for org.springframework.jdbc.support JdbcUtils getResultSetValue

List of usage examples for org.springframework.jdbc.support JdbcUtils getResultSetValue

Introduction

In this page you can find the example usage for org.springframework.jdbc.support JdbcUtils getResultSetValue.

Prototype

@Nullable
public static Object getResultSetValue(ResultSet rs, int index, @Nullable Class<?> requiredType)
        throws SQLException 

Source Link

Document

Retrieve a JDBC column value from a ResultSet, using the specified value type.

Usage

From source file:com.nortal.petit.core.SqlSupport.java

/**
 * @param <K>//from   w  w  w .j  ava2  s  .  c  om
 *            type of key
 * @param <V>
 *            type of value
 * @param sql
 *            query must return result set of two columns, where first will
 *            be treated as key
 * @return map reflecting result of given query
 */
public <K, V> Map<K, V> getMap(final Class<K> keyType, final Class<V> valueType, String sql, Object... args) {
    return getJdbcTemplate().query(sql, args, new ResultSetExtractor<Map<K, V>>() {
        @Override
        public Map<K, V> extractData(ResultSet rs) throws SQLException, DataAccessException {
            Map<K, V> result = new LinkedHashMap<K, V>();
            while (rs.next()) {
                K key = (K) JdbcUtils.getResultSetValue(rs, 1, keyType);
                V value = (V) JdbcUtils.getResultSetValue(rs, 2, valueType);
                result.put(key, value);
            }
            return result;
        }
    });
}

From source file:com.gzj.tulip.jade.rowmapper.BeanPropertyRowMapper.java

/**
 * Extract the values for all columns in the current row.
 * <p>/*  w  ww. j a v a 2s. c  o  m*/
 * Utilizes public setters and result set metadata.
 * 
 * @see java.sql.ResultSetMetaData
 */
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
    // spring's : Object mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    // jade's : private Object instantiateClass(this.mappedClass);
    // why: ??mappedClass.newInstranceBeanUtils.instantiateClass(mappedClass)1?
    Object mappedObject = instantiateClass(this.mappedClass);
    BeanWrapper bw = new BeanWrapperImpl(mappedObject);

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();

    boolean warnEnabled = logger.isWarnEnabled();
    boolean debugEnabled = logger.isDebugEnabled();
    Set<String> populatedProperties = (checkProperties ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
        PropertyDescriptor pd = this.mappedFields.get(column);
        if (pd != null) {
            try {
                Object value = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
                if (debugEnabled && rowNumber == 0) {
                    logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type "
                            + pd.getPropertyType());
                }
                bw.setPropertyValue(pd.getName(), value);
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + column + " to property " + pd.getName(), ex);
            }
        } else {
            if (checkColumns) {
                throw new InvalidDataAccessApiUsageException("Unable to map column '" + column
                        + "' to any properties of bean " + this.mappedClass.getName());
            }
            if (warnEnabled && rowNumber == 0) {
                logger.warn("Unable to map column '" + column + "' to any properties of bean "
                        + this.mappedClass.getName());
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:io.github.huherto.springyRecords.RecordMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <p>The default implementation calls
 * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}.
 * Subclasses may override this to check specific value types upfront,
 * or to post-process values return from <code>getResultSetValue</code>.
 * @param rs is the ResultSet holding the data
 * @param index is the column index/*from   ww  w .  j av  a  2 s .c o  m*/
 * @param pd the bean property that each result object is expected to match
 * (or <code>null</code> if none specified)
 * @return the Object value
 * @throws SQLException in case of extraction failure
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)
 */
protected Object getColumnValue(ResultSet rs, int index, Field field) throws SQLException {

    Object value = JdbcUtils.getResultSetValue(rs, index, field.getType());
    if (value != null && value instanceof String && autoTrimStrings) {
        return ((String) value).trim();
    }
    return value;
}

From source file:com.sinosoft.one.data.jade.rowmapper.BeanPropertyRowMapper.java

/**
 * Extract the values for all columns in the current row.
 * <p>/*from   w ww  . j  a va  2 s  .  c o m*/
 * Utilizes public setters and result set metadata.
 * 
 * @see java.sql.ResultSetMetaData
 */
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
    // spring's : Object mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    // jade's : private Object instantiateClass(this.mappedClass);
    // why: ??mappedClass.newInstranceBeanUtils.instantiateClass(mappedClass)1?
    Object mappedObject = instantiateClass(this.mappedClass);
    BeanWrapper bw = new BeanWrapperImpl(mappedObject);
    bw.setConversionService(this.conversionService);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();

    boolean warnEnabled = logger.isWarnEnabled();
    boolean debugEnabled = logger.isDebugEnabled();
    Set<String> populatedProperties = (checkProperties ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
        PropertyDescriptor pd = this.mappedFields.get(column);
        if (pd != null) {
            try {
                Object value = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
                if (debugEnabled && rowNumber == 0) {
                    logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type "
                            + pd.getPropertyType());
                }
                bw.setPropertyValue(pd.getName(), value);
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + column + " to property " + pd.getName(), ex);
            }
        } else {
            if (checkColumns) {
                throw new InvalidDataAccessApiUsageException("Unable to map column '" + column
                        + "' to any properties of bean " + this.mappedClass.getName());
            }
            if (warnEnabled && rowNumber == 0) {
                logger.warn("Unable to map column '" + column + "' to any properties of bean "
                        + this.mappedClass.getName());
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:com.wantscart.jade.core.BeanPropertyRowMapper.java

/**
 * Extract the values for all columns in the current row.
 * <p/>/*from  w ww  .  ja  va  2s  .  c  o  m*/
 * Utilizes public setters and result set metadata.
 *
 * @see java.sql.ResultSetMetaData
 */
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
    // spring's : Object mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    // jade's : private Object instantiateClass(this.mappedClass);
    // why: ??mappedClass.newInstranceBeanUtils.instantiateClass(mappedClass)1?
    Object mappedObject = instantiateClass(this.mappedClass);
    BeanWrapper bw = new BeanWrapperImpl(mappedObject);

    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();

    boolean warnEnabled = logger.isWarnEnabled();
    boolean debugEnabled = logger.isDebugEnabled();
    Set<String> populatedProperties = (checkProperties ? new HashSet<String>() : null);

    for (int index = 1; index <= columnCount; index++) {
        String columnName = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase();
        TableSchema.Column col = this.mappedFields.get(columnName);
        if (col != null) {
            try {
                Object value = JdbcUtils.getResultSetValue(rs, index, (Class<?>) col.getColumnType());
                if (debugEnabled && rowNumber == 0) {
                    logger.debug("Mapping column '" + columnName + "' to property '" + col.getName()
                            + "' of type " + col.getType());
                }
                if (col.isSerializable()) {
                    if (col.getSerializer().getClass() != NullSerializer.class) {
                        value = col.getSerializer().deserialize(value,
                                col.getGenericType() != null ? col.getGenericType() : col.getType());
                    } else {
                        Serializable instance = (Serializable) BeanUtils
                                .instantiateClass((Class) col.getType());
                        instance.deserialize(value);
                        value = instance;
                    }

                }
                bw.setPropertyValue(col.getOriginName(), value);
                if (populatedProperties != null) {
                    populatedProperties.add(col.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException(
                        "Unable to map column " + columnName + " to property " + col.getOriginName(), ex);
            }
        } else {
            if (checkColumns) {
                throw new InvalidDataAccessApiUsageException("Unable to map column '" + columnName
                        + "' to any properties of bean " + this.mappedClass.getName());
            }
            if (warnEnabled && rowNumber == 0) {
                logger.warn("Unable to map column '" + columnName + "' to any properties of bean "
                        + this.mappedClass.getName());
            }
        }
    }

    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields "
                + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties);
    }

    return mappedObject;
}

From source file:com.clican.pluto.common.support.spring.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <p>// w  w w  .j a  v  a2  s  .  co m
 * The default implementation calls
 * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}.
 * Subclasses may override this to check specific value types upfront, or to
 * post-process values return from <code>getResultSetValue</code>.
 * 
 * @param rs
 *            is the ResultSet holding the data
 * @param index
 *            is the column index
 * @param pd
 *            the bean property that each result object is expected to match
 *            (or <code>null</code> if none specified)
 * @return the Object value
 * @throws SQLException
 *             in case of extraction failure
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet,
 *      int, Class)
 */
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
    Object obj = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
    if (obj instanceof Date) {
        return new Date(((Date) obj).getTime());
    } else {
        return obj;
    }
}

From source file:com.nway.spring.jdbc.bean.JavassistBeanProcessor.java

private Object processColumn(ResultSet rs, int index, Class<?> propType, String writer, StringBuilder handler)
        throws SQLException {
    if (propType.equals(String.class)) {
        handler.append("bean.").append(writer).append("(").append("$1.getString(").append(index).append("));");
        return rs.getString(index);
    } else if (propType.equals(Integer.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getInt(").append(index).append("));");
        return rs.getInt(index);
    } else if (propType.equals(Integer.class)) {
        handler.append("bean.").append(writer).append("(").append("integerValue($1.getInt(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Integer.class);
    } else if (propType.equals(Long.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getLong(").append(index).append("));");
        return rs.getLong(index);
    } else if (propType.equals(Long.class)) {
        handler.append("bean.").append(writer).append("(").append("longValue($1.getLong(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Long.class);
    } else if (propType.equals(java.sql.Date.class)) {
        handler.append("bean.").append(writer).append("(").append("$1.getDate(").append(index).append("));");
        return rs.getDate(index);
    } else if (propType.equals(java.util.Date.class) || propType.equals(Timestamp.class)) {
        handler.append("bean.").append(writer).append("(").append("$1.getTimestamp(").append(index)
                .append("));");
        return rs.getTimestamp(index);
    } else if (propType.equals(Double.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getDouble(").append(index).append("));");
        return rs.getDouble(index);
    } else if (propType.equals(Double.class)) {
        handler.append("bean.").append(writer).append("(").append("doubleValue($1.getDouble(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Double.class);
    } else if (propType.equals(Float.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getFloat(").append(index).append("));");
        return rs.getFloat(index);
    } else if (propType.equals(Float.class)) {
        handler.append("bean.").append(writer).append("(").append("floatValue($1.getFloat(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Float.class);
    } else if (propType.equals(Time.class)) {
        handler.append("bean.").append(writer).append("(").append("$1.getTime(").append(index).append("));");
        return rs.getTime(index);
    } else if (propType.equals(Boolean.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getBoolean(").append(index).append("));");
        return rs.getBoolean(index);
    } else if (propType.equals(Boolean.class)) {
        handler.append("bean.").append(writer).append("(").append("booleanValue($1.getBoolean(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Boolean.class);
    } else if (propType.equals(byte[].class)) {
        handler.append("bean.").append(writer).append("(").append("$1.getBytes(").append(index).append("));");
        return rs.getBytes(index);
    } else if (BigDecimal.class.equals(propType)) {
        handler.append("bean.").append(writer).append("(").append("$1.getBigDecimal(").append(index)
                .append("));");
        return rs.getBigDecimal(index);
    } else if (Blob.class.equals(propType)) {
        handler.append("bean.").append(writer).append("(").append("$1.getBlob(").append(index).append("));");
        return rs.getBlob(index);
    } else if (Clob.class.equals(propType)) {
        handler.append("bean.").append(writer).append("(").append("$1.getClob(").append(index).append("));");
        return rs.getClob(index);
    } else if (propType.equals(Short.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getShort(").append(index).append("));");
        return rs.getShort(index);
    } else if (propType.equals(Short.class)) {
        handler.append("bean.").append(writer).append("(").append("shortValue($1.getShort(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Short.class);
    } else if (propType.equals(Byte.TYPE)) {
        handler.append("bean.").append(writer).append("(").append("$1.getByte(").append(index).append("));");
        return rs.getByte(index);
    } else if (propType.equals(Byte.class)) {
        handler.append("bean.").append(writer).append("(").append("byteValue($1.getByte(").append(index)
                .append("),$1.wasNull()));");
        return JdbcUtils.getResultSetValue(rs, index, Byte.class);
    } else {/*from www .  j  ava 2 s.  c  o  m*/
        handler.append("bean.").append(writer).append("(").append("(").append(propType.getName()).append(")")
                .append("$1.getObject(").append(index).append("));");
        return rs.getObject(index);
    }
}

From source file:com.dexcoder.dal.spring.mapper.JdbcRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <p>The default implementation calls
 * {@link JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)}.
 * Subclasses may override this to check specific value types upfront,
 * or to post-process values return from {@code getResultSetValue}.
 * @param rs is the ResultSet holding the data
 * @param index is the column index/*  w w w .j av  a 2s . c  o  m*/
 * @param pd the bean property that each result object is expected to match
 * (or {@code null} if none specified)
 * @return the Object value
 * @throws SQLException in case of extraction failure
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue(java.sql.ResultSet, int, Class)
 */
protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException {
    return JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType());
}

From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java

private Object processColumn(ResultSet rs, int index, Class<?> propType, String writer, String processorName,
        String beanName, MethodVisitor mv) throws SQLException {

    if (propType.equals(String.class)) {
        visitMethod(mv, index, beanName, "Ljava/lang/String;", "getString", writer);
        return rs.getString(index);
    } else if (propType.equals(Integer.TYPE)) {
        visitMethod(mv, index, beanName, "I", "getInt", writer);
        return rs.getInt(index);
    } else if (propType.equals(Integer.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_INTEGER, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Integer.class);
    } else if (propType.equals(Long.TYPE)) {
        visitMethod(mv, index, beanName, "J", "getLong", writer);
        return rs.getLong(index);
    } else if (propType.equals(Long.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_LONG, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Long.class);
    } else if (propType.equals(java.sql.Date.class)) {
        visitMethod(mv, index, beanName, "Ljava/sql/Date;", "getDate", writer);
        return rs.getDate(index);
    } else if (propType.equals(java.util.Date.class)) {
        visitMethodCast(mv, index, beanName, PROPERTY_TYPE_DATE, "java/util/Date", writer);
        return rs.getTimestamp(index);
    } else if (propType.equals(Timestamp.class)) {
        visitMethod(mv, index, beanName, "Ljava/sql/Timestamp;", "getTimestamp", writer);
        return rs.getTimestamp(index);
    } else if (propType.equals(Time.class)) {
        visitMethod(mv, index, beanName, "Ljava/sql/Time;", "getTime", writer);
        return rs.getTime(index);
    } else if (propType.equals(Double.TYPE)) {
        visitMethod(mv, index, beanName, "D", "getDouble", writer);
        return rs.getDouble(index);
    } else if (propType.equals(Double.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_DOUBLE, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Double.class);
    } else if (propType.equals(Float.TYPE)) {
        visitMethod(mv, index, beanName, "F", "getFloat", writer);
        return rs.getFloat(index);
    } else if (propType.equals(Float.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_FLOAT, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Float.class);
    } else if (propType.equals(Boolean.TYPE)) {
        visitMethod(mv, index, beanName, "Z", "getBoolean", writer);
        return rs.getBoolean(index);
    } else if (propType.equals(Boolean.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_BOOLEAN, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Boolean.class);
    } else if (propType.equals(Clob.class)) {
        visitMethod(mv, index, beanName, "Ljava/sql/Clob;", "getClob", writer);
        return rs.getClob(index);
    } else if (propType.equals(Blob.class)) {
        visitMethod(mv, index, beanName, "Ljava/sql/Blob;", "getBlob", writer);
        return rs.getBlob(index);
    } else if (propType.equals(byte[].class)) {
        visitMethod(mv, index, beanName, "[B", "getBytes", writer);
        return rs.getBytes(index);
    } else if (propType.equals(Short.TYPE)) {
        visitMethod(mv, index, beanName, "S", "getShort", writer);
        return rs.getShort(index);
    } else if (propType.equals(Short.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_SHORT, writer, processorName);
        return JdbcUtils.getResultSetValue(rs, index, Short.class);
    } else if (propType.equals(Byte.TYPE)) {
        visitMethod(mv, index, beanName, "B", "getByte", writer);
        return rs.getByte(index);
    } else if (propType.equals(Byte.class)) {
        visitMethodWrap(mv, index, beanName, PROPERTY_TYPE_BYTE, writer, processorName);
        return rs.getByte(index);
    } else {/*from w  ww .j a  v  a  2 s .co m*/
        visitMethodCast(mv, index, beanName, PROPERTY_TYPE_OTHER, propType.getName().replace('.', '/'), writer);
        return rs.getObject(index);
    }
}