From 4596614dc9721bc246c587f35092c86330a92629 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 29 Oct 2023 01:42:22 +0530 Subject: [PATCH 1/4] Initial Commit --- .../core-java-persistence-3/pom.xml | 35 ++++++ .../ResultSetToMapUnitTest.java | 105 ++++++++++++++++++ persistence-modules/pom.xml | 1 + 3 files changed, 141 insertions(+) create mode 100644 persistence-modules/core-java-persistence-3/pom.xml create mode 100644 persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java diff --git a/persistence-modules/core-java-persistence-3/pom.xml b/persistence-modules/core-java-persistence-3/pom.xml new file mode 100644 index 0000000000..6868da27be --- /dev/null +++ b/persistence-modules/core-java-persistence-3/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.baeldung.core-java-persistence-3 + core-java-persistence-3 + core-java-persistence-3 + 0.1.0-SNAPSHOT + jar + + com.baeldung + persistence-modules + 1.0.0-SNAPSHOT + + + + com.h2database + h2 + ${h2.version} + + + org.springframework + spring-jdbc + ${spring-jdbc.version} + + + commons-dbutils + commons-dbutils + ${commons-dbutils.version} + + + + 2.1.214 + 5.3.29 + 1.8.1 + + \ No newline at end of file diff --git a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java new file mode 100644 index 0000000000..b483cc04c9 --- /dev/null +++ b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java @@ -0,0 +1,105 @@ +package com.baeldung.resultsettomap; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.ResultSetHandler; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ResultSetToMapUnitTest { + private static Connection connection = null; + private static final String JDBC_URL = "jdbc:h2:mem:testDatabase"; + private static final String USERNAME = "dbUser"; + private static final String PASSWORD = "dbPassword"; + + @Before + public void setup() throws Exception { + connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); + initialDataSetup(); + } + + private void initialDataSetup() throws SQLException { + Statement statement = connection.createStatement(); + String sql = "CREATE TABLE EMPLOYEE (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; + statement.executeUpdate(sql); + List sqlQueryList = Arrays.asList("INSERT INTO EMPLOYEE VALUES (1, 'Steve','London')", "INSERT INTO EMPLOYEE VALUES (2, 'John','London')", "INSERT INTO EMPLOYEE VALUES (3, 'David', 'Sydney')", + "INSERT INTO EMPLOYEE VALUES (4, 'Kevin','London')", "INSERT INTO EMPLOYEE VALUES (5, 'Jade', 'Sydney')"); + for (String query : sqlQueryList) { + statement.executeUpdate(query); + } + } + + @Test + public void whenUsingNativeJava_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") + .executeQuery(); + Map> valueMap = new HashMap<>(); + + while (resultSet.next()) { + String empCity = resultSet.getString("empCity"); + String empName = resultSet.getString("empName"); + if (!valueMap.containsKey(empCity)) { + valueMap.putIfAbsent(empCity, new ArrayList<>()); + } + valueMap.get(empCity) + .add(empName); + } + assertEquals(3, valueMap.get("London") + .size()); + } + + @Test + public void whenUsingLambda_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") + .executeQuery(); + Map> valueMap = new HashMap<>(); + + while (resultSet.next()) { + String empCity = resultSet.getString("empCity"); + String empName = resultSet.getString("empName"); + valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()) + .add(empName); + } + assertEquals(3, valueMap.get("London") + .size()); + } + + @Test + public void whenUsingDbUtils_thenConvertResultSetToMap() throws SQLException { + + ResultSetHandler>> handler = new ResultSetHandler>>() { + public Map> handle(ResultSet resultSet) throws SQLException { + Map> result = new HashMap<>(); + while (resultSet.next()) { + String empCity = resultSet.getString("empCity"); + String empName = resultSet.getString("empName"); + result.computeIfAbsent(empCity, data -> new ArrayList<>()) + .add(empName); + } + return result; + } + }; + QueryRunner run = new QueryRunner(); + Map> valueMap = run.query(connection, "SELECT * FROM EMPLOYEE", handler); + assertEquals(3, valueMap.get("London") + .size()); + } + + @After + public void preDestroy() throws Exception { + connection.close(); + } +} diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index a99a180390..162894f1e0 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -21,6 +21,7 @@ blaze-persistence core-java-persistence core-java-persistence-2 + core-java-persistence-3 elasticsearch flyway flyway-repair From 6d2e874c264e67437078e9506e19dca6f0c181a9 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Sun, 29 Oct 2023 01:46:23 +0530 Subject: [PATCH 2/4] Updated Pom --- persistence-modules/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 53e408c3a5..9c150a28d7 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -21,6 +21,7 @@ blaze-persistence core-java-persistence core-java-persistence-2 + core-java-persistence-3 couchbase elasticsearch flyway From 3fe81a3e075ae05044038788b23fa8729115f734 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Fri, 3 Nov 2023 03:49:06 +0530 Subject: [PATCH 3/4] Formatting changes --- .../core-java-persistence-3/pom.xml | 6 --- .../ResultSetToMapUnitTest.java | 44 ++++++++----------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/persistence-modules/core-java-persistence-3/pom.xml b/persistence-modules/core-java-persistence-3/pom.xml index 6868da27be..26ac2a5218 100644 --- a/persistence-modules/core-java-persistence-3/pom.xml +++ b/persistence-modules/core-java-persistence-3/pom.xml @@ -16,11 +16,6 @@ h2 ${h2.version} - - org.springframework - spring-jdbc - ${spring-jdbc.version} - commons-dbutils commons-dbutils @@ -29,7 +24,6 @@ 2.1.214 - 5.3.29 1.8.1 \ No newline at end of file diff --git a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java index b483cc04c9..c8a6351804 100644 --- a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java +++ b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java @@ -33,48 +33,43 @@ public class ResultSetToMapUnitTest { private void initialDataSetup() throws SQLException { Statement statement = connection.createStatement(); - String sql = "CREATE TABLE EMPLOYEE (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; - statement.executeUpdate(sql); - List sqlQueryList = Arrays.asList("INSERT INTO EMPLOYEE VALUES (1, 'Steve','London')", "INSERT INTO EMPLOYEE VALUES (2, 'John','London')", "INSERT INTO EMPLOYEE VALUES (3, 'David', 'Sydney')", - "INSERT INTO EMPLOYEE VALUES (4, 'Kevin','London')", "INSERT INTO EMPLOYEE VALUES (5, 'Jade', 'Sydney')"); + String ddlQuery = "CREATE TABLE employee (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))"; + statement.execute(ddlQuery); + List sqlQueryList = Arrays.asList("INSERT INTO employee VALUES (1, 'Steve','London')", "INSERT INTO employee VALUES (2, 'John','London')", "INSERT INTO employee VALUES (3, 'David', 'Sydney')", + "INSERT INTO employee VALUES (4, 'Kevin','London')", "INSERT INTO employee VALUES (5, 'Jade', 'Sydney')"); + for (String query : sqlQueryList) { - statement.executeUpdate(query); + statement.execute(query); } } @Test - public void whenUsingNativeJava_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") - .executeQuery(); + public void whenUsingContainsKey_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); if (!valueMap.containsKey(empCity)) { - valueMap.putIfAbsent(empCity, new ArrayList<>()); + valueMap.put(empCity, new ArrayList<>()); } - valueMap.get(empCity) - .add(empName); + valueMap.get(empCity).add(empName); } - assertEquals(3, valueMap.get("London") - .size()); + assertEquals(3, valueMap.get("London").size()); } @Test - public void whenUsingLambda_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM EMPLOYEE") - .executeQuery(); + public void whenUsingComputeIfAbsent_thenConvertResultSetToMap() throws SQLException { + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()) - .add(empName); + valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); } - assertEquals(3, valueMap.get("London") - .size()); + assertEquals(3, valueMap.get("London").size()); } @Test @@ -86,16 +81,15 @@ public class ResultSetToMapUnitTest { while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - result.computeIfAbsent(empCity, data -> new ArrayList<>()) - .add(empName); + result.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); } return result; } }; + QueryRunner run = new QueryRunner(); - Map> valueMap = run.query(connection, "SELECT * FROM EMPLOYEE", handler); - assertEquals(3, valueMap.get("London") - .size()); + Map> valueMap = run.query(connection, "SELECT * FROM employee", handler); + assertEquals(3, valueMap.get("London").size()); } @After From 270acf62e46bc420a0517d756fa63b1742b904b2 Mon Sep 17 00:00:00 2001 From: Niket Agrawal Date: Tue, 7 Nov 2023 21:49:49 +0530 Subject: [PATCH 4/4] Formatter Changes --- .../ResultSetToMapUnitTest.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java index c8a6351804..e449beb83a 100644 --- a/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java +++ b/persistence-modules/core-java-persistence-3/src/test/java/com/baeldung/resultsettomap/ResultSetToMapUnitTest.java @@ -45,7 +45,8 @@ public class ResultSetToMapUnitTest { @Test public void whenUsingContainsKey_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee") + .executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { @@ -54,22 +55,27 @@ public class ResultSetToMapUnitTest { if (!valueMap.containsKey(empCity)) { valueMap.put(empCity, new ArrayList<>()); } - valueMap.get(empCity).add(empName); + valueMap.get(empCity) + .add(empName); } - assertEquals(3, valueMap.get("London").size()); + assertEquals(3, valueMap.get("London") + .size()); } @Test public void whenUsingComputeIfAbsent_thenConvertResultSetToMap() throws SQLException { - ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee").executeQuery(); + ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee") + .executeQuery(); Map> valueMap = new HashMap<>(); while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); + valueMap.computeIfAbsent(empCity, data -> new ArrayList<>()) + .add(empName); } - assertEquals(3, valueMap.get("London").size()); + assertEquals(3, valueMap.get("London") + .size()); } @Test @@ -81,15 +87,17 @@ public class ResultSetToMapUnitTest { while (resultSet.next()) { String empCity = resultSet.getString("empCity"); String empName = resultSet.getString("empName"); - result.computeIfAbsent(empCity, data -> new ArrayList<>()).add(empName); + result.computeIfAbsent(empCity, data -> new ArrayList<>()) + .add(empName); } return result; } }; - + QueryRunner run = new QueryRunner(); Map> valueMap = run.query(connection, "SELECT * FROM employee", handler); - assertEquals(3, valueMap.get("London").size()); + assertEquals(3, valueMap.get("London") + .size()); } @After