Search in sources :

Example 16 with Customer

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);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 17 with Customer

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);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 18 with Customer

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();
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 19 with Customer

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();
}
Also used : Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 20 with Customer

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

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