Initial Commit

This commit is contained in:
Niket Agrawal 2023-10-29 01:42:22 +05:30
parent 29d0e4f04c
commit 4596614dc9
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<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>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.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>
<spring-jdbc.version>5.3.29</spring-jdbc.version>
<commons-dbutils.version>1.8.1</commons-dbutils.version>
</properties>
</project>

View File

@ -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<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.executeUpdate(query);
}
}
@Test
public void whenUsingNativeJava_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.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<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();
}
}

View File

@ -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>elasticsearch</module>
<module>flyway</module>
<module>flyway-repair</module>