Search in sources :

Example 1 with RowMapperResultSetExtractor

use of org.springframework.jdbc.core.RowMapperResultSetExtractor in project dhis2-core by dhis2.

the class JdbcUtils method generatedKeys.

private static void generatedKeys(PreparedStatement ps, KeyHolder keyHolder) throws SQLException {
    List<Map<String, Object>> keys = keyHolder.getKeyList();
    ResultSet rs = ps.getGeneratedKeys();
    if (rs == null)
        return;
    try {
        keys.addAll(new RowMapperResultSetExtractor<>(new ColumnMapRowMapper(), 1).extractData(rs));
    } finally {
        rs.close();
    }
}
Also used : ResultSet(java.sql.ResultSet) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map)

Example 2 with RowMapperResultSetExtractor

use of org.springframework.jdbc.core.RowMapperResultSetExtractor in project camel by apache.

the class DefaultSqlEndpoint method queryForObject.

@SuppressWarnings("unchecked")
public Object queryForObject(ResultSet rs) throws SQLException {
    Object result = null;
    if (outputClass == null) {
        RowMapper rowMapper = new ColumnMapRowMapper();
        RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
        List<Map<String, Object>> data = mapper.extractData(rs);
        if (data.size() > 1) {
            throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
        } else if (data.size() == 1) {
            // Set content depend on number of column from query result
            Map<String, Object> row = data.get(0);
            if (row.size() == 1) {
                result = row.values().iterator().next();
            } else {
                result = row;
            }
        }
    } else {
        Class<?> outputClzz = getCamelContext().getClassResolver().resolveClass(outputClass);
        RowMapper rowMapper = new BeanPropertyRowMapper(outputClzz);
        RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
        List<?> data = mapper.extractData(rs);
        if (data.size() > 1) {
            throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
        } else if (data.size() == 1) {
            result = data.get(0);
        }
    }
    // If data.size is zero, let result be null.
    return result;
}
Also used : ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) SQLDataException(java.sql.SQLDataException) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Example 3 with RowMapperResultSetExtractor

use of org.springframework.jdbc.core.RowMapperResultSetExtractor in project camel by apache.

the class DefaultSqlEndpoint method queryForList.

@SuppressWarnings("unchecked")
public List<?> queryForList(ResultSet rs, boolean allowMapToClass) throws SQLException {
    if (allowMapToClass && outputClass != null) {
        Class<?> outputClazz = getCamelContext().getClassResolver().resolveClass(outputClass);
        RowMapper rowMapper = new BeanPropertyRowMapper(outputClazz);
        RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
        List<?> data = mapper.extractData(rs);
        return data;
    } else {
        ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
        RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
        List<Map<String, Object>> data = mapper.extractData(rs);
        return data;
    }
}
Also used : BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Example 4 with RowMapperResultSetExtractor

use of org.springframework.jdbc.core.RowMapperResultSetExtractor in project camel by apache.

the class DrillEndpoint method queryForList.

public List<?> queryForList(ResultSet rs) throws SQLException {
    ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
    RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
    List<Map<String, Object>> data = mapper.extractData(rs);
    return data;
}
Also used : ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map)

Example 5 with RowMapperResultSetExtractor

use of org.springframework.jdbc.core.RowMapperResultSetExtractor in project Gargoyle by callakrsos.

the class DbUtil method select.

public static <T> List<T> select(final Connection con, final String sql, int fetchCount, RowMapper<T> mapper) throws Exception {
    List<T> resultList = Collections.emptyList();
    try {
        noticeQuery(sql);
        PreparedStatement prepareStatement = null;
        ResultSet executeQuery = null;
        /* 쿼리 타임아웃 시간 설정 SEC */
        // int queryTimeout = getQueryTimeout();
        prepareStatement = con.prepareStatement(sql);
        LOGGER.debug(sql);
        if (fetchCount > 0) {
            prepareStatement.setFetchSize(fetchCount);
        }
        executeQuery = prepareStatement.executeQuery();
        RowMapperResultSetExtractor<T> rowMapperResultSetExtractor = new RowMapperResultSetExtractor<T>(mapper);
        resultList = rowMapperResultSetExtractor.extractData(executeQuery);
    } catch (Throwable e) {
        throw e;
    } finally {
        close(con);
    }
    return resultList;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor)

Aggregations

RowMapperResultSetExtractor (org.springframework.jdbc.core.RowMapperResultSetExtractor)6 ColumnMapRowMapper (org.springframework.jdbc.core.ColumnMapRowMapper)5 Map (java.util.Map)4 ResultSet (java.sql.ResultSet)2 BeanPropertyRowMapper (org.springframework.jdbc.core.BeanPropertyRowMapper)2 RowMapper (org.springframework.jdbc.core.RowMapper)2 PreparedStatement (java.sql.PreparedStatement)1 SQLDataException (java.sql.SQLDataException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1