Skip to content

Commit bb4a527

Browse files
authored
BAEL-8008 - H2 running script from file (#16765)
* BAEL-8008 - H2 running script from file * BAEL-8008 - changing script file locations * BAEL-8008 - moving to another module * BAEL-8008 - moving to another module
1 parent 32d522a commit bb4a527

File tree

10 files changed

+140
-1
lines changed

10 files changed

+140
-1
lines changed

persistence-modules/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@
130130
<module>spring-data-cassandra-2</module>
131131
<module>spring-data-jpa-repo-3</module>
132132
<module>spring-boot-persistence-4</module>
133+
<module>spring-boot-persistence-5</module>
133134
<module>hibernate-annotations-2</module>
134135
<module>hibernate-reactive</module>
135-
136136
</modules>
137137

138138
<properties>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Relevant Articles
2+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung.boot.persistence</groupId>
7+
<artifactId>spring-boot-persistence-5</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>spring-boot-persistence-5</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-boot-3</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../../parent-boot-3</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-jpa</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.h2database</groupId>
25+
<artifactId>h2</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
35+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.h2;
2+
3+
import jakarta.annotation.PostConstruct;
4+
import org.h2.tools.RunScript;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.core.io.ClassPathResource;
11+
12+
import java.io.FileReader;
13+
import java.io.IOException;
14+
import java.sql.Connection;
15+
import java.sql.DriverManager;
16+
import java.sql.ResultSet;
17+
import java.sql.SQLException;
18+
19+
@SpringBootApplication
20+
public class H2Application {
21+
public static final Logger log = LoggerFactory.getLogger(H2Application.class);
22+
23+
@Value("${spring.datasource.url}")
24+
private String url;
25+
@Value("${spring.datasource.username}")
26+
private String user;
27+
@Value("${spring.datasource.password}")
28+
private String password;
29+
30+
public static void main(String[] args) {
31+
SpringApplication.run(H2Application.class, args);
32+
}
33+
34+
35+
@PostConstruct
36+
public void init() throws SQLException, IOException {
37+
Connection connection = DriverManager.getConnection(url, user, password);
38+
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile()));
39+
log.info("Reading Data from the employee table");
40+
while (rs.next()) {
41+
log.info("ID: {}, Name: {}", rs.getInt("id"), rs.getString("name"));
42+
}
43+
}
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=password
5+
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
6+
spring.jpa.hibernate.ddl-auto=none
7+
spring.sql.init.data-locations=classpath:db/data.sql
8+
spring.sql.init.schema-locations=classpath:db/schema.sql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO employee (name) VALUES ('John');
2+
INSERT INTO employee (name) VALUES ('Jane');
3+
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE IF NOT EXISTS employee (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL
4+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS employee (
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL
4+
);
5+
INSERT INTO employee (name) VALUES ('John');
6+
INSERT INTO employee (name) VALUES ('Jane');
7+
UPDATE EMPLOYEE SET NAME = 'Jane Doe' WHERE ID = 2;
8+
UPDATE employee SET NAME = 'John Doe' WHERE ID = 1;
9+
SELECT * FROM employee;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.h2;
2+
3+
import org.h2.tools.RunScript;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.core.io.ClassPathResource;
8+
9+
import java.io.FileReader;
10+
import java.sql.Connection;
11+
import java.sql.DriverManager;
12+
import java.sql.ResultSet;
13+
14+
@SpringBootTest
15+
class H2ApplicationIntegrationTest {
16+
17+
18+
@Test
19+
void givenApplication_whenBootstrapped_thenDataAvailable() throws Exception {
20+
Connection connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "password");
21+
ResultSet rs = RunScript.execute(connection, new FileReader(new ClassPathResource("db/script.sql").getFile()));
22+
Assertions.assertTrue(rs.next());
23+
Assertions.assertEquals(1, rs.getInt("id"));
24+
Assertions.assertEquals("John Doe", rs.getString("name"));
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=password
5+
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
6+
spring.jpa.hibernate.ddl-auto=none
7+
spring.sql.init.data-locations=classpath:db/data.sql
8+
spring.sql.init.schema-locations=classpath:db/schema.sql

0 commit comments

Comments
 (0)