use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testNamedParameterUsingInvalidQuestionMarkPlaceHolders.
@Test
public void testNamedParameterUsingInvalidQuestionMarkPlaceHolders() throws SQLException {
given(connection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_REUSED_1, 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_ID_REUSED_1);
setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
declareParameter(new SqlParameter("id1", 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 List<Customer> findCustomers(Integer id1) {
Map<String, Integer> params = new HashMap<>();
params.put("id1", id1);
return executeByNamedParam(params);
}
}
CustomerQuery query = new CustomerQuery(dataSource);
thrown.expect(InvalidDataAccessApiUsageException.class);
query.findCustomers(1);
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testUnnamedParameterDeclarationWithNamedParameterQuery.
@Test
public void testUnnamedParameterDeclarationWithNamedParameterQuery() throws SQLException {
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) {
Map<String, Integer> params = new HashMap<>();
params.put("id", id);
return executeByNamedParam(params).get(0);
}
}
// Query should not succeed since parameter declaration did not specify parameter name
CustomerQuery query = new CustomerQuery(dataSource);
thrown.expect(InvalidDataAccessApiUsageException.class);
query.findCustomer(1);
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractor.
@Test
public void testQueryWithResultSetExtractor() throws SQLException {
given(resultSet.next()).willReturn(true);
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.query(SELECT_NAMED_PARAMETERS, params, new ResultSetExtractor<Customer>() {
@Override
public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
rs.next();
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();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractorNoParameters.
@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS, new ResultSetExtractor<Customer>() {
@Override
public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
rs.next();
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_NO_PARAMETERS);
verify(preparedStatement).close();
verify(connection).close();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class NamedParameterJdbcTemplateTests method testQueryWithRowCallbackHandler.
@Test
public void testQueryWithRowCallbackHandler() 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");
final List<Customer> customers = new LinkedList<>();
namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, 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_NAMED_PARAMETERS_PARSED);
verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
verify(preparedStatement).setString(2, "UK");
verify(preparedStatement).close();
verify(connection).close();
}
Aggregations