List of usage examples for org.springframework.jdbc.support.rowset SqlRowSet findColumn
int findColumn(String columnLabel) throws InvalidResultSetAccessException;
From source file:com.google.api.ads.adwords.awalerting.sampleimpl.downloader.SqlDbReportDownloader.java
@Override public List<ReportData> downloadReports(ImmutableAdWordsSession protoSession, Set<Long> clientCustomerIds) { Map<Long, ReportData> reportDataMap = new HashMap<Long, ReportData>(); JdbcTemplate jdbcTemplate = getJdbcTemplate(); String sqlQuery = getSqlQueryWithReportColumnNames(); ReportDefinitionReportType reportType = getReportType(); SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sqlQuery); // Get the column index of customer id. int customerIdColumnIndex = rowSet.findColumn(EXTERNAL_CUSTOMER_ID_REPORT_COLUMN_NAME); Preconditions.checkState(customerIdColumnIndex >= 0, "You must choose \"%s\" field to generate report data", EXTERNAL_CUSTOMER_ID_REPORT_COLUMN_NAME); List<String> columnNames = Arrays.asList(rowSet.getMetaData().getColumnNames()); int columns = columnNames.size(); // Read result into map. int rows = 0; while (rowSet.next()) { rows++;/*from w w w . j a v a 2 s . c om*/ List<String> row = new ArrayList<String>(columns); for (int i = 0; i < columns; i++) { row.add(rowSet.getString(i)); } String customerIdStr = row.get(customerIdColumnIndex); Long customerId = Long.parseLong(customerIdStr); ReportData reportData = reportDataMap.get(customerId); if (reportData == null) { reportData = new ReportData(customerId, reportType, columnNames); reportDataMap.put(customerId, reportData); } reportData.addRow(row); } LOGGER.info("Retrieved and parsed {} rows from database.", rows); return new ArrayList<ReportData>(reportDataMap.values()); }