Skip to content

Commit 93339f4

Browse files
micropatelKevinGilmore
authored andcommitted
BAEL-1980 - Sharing H2 database (eugenp#5039)
* Added Spring-Boot-H2 project * Optimized imports and formatted code * Code Formatting changes * Renamed the client applicaiton * Removed Name from pom.xml * formatting changes - applied formatting style * added souts * Update SpringBootApp.java * Update ClientSpringBootApp.java * Fixed Formatting issues * Fixed formatting and spelling mistakes
1 parent c1db9c7 commit 93339f4

8 files changed

Lines changed: 278 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.h2db</groupId>
8+
<artifactId>spring-boot-h2-database</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<description>Demo Spring Boot applications that starts H2 in memory database</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>com.h2database</groupId>
35+
<artifactId>h2</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.h2db.demo;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.util.Arrays;
6+
import javax.annotation.PostConstruct;
7+
import org.h2.tools.Server;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.jdbc.core.JdbcTemplate;
13+
import org.springframework.jdbc.core.RowMapper;
14+
15+
@SpringBootApplication
16+
public class SpringBootApp {
17+
18+
@Autowired
19+
private JdbcTemplate jdbcTemplate;
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(SpringBootApp.class, args);
23+
}
24+
25+
@PostConstruct
26+
private void initDb() {
27+
System.out.println(String.format(
28+
"****** Creating table: %s, and Inserting test data ******", "Employees"));
29+
30+
String sqlStatements[] = {
31+
"drop table employees if exists",
32+
"create table employees(id serial,first_name varchar(255),last_name varchar(255))",
33+
"insert into employees(first_name, last_name) values('Eugen','Paraschiv')",
34+
"insert into employees(first_name, last_name) values('Scott','Tiger')"
35+
};
36+
37+
Arrays.asList(sqlStatements).stream().forEach(sql -> {
38+
System.out.println(sql);
39+
jdbcTemplate.execute(sql);
40+
});
41+
42+
System.out.println(String.format("****** Fetching from table: %s ******", "Employees"));
43+
jdbcTemplate.query("select id,first_name,last_name from employees",
44+
new RowMapper<Object>() {
45+
@Override
46+
public Object mapRow(ResultSet rs, int i) throws SQLException {
47+
System.out.println(String.format("id:%s,first_name:%s,last_name:%s",
48+
rs.getString("id"),
49+
rs.getString("first_name"),
50+
rs.getString("last_name")));
51+
return null;
52+
}
53+
});
54+
}
55+
56+
@Bean(initMethod = "start", destroyMethod = "stop")
57+
public Server inMemoryH2DatabaseServer() throws SQLException {
58+
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091");
59+
}
60+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.datasource.url=jdbc:h2:mem:mydb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.h2.console.enabled=true
7+
spring.h2.console.path=/h2-console
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung.h2db</groupId>
8+
<artifactId>spring-boot-h2-remote-app</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<description>Demo Spring Boot applications that access H2 in memory database created
13+
in another Spring Boot application
14+
</description>
15+
16+
<parent>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-parent</artifactId>
19+
<version>2.0.4.RELEASE</version>
20+
<relativePath/> <!-- lookup parent from repository -->
21+
</parent>
22+
23+
<properties>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
26+
<java.version>1.8</java.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>com.h2database</groupId>
37+
<artifactId>h2</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.h2db.demo;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.util.Arrays;
6+
import javax.annotation.PostConstruct;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.jdbc.core.RowMapper;
12+
13+
@SpringBootApplication
14+
public class ClientSpringBootApp {
15+
16+
@Autowired
17+
private JdbcTemplate jdbcTemplate;
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(ClientSpringBootApp.class, args);
21+
}
22+
23+
@PostConstruct
24+
private void initDb() {
25+
System.out.println("****** Inserting more sample data in the table: Employees ******");
26+
String sqlStatements[] = {
27+
"insert into employees(first_name, last_name) values('Donald','Trump')",
28+
"insert into employees(first_name, last_name) values('Barack','Obama')"
29+
};
30+
31+
Arrays.asList(sqlStatements).stream().forEach(sql -> {
32+
System.out.println(sql);
33+
jdbcTemplate.execute(sql);
34+
});
35+
36+
System.out.println(String.format("****** Fetching from table: %s ******", "Employees"));
37+
jdbcTemplate.query("select id,first_name,last_name from employees",
38+
new RowMapper<Object>() {
39+
@Override
40+
public Object mapRow(ResultSet rs, int i) throws SQLException {
41+
System.out.println(String.format("id:%s,first_name:%s,last_name:%s",
42+
rs.getString("id"),
43+
rs.getString("first_name"),
44+
rs.getString("last_name")));
45+
return null;
46+
}
47+
});
48+
}
49+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.datasource.url=jdbc:h2:tcp://localhost:9091/mem:mydb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=
5+
spring.jpa.hibernate.ddl-auto=create

0 commit comments

Comments
 (0)