Search in sources :

Example 1 with BeanListHandler

use of org.apache.commons.dbutils.handlers.BeanListHandler in project tutorials by eugenp.

the class EmployeeHandler method handle.

@Override
public List<Employee> handle(ResultSet rs) throws SQLException {
    List<Employee> employees = super.handle(rs);
    QueryRunner runner = new QueryRunner();
    BeanListHandler<Email> handler = new BeanListHandler<>(Email.class);
    String query = "SELECT * FROM email WHERE employeeid = ?";
    for (Employee employee : employees) {
        List<Email> emails = runner.query(connection, query, handler, employee.getId());
        employee.setEmails(emails);
    }
    return employees;
}
Also used : BeanListHandler(org.apache.commons.dbutils.handlers.BeanListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 2 with BeanListHandler

use of org.apache.commons.dbutils.handlers.BeanListHandler in project tutorials by eugenp.

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenEmployeeList.

@Test
public void givenResultHandler_whenExecutingQuery_thenEmployeeList() throws SQLException {
    BeanListHandler<Employee> beanListHandler = new BeanListHandler<>(Employee.class);
    QueryRunner runner = new QueryRunner();
    List<Employee> employeeList = runner.query(connection, "SELECT * FROM employee", beanListHandler);
    assertEquals(employeeList.size(), 5);
    assertEquals(employeeList.get(0).getFirstName(), "John");
    assertEquals(employeeList.get(4).getFirstName(), "Christian");
}
Also used : BeanListHandler(org.apache.commons.dbutils.handlers.BeanListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Aggregations

QueryRunner (org.apache.commons.dbutils.QueryRunner)2 BeanListHandler (org.apache.commons.dbutils.handlers.BeanListHandler)2 AsyncQueryRunner (org.apache.commons.dbutils.AsyncQueryRunner)1 Test (org.junit.Test)1