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) throws SQLException 

Source Link

Document

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

Usage

From source file:io.pivotal.dataflow.task.app.jdbcgemfire.common.JdbcColumnToPojoRowMapper.java

@Override
public Map<String, Object> mapRow(ResultSet rs, int arg1) throws SQLException {
    Map<String, Object> builder = new HashMap<>();
    Map<String, Object> processed = new HashMap<>();
    ColumnStandardMapper mapper = new ColumnStandardMapper();

    ResultSetMetaData metaData = rs.getMetaData();
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
        builder.put(JdbcUtils.lookupColumnName(metaData, i), JdbcUtils.getResultSetValue(rs, i));
    }/*from   ww  w.ja va2  s . c om*/
    // send map to groovyColumnProcessor.process()
    processed = mapper.process(builder);

    return processed;
}

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

@Override
public void processRow(ResultSet rs) throws SQLException {

    if (rsmd == null) {
        rsmd = rs.getMetaData();//from w w w .  ja v a  2  s.  c om
    }

    if (format != null) {
        Object args[] = new Object[rsmd.getColumnCount()];
        for (int index = 1; index <= rsmd.getColumnCount(); index++) {
            Object obj = JdbcUtils.getResultSetValue(rs, index);
            args[index - 1] = obj;
        }
        writer.println(String.format(format, args));
    } else {
        StringBuilder sb = new StringBuilder();
        for (int index = 1; index <= rsmd.getColumnCount(); index++) {
            Object obj = JdbcUtils.getResultSetValue(rs, index);
            if (index > 1) {
                sb.append(", ");
            }
            sb.append(JdbcUtils.lookupColumnName(rsmd, index));
            sb.append("=");
            sb.append(obj);
        }
        writer.println(sb.toString());
    }
    writer.flush();
}

From source file:org.apereo.services.persondir.support.jdbc.ColumnMapParameterizedRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <br>/*from   w ww  .j ava  2s . c o m*/
 *
 * The default implementation uses the <code>getObject</code> method. Additionally, this implementation includes
 * a "hack" to get around Oracle returning a non standard object for their TIMESTAMP datatype.
 *
 * @param rs is the ResultSet holding the data
 * @param index is the column index
 * @throws SQLException SQL Exception
 * @return the Object returned
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue
 */
protected Object getColumnValue(final ResultSet rs, final int index) throws SQLException {
    return JdbcUtils.getResultSetValue(rs, index);
}

From source file:org.jasig.services.persondir.support.jdbc.ColumnMapParameterizedRowMapper.java

/**
 * Retrieve a JDBC object value for the specified column.
 * <br/>//  w  ww.j  a  v a2s.c o m
 * 
 * The default implementation uses the <code>getObject</code> method. Additionally, this implementation includes
 * a "hack" to get around Oracle returning a non standard object for their TIMESTAMP datatype.
 * 
 * @param rs is the ResultSet holding the data
 * @param index is the column index
 * @return the Object returned
 * @see org.springframework.jdbc.support.JdbcUtils#getResultSetValue
 */
protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
    return JdbcUtils.getResultSetValue(rs, index);
}

From source file:architecture.common.adaptor.connector.jdbc.AbstractJdbcConnector.java

protected List<Map<String, Object>> pull(final String queryString,
        final List<ParameterMapping> parameterMappings, final Object[] args) {

    // log.debug("pulling..." );

    return getJdbcTemplate().query(queryString, args, new RowMapper<Map<String, Object>>() {
        public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            Map<String, Object> mapOfColValues = createColumnMap(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                String key = getColumnKey(lookupColumnName(rsmd, i));
                Object obj = getColumnValue(rs, i);
                mapOfColValues.put(key, obj);
            }/*  w  ww  .ja  v  a 2 s  . c  o m*/

            return mapOfColValues;
        }

        protected Map<String, Object> createColumnMap(int columnCount) {
            return new LinkedHashMap<String, Object>(columnCount);
        }

        protected String getColumnKey(String columnName) {
            return columnName;
        }

        protected Object getColumnValue(ResultSet rs, int index) throws SQLException {

            for (ParameterMapping mapping : parameterMappings) {
                // LOG.debug( index + " mapping match :" +
                // mapping.getIndex());
                if (index == mapping.getIndex()) {
                    if (String.class == mapping.getJavaType()) {

                        String value = rs.getString(index);
                        if (StringUtils.isEmpty(value))
                            value = "";

                        if (!StringUtils.isEmpty(mapping.getCipher())) {
                            try {
                                Cipher cipher = Cipher.getInstance(mapping.getCipher());
                                SecretKeySpec skeySpec = new SecretKeySpec(
                                        Hex.decodeHex(mapping.getCipherKey().toCharArray()),
                                        mapping.getCipherKeyAlg());
                                cipher.init(Cipher.DECRYPT_MODE, skeySpec);

                                byte raw[];
                                if (!StringUtils.isEmpty(mapping.getEncoding())) {
                                    String enc = mapping.getEncoding();
                                    if (enc.toUpperCase().equals("BASE64")) {
                                        raw = Base64.decodeBase64(value);
                                        // BASE64Decoder decoder = new
                                        // BASE64Decoder();
                                        // raw =
                                        // decoder.decodeBuffer(value);
                                    } else {
                                        raw = value.getBytes();
                                    }
                                } else {
                                    raw = value.getBytes();
                                }
                                byte stringBytes[] = cipher.doFinal(raw);
                                return new String(stringBytes);

                            } catch (Exception e) {
                                LOG.error(e);
                            }
                            return value;
                        }

                        if (!StringUtils.isEmpty(mapping.getEncoding())) {
                            String[] encoding = StringUtils.split(mapping.getEncoding(), ">");
                            try {
                                if (encoding.length == 2)
                                    return new String(value.getBytes(encoding[0]), encoding[1]);
                                else if (encoding.length == 1) {
                                    return new String(value.getBytes(), encoding[0]);
                                }
                            } catch (UnsupportedEncodingException e) {
                                LOG.error(e);
                                return value;
                            }
                        }

                    } else if (Long.class == mapping.getJavaType()) {
                        String value = rs.getString(index);
                        if (StringUtils.isEmpty(value))
                            value = "0";
                        return new Long(value);
                    } else if (Integer.class == mapping.getJavaType()) {
                        String value = rs.getString(index);
                        if (StringUtils.isEmpty(value))
                            value = "0";
                        return new Integer(value);
                    } else if (Double.class == mapping.getJavaType()) {
                        String value = rs.getString(index);
                        if (StringUtils.isEmpty(value))
                            value = "0";
                        return new Double(value);
                    }
                }
            }
            return JdbcUtils.getResultSetValue(rs, index);
        }
    });
}

