Example usage for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler

List of usage examples for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler

Introduction

In this page you can find the example usage for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler.

Prototype

RowCountCallbackHandler

Source Link

Usage

From source file:io.github.benas.jql.shell.StringResultSetExtractor.java

@Override
public String extractData(ResultSet resultSet) throws SQLException, DataAccessException {
    RowCountCallbackHandler rowCountCallbackHandler = new RowCountCallbackHandler();
    rowCountCallbackHandler.processRow(resultSet);
    int columnCount = resultSet.getMetaData().getColumnCount();
    List<String> columnNames = asList(rowCountCallbackHandler.getColumnNames());
    String header = getHeader(columnNames);

    StringBuilder result = new StringBuilder(header);
    result.append("\n");

    while (resultSet.next()) {
        StringBuilder stringBuilder = new StringBuilder();
        int i = 1;
        while (i <= columnCount) {
            stringBuilder.append(resultSet.getObject(i));
            if (i < columnCount) {
                stringBuilder.append(" | ");
            }//w w  w  .jav a 2  s  .  c o m
            i++;
        }
        result.append(stringBuilder.toString()).append("\n");
    }

    return result.toString();
}

From source file:io.github.benas.jql.core.QueryExecutor.java

public int count(String countQuery) {
    RowCountCallbackHandler rowCountCallbackHandler = new RowCountCallbackHandler();
    jdbcTemplate.query(countQuery, rowCountCallbackHandler);
    return rowCountCallbackHandler.getRowCount();
}

From source file:gov.nih.nci.cabig.ctms.tools.configuration.DatabaseBackedConfigurationTest.java

private <V> void assertNotSet(ConfigurationProperty<V> property) {
    RowCountCallbackHandler counter = new RowCountCallbackHandler();
    jdbc.query(String.format("SELECT * FROM %s WHERE key=?", DEFAULT_TABLE), new Object[] { property.getKey() },
            counter);/* ww w.j a v a 2  s .co m*/
    assertEquals(property + " is set", 0, counter.getRowCount());
}

From source file:org.opennms.core.test.db.TemporaryDatabase.java

public int countRows(String sql, Object... values) {
    RowCountCallbackHandler counter = new RowCountCallbackHandler();
    getJdbcTemplate().getJdbcOperations().query(sql, values, counter);
    return counter.getRowCount();
}

From source file:org.opennms.core.test.db.TemporaryDatabasePostgreSQL.java

public int countRows(String sql, Object... values) {
    RowCountCallbackHandler counter = new RowCountCallbackHandler();
    getJdbcTemplate().query(sql, values, counter);
    return counter.getRowCount();
}

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

public void testLeaveConnectionOpenOnRequest() throws Exception {
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    ctrlResultSet = MockControl.createControl(ResultSet.class);
    mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();/*from ww  w . j  a va  2s. c om*/
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    ctrlStatement = MockControl.createControl(PreparedStatement.class);
    mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.isClosed();
    ctrlConnection.setReturnValue(false, 2);
    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);
    // if close is called entire test will fail
    mockConnection.close();
    ctrlConnection.setDefaultThrowable(new RuntimeException());

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    SingleConnectionDataSource scf = new SingleConnectionDataSource(mockDataSource.getConnection(), false);
    JdbcTemplate template2 = new JdbcTemplate(scf, false);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    template2.query(sql, rcch);

    ctrlResultSet.verify();
    ctrlStatement.verify();
}

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

public void testCloseConnectionOnRequest() throws Exception {
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();/*from   www . j  a  v a  2s.c  o m*/
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    template.query(sql, rcch);

    ctrlResultSet.verify();
    ctrlStatement.verify();
}

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

public void testCouldntGetConnectionOrExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();/*  www  .j  a v a 2 s .  com*/
    // Expect two calls (one call after caching data product name): make get metadata fail also
    ctrlDataSource.setThrowable(sex, 2);
    replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}

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

public void testCouldntGetConnectionForOperationOrExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    // Change behavior in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();//from   w  w w .  ja  v a 2s  .c  o m
    // Expect two calls (one call after caching data product name): make get Metadata fail also
    ctrlDataSource.setThrowable(sex, 2);
    replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}

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

public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();/*from ww  w.j  av  a  2 s .c  om*/
    ctrlDataSource.setThrowable(sex, 1);
    replay();

    try {
        JdbcTemplate template2 = new JdbcTemplate();
        template2.setDataSource(mockDataSource);
        template2.afterPropertiesSet();
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}