Example usage for org.springframework.jdbc.core JdbcTemplate query

List of usage examples for org.springframework.jdbc.core JdbcTemplate query

Introduction

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

Prototype

@Override
    public <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper) throws DataAccessException 

Source Link

Usage

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();// w w  w. ja v a 2 s. c  om
    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

/**
 * Test that we see a runtime exception come back.
 *//* w w w  .ja v  a  2s . co m*/
public void testExceptionComesBack() throws Exception {
    final String sql = "SELECT ID FROM CUSTMR";
    final RuntimeException rex = new RuntimeException("What I want to see");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    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);
    try {
        template.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) {
                throw rex;
            }
        });
        fail("Should have thrown exception");
    } catch (RuntimeException ex) {
        assertTrue("Wanted same exception back, not " + ex, ex == rex);
    }
}

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();//from  ww w .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 ww.jav  a2s  . 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 a v  a  2s.  com*/
    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();
}

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

/**
 * Verify that afterPropertiesSet invokes exception translator.
 *///  w w w  .j  a  va2s.  c  o m
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitialized() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour 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();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.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();
    ctrlConnection.verify();
}

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

/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet()./*from   w  ww.ja va 2  s .c  o  m*/
 */
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
        throws SQLException {

    SQLException sex = new SQLException("foo", "07xxx");

    ctrlConnection = MockControl.createControl(Connection.class);
    mockConnection = (Connection) ctrlConnection.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour 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();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.replay();

    try {
        JdbcTemplate template2 = new JdbcTemplate();
        template2.setDataSource(mockDataSource);
        template2.setLazyInit(false);
        if (beanProperty) {
            // This will get a connection.
            template2.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(mockDataSource));
        } else {
            // This will cause creation of default SQL translator.
            // Note that only call should be effective.
            template2.afterPropertiesSet();
            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();
    ctrlConnection.verify();
}

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

public void testCouldntClose() throws Exception {
    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockConnection.createStatement();/*from  www .j  av  a2s . co m*/
    ctrlConnection.setReturnValue(mockStatement);
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    SQLException sex = new SQLException("bar");
    mockResultSet.close();
    ctrlResultSet.setThrowable(sex);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setThrowable(sex);
    mockConnection.close();
    ctrlConnection.setThrowable(sex);

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

    JdbcTemplate template2 = new JdbcTemplate(mockDataSource);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);

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

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

/**
 * Mock objects allow us to produce warnings at will
 *//*from www.j  av  a  2 s .c o m*/
public void testFatalWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(warnings);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

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

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

    JdbcTemplate t = new JdbcTemplate(mockDataSource);
    t.setIgnoreWarnings(false);
    try {
        t.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                rs.getByte(1);
            }
        });
        fail("Should have thrown exception on warning");
    } catch (SQLWarningException ex) {
        // Pass
        assertTrue("Root cause of warning was correct", ex.getCause() == warnings);
    }

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

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

public void testIgnoredWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();/*from  w  w w .j a v  a  2 s . co 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();

    // Too long: truncation
    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    template.setIgnoreWarnings(true);
    template.query(sql, new RowCallbackHandler() {
        public void processRow(ResultSet rs) throws java.sql.SQLException {
            rs.getByte(1);
        }
    });

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