Merge pull request from niket17590/feature/BAEL-7086-ResultSetToMap

BAEL-7086 - Convert ResultSet Into Map
This commit is contained in:
Maiklins 2023-11-10 13:04:12 +01:00 committed by GitHub
commit 9cea810493
3 changed files with 137 additions and 0 deletions
persistence-modules
core-java-persistence-3
pom.xml
src/test/java/com/baeldung/resultsettomap
pom.xml

@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.core-java-persistence-3</groupId>
<artifactId>core-java-persistence-3</artifactId>
<name>core-java-persistence-3</name>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>persistence-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>
</dependencies>
<properties>
<h2.version>2.1.214</h2.version>
<commons-dbutils.version>1.8.1</commons-dbutils.version>
</properties>
</project>

@ -0,0 +1,107 @@
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 ddlQuery = "CREATE TABLE employee (empId INTEGER not null, empName VARCHAR(50), empCity VARCHAR(50), PRIMARY KEY (empId))";
statement.execute(ddlQuery);
List<String> 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.execute(query);
}
}
@Test
public void whenUsingContainsKey_thenConvertResultSetToMap() throws SQLException {
ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee")
.executeQuery();
Map<String, List<String>> valueMap = new HashMap<>();
while (resultSet.next()) {
String empCity = resultSet.getString("empCity");
String empName = resultSet.getString("empName");
if (!valueMap.containsKey(empCity)) {
valueMap.put(empCity, new ArrayList<>());
}
valueMap.get(empCity)
.add(empName);
}
assertEquals(3, valueMap.get("London")
.size());
}
@Test
public void whenUsingComputeIfAbsent_thenConvertResultSetToMap() throws SQLException {
ResultSet resultSet = connection.prepareStatement("SELECT * FROM employee")
.executeQuery();
Map<String, List<String>> 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<Map<String, List<String>>> handler = new ResultSetHandler<Map<String, List<String>>>() {
public Map<String, List<String>> handle(ResultSet resultSet) throws SQLException {
Map<String, List<String>> 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<String, List<String>> valueMap = run.query(connection, "SELECT * FROM employee", handler);
assertEquals(3, valueMap.get("London")
.size());
}
@After
public void preDestroy() throws Exception {
connection.close();
}
}

@ -21,6 +21,7 @@
<module>blaze-persistence</module>
<module>core-java-persistence</module>
<module>core-java-persistence-2</module>
<module>core-java-persistence-3</module>
<module>couchbase</module>
<module>elasticsearch</module>
<module>flyway</module>