From 8150ad7e5fdef9c0a2cb21d16ac77e0cde4ddb73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Thu, 9 Nov 2023 10:47:45 +0100
Subject: [PATCH] use h2 database.

---
 pom.xml                         |  7 +++++++
 src/main/java/lab/ScoreDAO.java | 36 +++++++++++++++++----------------
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3f573e8..44d191f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,5 +46,12 @@
 			<version>5.5.2</version>
 			<scope>test</scope>
 		</dependency>
+		<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
+		<dependency>
+		    <groupId>com.h2database</groupId>
+		    <artifactId>h2</artifactId>
+		    <version>2.2.224</version>
+		</dependency>
+
 	</dependencies>
 </project>
diff --git a/src/main/java/lab/ScoreDAO.java b/src/main/java/lab/ScoreDAO.java
index 8bfdc72..fca1922 100644
--- a/src/main/java/lab/ScoreDAO.java
+++ b/src/main/java/lab/ScoreDAO.java
@@ -16,6 +16,20 @@ import java.util.stream.Collectors;
 
 public class ScoreDAO {
 
+	@SuppressWarnings("unused")
+	private static final String SQL_CREATE_DERBY = "CREATE TABLE score ("
+				+ "id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
+				+ "score_value INT NOT NULL, "
+				+ "name VARCHAR(255) NOT NULL, "
+				+ "PRIMARY KEY (id)"
+				+ ")";
+
+	private static final String SQL_CREATE_H2 = "CREATE TABLE IF NOT EXISTS score ("
+			+ "  id INT AUTO_INCREMENT PRIMARY KEY,"
+			+ "  score_value INT NOT NULL,"
+			+ "  name VARCHAR(255) NOT NULL"
+			+ ")"	;
+	
 	public ScoreDAO() {
 		
 		try (Connection conn = getConnection()) {
@@ -28,7 +42,7 @@ public class ScoreDAO {
 	public List<Score> load( ){
 		List<Score> result = new LinkedList<>();
 		try(Connection con = getConnection(); Statement stmt = con.createStatement()) {
-			try(ResultSet rs = stmt.executeQuery("SELECT id, value, name FROM score")) {
+			try(ResultSet rs = stmt.executeQuery("SELECT id, score_value, name FROM score")) {
 				while (rs.next()) {
 					result.add(new Score(rs.getInt(1), rs.getInt(2), rs.getString(3)));
 				}
@@ -41,13 +55,6 @@ public class ScoreDAO {
 	
 	public void save(List<Score> scores) {
 		
-		/*Set<Integer> m = new HashSet<>();
-		for (Score score: scores) {
-			if (score.getId() == null) {
-				continue;
-			}
-			m.add(score.getId());
-		}*/
 		Set<Integer> m = scores.stream()
 							   .filter(s -> s.getId() != null)
 							   .map(Score::getId)
@@ -58,7 +65,7 @@ public class ScoreDAO {
 		removeByIDS(idsOnlyInDB);
 		List<Score> scoreSaved = new LinkedList<>();
 		try (Connection con = getConnection(); 
-			 PreparedStatement pstmt = con.prepareStatement("INSERT INTO score (value, name) VALUES (?, ?)"
+			 PreparedStatement pstmt = con.prepareStatement("INSERT INTO score (score_value, name) VALUES (?, ?)"
 					                                        , Statement.RETURN_GENERATED_KEYS)) {
 			for (Score score: scores) {
 				if (score.getId() != null) {
@@ -78,7 +85,7 @@ public class ScoreDAO {
 					score.setId(rs.getInt(1));
 				}
 			}
-			
+			return;
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
@@ -101,12 +108,7 @@ public class ScoreDAO {
 
 	private void createTable(Connection conn) {
 		try  (Statement stmt = conn.createStatement()){
-			stmt.execute("CREATE TABLE score ("
-						+ "id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
-						+ "value INT NOT NULL, "
-						+ "name VARCHAR(255) NOT NULL, "
-						+ "PRIMARY KEY (id)"
-						+ ")");
+			stmt.execute(SQL_CREATE_H2);
 		} catch (SQLException e) {
 			if (e.getSQLState().equals("X0Y32")) {
 				//ignore this because table exists
@@ -119,7 +121,7 @@ public class ScoreDAO {
 
 	private Connection getConnection() {
 		try {
-			return DriverManager.getConnection("jdbc:derby:scoreDB;create=true");
+			return DriverManager.getConnection("jdbc:h2:./scoreDBh2");
 		} catch (SQLException e) {
 			e.printStackTrace();
 			return null;
-- 
GitLab