Search in sources :

Example 21 with Customer

use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testQueryWithRowMapperNoParameters.

@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS, new RowMapper<Customer>() {

        @Override
        public Customer mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertEquals(1, customers.size());
    assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
    assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

Customer (org.springframework.jdbc.Customer)21 ResultSet (java.sql.ResultSet)19 Test (org.junit.Test)18 DataSource (javax.sql.DataSource)12 SqlParameter (org.springframework.jdbc.core.SqlParameter)12 SQLException (java.sql.SQLException)7 HashMap (java.util.HashMap)6 SqlParameterValue (org.springframework.jdbc.core.SqlParameterValue)4 LinkedList (java.util.LinkedList)2 DataAccessException (org.springframework.dao.DataAccessException)2 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)2 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1