From source file:org.springframework.jdbc.core.AbstractBeanPropertyRowMapper.java

protected Object doMapRow(ResultSet rs, int rowNumber) throws SQLException {
    if (getMappedClass() == null)
        throw new InvalidDataAccessApiUsageException("Target class was not specified - it is mandatory");
    Object result;//from  www. j  av a2 s .c  o  m
    try {
        result = this.defaultConstruct.newInstance((Object[]) null);
    } catch (IllegalAccessException e) {
        throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e);
    } catch (InvocationTargetException e) {
        throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e);
    } catch (InstantiationException e) {
        throw new DataAccessResourceFailureException("Failed to load class " + this.mappedClass.getName(), e);
    }
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    for (int i = 1; i <= columns; i++) {
        String column = JdbcUtils.lookupColumnName(rsmd, i).toLowerCase();
        PersistentField fieldMeta = (PersistentField) this.mappedFields.get(column);
        if (fieldMeta != null) {
            BeanWrapper bw = new BeanWrapperImpl(mappedClass);
            bw.setWrappedInstance(result);
            fieldMeta.setSqlType(rsmd.getColumnType(i));
            Object value = null;
            Class fieldType = fieldMeta.getJavaType();
            if (fieldType.equals(String.class)) {
                value = rs.getString(column);
            } else if (fieldType.equals(byte.class) || fieldType.equals(Byte.class)) {
                value = new Byte(rs.getByte(column));
            } else if (fieldType.equals(short.class) || fieldType.equals(Short.class)) {
                value = new Short(rs.getShort(column));
            } else if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
                value = new Integer(rs.getInt(column));
            } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
                value = new Long(rs.getLong(column));
            } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) {
                value = new Float(rs.getFloat(column));
            } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) {
                value = new Double(rs.getDouble(column));
            } else if (fieldType.equals(BigDecimal.class)) {
                value = rs.getBigDecimal(column);
            } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
                value = (rs.getBoolean(column)) ? Boolean.TRUE : Boolean.FALSE;
            } else if (fieldType.equals(java.util.Date.class) || fieldType.equals(java.sql.Timestamp.class)
                    || fieldType.equals(java.sql.Time.class) || fieldType.equals(Number.class)) {
                value = JdbcUtils.getResultSetValue(rs, rs.findColumn(column));
            }
            if (value != null) {
                if (bw.isWritableProperty(fieldMeta.getFieldName())) {
                    try {
                        if (logger.isDebugEnabled() && rowNumber == 0) {
                            logger.debug("Mapping column named \"" + column + "\""
                                    + " containing values of SQL type " + fieldMeta.getSqlType()
                                    + " to property \"" + fieldMeta.getFieldName() + "\"" + " of type "
                                    + fieldMeta.getJavaType());
                        }
                        bw.setPropertyValue(fieldMeta.getFieldName(), value);
                    } catch (NotWritablePropertyException ex) {
                        throw new DataRetrievalFailureException(
                                "Unable to map column " + column + " to property " + fieldMeta.getFieldName(),
                                ex);
                    }
                } else {
                    if (rowNumber == 0) {
                        logger.warn("Unable to access the setter for " + fieldMeta.getFieldName()
                                + ".  Check that " + "set" + StringUtils.capitalize(fieldMeta.getFieldName())
                                + " is declared and has public access.");
                    }
                }
            }
        }
    }
    return result;
}