Skip to content
Snippets Groups Projects
Verified Commit 8150ad7e authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

use h2 database.

parent c8eaea66
Branches
No related merge requests found
......@@ -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>
......@@ -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;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment