Example usage for org.springframework.jdbc.object SqlQuery setDataSource

List of usage examples for org.springframework.jdbc.object SqlQuery setDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.object SqlQuery setDataSource.

Prototype

public void setDataSource(DataSource dataSource) 

Source Link

Document

Set the JDBC DataSource to obtain connections from.

Usage

From source file:org.springframework.jdbc.object.SqlQueryTests.java

public void testQueryWithoutParams() throws SQLException {
    mockResultSet.next();/*from ww  w  .  jav a  2  s.c  o m*/
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt(1);
    ctrlResultSet.setReturnValue(1);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    SqlQuery query = new MappingSqlQueryWithParameters() {
        protected Object mapRow(ResultSet rs, int rownum, Object[] params, Map context) throws SQLException {
            assertTrue("params were null", params == null);
            assertTrue("context was null", context == null);
            return new Integer(rs.getInt(1));
        }
    };

    query.setDataSource(mockDataSource);
    query.setSql(SELECT_ID);
    query.compile();
    List list = query.execute();
    assertTrue("Found customers", list.size() != 0);
    for (Iterator itr = list.iterator(); itr.hasNext();) {
        Integer id = (Integer) itr.next();
        assertTrue("Customer id was assigned correctly", id.intValue() == 1);
    }
}