Example usage for org.springframework.jdbc.support.rowset SqlRowSet next

List of usage examples for org.springframework.jdbc.support.rowset SqlRowSet next

Introduction

In this page you can find the example usage for org.springframework.jdbc.support.rowset SqlRowSet next.

Prototype

boolean next() throws InvalidResultSetAccessException;

Source Link

Document

Move the cursor to the next row.

Usage

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++;/*w ww  .  j  a v  a  2  s . c o  m*/
        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());
}

From source file:com.emc.ecs.sync.EndToEndTest.java

protected void verifyDb(TestObjectSource testSource, boolean truncateDb) {
    SingleConnectionDataSource ds = new SingleConnectionDataSource();
    ds.setUrl(SqliteDbService.JDBC_URL_BASE + dbFile.getPath());
    ds.setSuppressClose(true);/*from  w w  w  .ja  va  2  s.  c o m*/
    JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);

    long totalCount = verifyDbObjects(jdbcTemplate, testSource.getObjects());
    try {
        SqlRowSet rowSet = jdbcTemplate.queryForRowSet("SELECT count(source_id) FROM "
                + DbService.DEFAULT_OBJECTS_TABLE_NAME + " WHERE target_id != ''");
        Assert.assertTrue(rowSet.next());
        Assert.assertEquals(totalCount, rowSet.getLong(1));
        if (truncateDb)
            jdbcTemplate.update("DELETE FROM " + DbService.DEFAULT_OBJECTS_TABLE_NAME);
    } finally {
        try {
            ds.destroy();
        } catch (Throwable t) {
            log.warn("could not close datasource", t);
        }
    }
}

From source file:net.freechoice.dao.impl.DaoUser.java

@Deprecated
@Override/*  ww  w.j  ava  2s.c  o  m*/
public Result getPasswordOfUser(String nameOrEmail) {

    SqlRowSet rowSet;
    if (nameOrEmail.contains("@")) {
        rowSet = getJdbcTemplate().queryForRowSet("select id, password from fc_user "
                + "where is_valid = true and email = " + quote(nameOrEmail));
    } else {
        rowSet = getJdbcTemplate().queryForRowSet("select id, password from fc_user "
                + "where is_valid = true and name_login = " + quote(nameOrEmail));
    }

    if (rowSet.next()) {
        if (rowSet.isLast()) {
            Result result = new Result();
            result.id = rowSet.getInt(1);
            result.password = rowSet.getString(2);
            return result;
        } else {
            throw new RuntimeException("multiple user found, should be one only");
        }
    } else {
        return null;
    }
}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Called when needing a count of distincts or other numerical result.
 * Returns an Big Integer so only an integer should be used
 * //  w  w  w.j a va  2s  . c om
 * @param sql
 * @param columns
 * @return
 */
public BigDecimal queryForBigInt(String sql) {
    SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

    if (rs.next()) {
        if (rs.getMetaData().getColumnNames().length > 0) {
            return rs.getBigDecimal(1);
        }
    }
    return new BigDecimal(0);
}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Called when needing a count of distincts or other numerical result.
 * Returns an Integer so only an integer should be used
 * //  w  w w.j av  a2s  . c  om
 * @param sql
 * @param columns
 * @return
 */
public int queryForInt(String sql) {
    SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

    if (rs.next()) {
        if (rs.getMetaData().getColumnNames().length > 0) {
            return rs.getInt(1);
        }
    }
    return 0;

}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Called when needing a count of distincts or other numerical result.
 * Returns an Integer so only an integer should be used
 * /* w  ww .j av a 2 s  . com*/
 * @param sql
 * @param columns
 * @return
 */
public double queryForDoubleString(String sql) {
    SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

    if (rs.next()) {
        if (rs.getMetaData().getColumnNames().length > 0) {
            return rs.getDouble(1);
        }
    }
    return 0;

}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Called when needing a count of distincts or other numerical result.
 * Returns a Float so only an integer should be used
 * /*from   ww  w.ja va  2s.c o m*/
 * @param sql
 * @param columns
 * @return
 */
public float queryForFloat(String sql) {
    SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

    if (rs.next()) {
        if (rs.getMetaData().getColumnNames().length > 0) {

            return rs.getFloat(1);
        }
    }
    return 0f;

}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Checks for a table schema//from  w  w w . ja  v  a 2 s.c o  m
 * 
 * @param schema
 * @return
 */
public boolean checkSchema(String schema) {
    String sql = "SELECT count(schema_name) FROM information_schema.schemata WHERE schema_name='" + schema
            + "'";

    SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

    if (rs.next()) {
        if (rs.getInt(1) > 0) {
            return true;
        }
    }

    return false;
}

From source file:com.emc.ecs.sync.EndToEndTest.java

protected long verifyDbObjects(JdbcTemplate jdbcTemplate, List<TestSyncObject> objects) {
    Date now = new Date();
    long count = 0;
    for (TestSyncObject object : objects) {
        count++;/*from   w  w  w . j  ava 2 s . co m*/
        SqlRowSet rowSet = jdbcTemplate.queryForRowSet(
                "SELECT * FROM " + DbService.DEFAULT_OBJECTS_TABLE_NAME + " WHERE target_id=?",
                object.getSourceIdentifier());
        Assert.assertTrue(rowSet.next());
        Assert.assertEquals(object.getSourceIdentifier(), rowSet.getString("target_id"));
        Assert.assertEquals(object.isDirectory(), rowSet.getBoolean("is_directory"));
        Assert.assertEquals(object.getMetadata().getContentLength(), rowSet.getLong("size"));
        // mtime in the DB is actually pulled from the target system, so we don't know what precision it will be in
        // or if the target system's clock is in sync, but let's assume it will always be within 5 minutes
        Assert.assertTrue(
                Math.abs(object.getMetadata().getModificationTime().getTime() - rowSet.getLong("mtime")) < 5
                        * 60 * 1000);
        Assert.assertEquals(ObjectStatus.Verified.getValue(), rowSet.getString("status"));
        Assert.assertTrue(now.getTime() - rowSet.getLong("transfer_start") < 10 * 60 * 1000); // less than 10 minutes ago
        Assert.assertTrue(now.getTime() - rowSet.getLong("transfer_complete") < 10 * 60 * 1000); // less than 10 minutes ago
        Assert.assertTrue(now.getTime() - rowSet.getLong("verify_start") < 10 * 60 * 1000); // less than 10 minutes ago
        Assert.assertTrue(now.getTime() - rowSet.getLong("verify_complete") < 10 * 60 * 1000); // less than 10 minutes ago
        Assert.assertEquals(object.getFailureCount(), rowSet.getInt("retry_count"));
        if (object.getFailureCount() > 0) {
            String error = rowSet.getString("error_message");
            Assert.assertNotNull(error);
            log.warn("{} was retried {} time{}; error: {}", object.getRelativePath(), object.getFailureCount(),
                    object.getFailureCount() > 1 ? "s" : "", error);
        }
        if (object.isDirectory())
            count += verifyDbObjects(jdbcTemplate, object.getChildren());
    }
    return count;
}

From source file:com.hygenics.parser.getDAOTemplate.java

/**
 * Check Table//from ww  w. j  ava 2 s  .com
 */

public boolean checkTable(String table, String schema) {
    String[] table_split = table.split("\\.");

    if (table_split.length > 0) {

        String sql = "SELECT count(table_name) FROM information_schema.tables WHERE table_name='"
                + table_split[1] + "' AND table_schema='" + schema + "'";
        log.info(sql);
        SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql);

        if (rs.next()) {
            if (rs.getInt(1) > 0) {
                return true;
            }
        }

    }
    return false;
}