diff --git a/libraries/pom.xml b/libraries/pom.xml
index 28dd36fceb..dfae8085bb 100644
--- a/libraries/pom.xml
+++ b/libraries/pom.xml
@@ -227,6 +227,11 @@
commons-io
${commons.io.version}
+
+ commons-dbutils
+ commons-dbutils
+ ${commons.dbutils.version}
+
org.apache.flink
flink-core
@@ -369,7 +374,7 @@
com.h2database
h2
- 1.4.195
+ ${h2.version}
pl.pragmatists
@@ -530,6 +535,8 @@
9.4.3.v20170317
4.5.3
2.5
+ 1.6
+ 1.4.196
9.4.2.v20170220
4.5.3
2.5
diff --git a/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java b/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java
new file mode 100644
index 0000000000..c82798d52d
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/commons/dbutils/Email.java
@@ -0,0 +1,38 @@
+package com.baeldung.commons.dbutils;
+
+public class Email {
+ private Integer id;
+ private Integer employeeId;
+ private String address;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Integer getEmployeeId() {
+ return employeeId;
+ }
+
+ public void setEmployeeId(Integer employeeId) {
+ this.employeeId = employeeId;
+ }
+
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ @Override
+ public String toString() {
+ return "Email{" + "id=" + id + ", address=" + address + '}';
+ }
+
+}
diff --git a/libraries/src/main/java/com/baeldung/commons/dbutils/Employee.java b/libraries/src/main/java/com/baeldung/commons/dbutils/Employee.java
new file mode 100644
index 0000000000..e6f34c0201
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/commons/dbutils/Employee.java
@@ -0,0 +1,67 @@
+package com.baeldung.commons.dbutils;
+
+import java.util.Date;
+import java.util.List;
+
+public class Employee {
+ private Integer id;
+ private String firstName;
+ private String lastName;
+ private Double salary;
+ private Date hiredDate;
+ private List emails;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public Double getSalary() {
+ return salary;
+ }
+
+ public void setSalary(Double salary) {
+ this.salary = salary;
+ }
+
+ public Date getHiredDate() {
+ return hiredDate;
+ }
+
+ public void setHiredDate(Date hiredDate) {
+ this.hiredDate = hiredDate;
+ }
+
+ public List getEmails() {
+ return emails;
+ }
+
+ public void setEmails(List emails) {
+ this.emails = emails;
+ }
+
+ @Override
+ public String toString() {
+ return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", salary=" + salary + ", hiredDate=" + hiredDate + '}';
+ }
+
+}
\ No newline at end of file
diff --git a/libraries/src/main/java/com/baeldung/commons/dbutils/EmployeeHandler.java b/libraries/src/main/java/com/baeldung/commons/dbutils/EmployeeHandler.java
new file mode 100644
index 0000000000..6f68bafe57
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/commons/dbutils/EmployeeHandler.java
@@ -0,0 +1,45 @@
+package com.baeldung.commons.dbutils;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.dbutils.BasicRowProcessor;
+import org.apache.commons.dbutils.BeanProcessor;
+
+import org.apache.commons.dbutils.QueryRunner;
+import org.apache.commons.dbutils.handlers.BeanListHandler;
+
+public class EmployeeHandler extends BeanListHandler {
+
+ private Connection connection;
+
+ public EmployeeHandler(Connection con) {
+ super(Employee.class, new BasicRowProcessor(new BeanProcessor(getColumnsToFieldsMap())));
+ this.connection = con;
+ }
+
+ @Override
+ public List handle(ResultSet rs) throws SQLException {
+ List employees = super.handle(rs);
+
+ QueryRunner runner = new QueryRunner();
+ BeanListHandler handler = new BeanListHandler<>(Email.class);
+ String query = "SELECT * FROM email WHERE employeeid = ?";
+ for (Employee employee : employees) {
+ List emails = runner.query(connection, query, handler, employee.getId());
+ employee.setEmails(emails);
+ }
+ return employees;
+ }
+
+ public static Map getColumnsToFieldsMap() {
+ Map columnsToFieldsMap = new HashMap<>();
+ columnsToFieldsMap.put("FIRST_NAME", "firstName");
+ columnsToFieldsMap.put("LAST_NAME", "lastName");
+ columnsToFieldsMap.put("HIRED_DATE", "hiredDate");
+ return columnsToFieldsMap;
+ }
+}
diff --git a/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java
new file mode 100644
index 0000000000..8eb2fd1350
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/commons/dbutils/DbUtilsUnitTest.java
@@ -0,0 +1,160 @@
+package com.baeldung.commons.dbutils;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.dbutils.AsyncQueryRunner;
+import org.apache.commons.dbutils.DbUtils;
+import org.apache.commons.dbutils.QueryRunner;
+import org.apache.commons.dbutils.handlers.BeanListHandler;
+import org.apache.commons.dbutils.handlers.MapListHandler;
+import org.apache.commons.dbutils.handlers.ScalarHandler;
+import org.junit.After;
+import static org.junit.Assert.*;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DbUtilsUnitTest {
+
+ private Connection connection;
+
+ @Before
+ public void setupDB() throws Exception {
+ Class.forName("org.h2.Driver");
+ String db = "jdbc:h2:mem:;INIT=runscript from 'classpath:/employees.sql'";
+ connection = DriverManager.getConnection(db);
+ }
+
+ @After
+ public void closeBD() {
+ DbUtils.closeQuietly(connection);
+ }
+
+ @Test
+ public void givenResultHandler_whenExecutingQuery_thenExpectedList() throws SQLException {
+ MapListHandler beanListHandler = new MapListHandler();
+
+ QueryRunner runner = new QueryRunner();
+ List