@Test public void testFindByLastname() throws Exception { DataSource datasource = createMock(DataSource.class); Connection connection = createMock(Connection.class); expect(datasource.getConnection()).andReturn(connection); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = createMock(PreparedStatement.class); expect(connection.prepareStatement(sql)).andReturn(stmt); stmt.setString(1, "Smith"); MockMultiRowResultSet resultset = new MockMultiRowResultSet(); String[] columns = new String[] { "first_name", "last_name" }; resultset.setupColumnNames(columns); List<Person> smiths = createListOfPeopleWithLastname("Smith"); resultset.setupRows(asResultSetArray(smiths)); expect(stmt.executeQuery()).andReturn(resultset); resultset.setExpectedCloseCalls(1); stmt.close(); connection.close(); replay(datasource, connection, stmt); JdbcPersonDao dao = new JdbcPersonDao(); dao.setDatasource(datasource); List<Person> people = dao.findByLastname("Smith"); assertEquals(smiths, people); verify(datasource, connection, stmt); resultset.verify(); }
public List<Person> findByLastname(String lastname) { try { Connection conn = datasource.getConnection(); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, lastname); ResultSet rset = stmt.executeQuery(); List<Person> people = new ArrayList<Person>(); while (rset.next()) { String firstName = rset.getString("first_name"); String lastName = rset.getString("last_name"); people.add(new Person(firstName, lastName)); } rset.close(); stmt.close(); conn.close(); return people; } catch (SQLException e) { throw new RuntimeException(e); } }
JdbcPersonDao implements PersonDao { public List<Person> findByLastname(String lastname) { try { Connection conn = datasource.getConnection(); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, lastname); ResultSet rset = stmt.executeQuery(); List<Person> people = new ArrayList<Person>(); while (rset.next()) { String firstName = rset.getString("first_name"); String lastName = rset.getString("last_name"); people.add(new Person(firstName, lastName)); } rset.close(); stmt.close(); conn.close(); return people; } catch (SQLException e) { throw new RuntimeException(e); } } }
JdbcPersonDao implements PersonDao { public List<Person> findByLastname(String lastname) { try { Connection conn = datasource.getConnection(); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, lastname); ResultSet rset = stmt.executeQuery(); List<Person> people = new ArrayList<Person>(); while (rset.next()) { String firstName = rset.getString("first_name"); String lastName = rset.getString("last_name"); people.add(new Person(firstName, lastName)); } rset.close(); stmt.close(); conn.close(); return people; } catch (SQLException e) { throw new RuntimeException(e); } }  }
JdbcPersonDao implements PersonDao { public List<Person> findByLastname(String lastname) { try { Connection conn = datasource.getConnection(); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, lastname); ResultSet rset = stmt.executeQuery(); List<Person> people = new ArrayList<Person>(); while (rset.next()) { String firstName = rset.getString("first_name"); String lastName = rset.getString("last_name"); people.add(new Person(firstName, lastName)); } rset.close(); stmt.close(); conn.close(); return people; } catch (SQLException e) { throw new RuntimeException(e); } }  void setDatasource(DataSource datasource); List<Person> findByLastname(String lastname); void save(Person person); Person find(Long id); List<Person> findAll(); }
JdbcPersonDao implements PersonDao { public List<Person> findByLastname(String lastname) { try { Connection conn = datasource.getConnection(); String sql = "SELECT * FROM people WHERE last_name = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, lastname); ResultSet rset = stmt.executeQuery(); List<Person> people = new ArrayList<Person>(); while (rset.next()) { String firstName = rset.getString("first_name"); String lastName = rset.getString("last_name"); people.add(new Person(firstName, lastName)); } rset.close(); stmt.close(); conn.close(); return people; } catch (SQLException e) { throw new RuntimeException(e); } }  void setDatasource(DataSource datasource); List<Person> findByLastname(String lastname); void save(Person person); Person find(Long id); List<Person> findAll();  }