Search in sources :

Example 1 with Customer

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

the class NamedParameterJdbcTemplateTests method testQueryForObjectWithRowMapper.

@Test
public void testQueryForObjectWithRowMapper() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params, 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;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 2 with Customer

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

the class NamedParameterJdbcTemplateTests method testQueryWithRowMapper.

@Test
public void testQueryWithRowMapper() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
    params.put("country", "UK");
    List<Customer> customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, 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_NAMED_PARAMETERS_PARSED);
    verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
    verify(preparedStatement).setString(2, "UK");
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 3 with Customer

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

the class NamedParameterJdbcTemplateTests method testQueryWithRowCallbackHandlerNoParameters.

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

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            customers.add(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) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with Customer

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

the class CustomerMapper method mapRow.

@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;
}
Also used : Customer(org.springframework.jdbc.Customer)

Example 5 with Customer

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

the class SqlQueryTests method testFancyCustomerQuery.

@Test
public void testFancyCustomerQuery() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    given(connection.prepareStatement(SELECT_ID_FORENAME_WHERE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)).willReturn(preparedStatement);
    class CustomerQuery extends MappingSqlQuery<Customer> {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        @Override
        protected 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;
        }

        public Customer findCustomer(int id) {
            return findObject(id);
        }
    }
    CustomerQuery query = new CustomerQuery(dataSource);
    Customer cust = query.findCustomer(1);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
    verify(resultSet).close();
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) 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