Example usage for org.springframework.jdbc Customer setForename

List of usage examples for org.springframework.jdbc Customer setForename

Introduction

In this page you can find the example usage for org.springframework.jdbc Customer setForename.

Prototype

public void setForename(String forename) 

Source Link

Usage

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

public void testFancyCustomerQuery() throws SQLException {
    mockResultSet.next();//  www  .ja  v a 2  s.  c  om
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerQuery extends MappingSqlQuery {

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

        protected Object 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 (Customer) findObject(id);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    Customer cust = query.findCustomer(1);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
}

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

public void testUnnamedParameterDeclarationWithNamedParameterQuery() throws SQLException {
    replay();//from   w  ww  .  j a va2 s.com

    class CustomerQuery extends MappingSqlQuery {

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

        protected Object 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 params = new HashMap();
            params.put("id", new Integer(id));
            return (Customer) executeByNamedParam(params).get(0);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    try {
        Customer cust = query.findCustomer(1);
        fail("Query should not succeed since parameter declaration did not specify parameter name");
    } catch (InvalidDataAccessApiUsageException ex) {
        // OK - it worked
    }
}

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

private void doTestNamedParameterCustomerQuery(final boolean namedDeclarations) throws SQLException {
    mockResultSet.next();//w w  w .  ja  v a  2  s  .c om
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setString(2, "UK");
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerQuery extends MappingSqlQuery {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_NAMED_PARAMETERS);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            if (namedDeclarations) {
                declareParameter(new SqlParameter("country", Types.VARCHAR));
                declareParameter(new SqlParameter("id", Types.NUMERIC));
            } else {
                declareParameter(new SqlParameter(Types.NUMERIC));
                declareParameter(new SqlParameter(Types.VARCHAR));
            }
            compile();
        }

        protected Object 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, String country) {
            Map params = new HashMap();
            params.put("id", new Integer(id));
            params.put("country", country);
            return (Customer) executeByNamedParam(params).get(0);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    Customer cust = query.findCustomer(1, "UK");
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
}

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

public void testNamedParameterInListQuery() throws SQLException {
    mockResultSet.next();//from w w w.j  av a2s  .  c o  m
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(2);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("juergen");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(2), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_IN_LIST_1, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerQuery extends MappingSqlQuery {

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

        protected Object 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 findCustomers(List ids) {
            Map params = new HashMap();
            params.put("ids", ids);
            return (List) executeByNamedParam(params);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    List ids = new ArrayList();
    ids.add(new Integer(1));
    ids.add(new Integer(2));
    List cust = query.findCustomers(ids);

    assertEquals("We got two customers back", cust.size(), 2);
    Assert.assertEquals("First customer id was assigned correctly", ((Customer) cust.get(0)).getId(), 1);
    Assert.assertEquals("First customer forename was assigned correctly",
            ((Customer) cust.get(0)).getForename(), "rod");
    Assert.assertEquals("Second customer id was assigned correctly", ((Customer) cust.get(1)).getId(), 2);
    Assert.assertEquals("Second customer forename was assigned correctly",
            ((Customer) cust.get(1)).getForename(), "juergen");
}

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

public void testNamedParameterQueryReusingParameter() throws SQLException {
    mockResultSet.next();/*  w w w  .  j  a va2  s . c om*/
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(2);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("juergen");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(1), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_REUSED_1, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerQuery extends MappingSqlQuery {

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

        protected Object 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 findCustomers(Integer id) {
            Map params = new HashMap();
            params.put("id1", id);
            return (List) executeByNamedParam(params);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    List cust = query.findCustomers(new Integer(1));

    assertEquals("We got two customers back", cust.size(), 2);
    Assert.assertEquals("First customer id was assigned correctly", ((Customer) cust.get(0)).getId(), 1);
    Assert.assertEquals("First customer forename was assigned correctly",
            ((Customer) cust.get(0)).getForename(), "rod");
    Assert.assertEquals("Second customer id was assigned correctly", ((Customer) cust.get(1)).getId(), 2);
    Assert.assertEquals("Second customer forename was assigned correctly",
            ((Customer) cust.get(1)).getForename(), "juergen");
}

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

public void testNamedParameterUsingInvalidQuestionMarkPlaceHolders() throws SQLException {

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_REUSED_1, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();//from   w w w.j  a  v  a2 s  .  c o m

    class CustomerQuery extends MappingSqlQuery {

        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();
        }

        protected Object 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 findCustomers(Integer id1) {
            Map params = new HashMap();
            params.put("id1", id1);
            return (List) executeByNamedParam(params);
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);
    try {
        List cust = query.findCustomers(new Integer(1));
        fail("Should have caused an InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException e) {
    }

}