BAEL-1980 Access an in-memory h2 db from 2 Spring Boot applications (#5230)
* 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 * added H2 shared db example * a * patch * test * Revert "Fixed formatting and spelling mistakes" This reverts commited56ededec. * Revert "test" This reverts commit4a7133dbd1. * Revert "patch" This reverts commit404b9ebe74. * Revert "a" This reverts commitaf77f84111. * Revert "added H2 shared db example" This reverts commite16918cae3. * Updated to have client and server in same directory * Deleted old files * removed unwanted directory * formating * removed unwanted module * commentted removed module * Revert "commentted removed module" This reverts commit211ff1441f. * Revert "removed unwanted module" This reverts commit657f9ee45e. * fixed pom.xml issue * typo fixed * fixed multiple main class issue
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.h2db.demo.client;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.baeldung.h2db.demo.client")
|
||||
public class ClientSpringBootApp {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("spring.datasource.url","jdbc:h2:tcp://localhost:9091/mem:mydb");
|
||||
SpringApplication.run(ClientSpringBootApp.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void initDb() {
|
||||
System.out.println("****** Inserting more sample data in the table: Employees ******");
|
||||
String sqlStatements[] = {
|
||||
"insert into employees(first_name, last_name) values('Donald','Trump')",
|
||||
"insert into employees(first_name, last_name) values('Barack','Obama')"
|
||||
};
|
||||
|
||||
Arrays.asList(sqlStatements).stream().forEach(sql -> {
|
||||
System.out.println(sql);
|
||||
jdbcTemplate.execute(sql);
|
||||
});
|
||||
|
||||
System.out.println(String.format("****** Fetching from table: %s ******", "Employees"));
|
||||
jdbcTemplate.query("select id,first_name,last_name from employees",
|
||||
new RowMapper<Object>() {
|
||||
@Override
|
||||
public Object mapRow(ResultSet rs, int i) throws SQLException {
|
||||
System.out.println(String.format("id:%s,first_name:%s,last_name:%s",
|
||||
rs.getString("id"),
|
||||
rs.getString("first_name"),
|
||||
rs.getString("last_name")));
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.h2db.demo;
|
||||
package com.baeldung.h2db.demo.server;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -9,10 +9,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.baeldung.h2db.demo.server")
|
||||
public class SpringBootApp {
|
||||
|
||||
@Autowired
|
||||
@@ -24,8 +26,7 @@ public class SpringBootApp {
|
||||
|
||||
@PostConstruct
|
||||
private void initDb() {
|
||||
System.out.println(String.format(
|
||||
"****** Creating table: %s, and Inserting test data ******", "Employees"));
|
||||
System.out.println(String.format("****** Creating table: %s, and Inserting test data ******", "Employees"));
|
||||
|
||||
String sqlStatements[] = {
|
||||
"drop table employees if exists",
|
||||
@@ -57,4 +58,4 @@ public class SpringBootApp {
|
||||
public Server inMemoryH2DatabaseServer() throws SQLException {
|
||||
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user