I created a H2 In-Memory Database in Spring framework like this:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.H2) //.H2 or .DERBY
.addScript("create-table.sql")
.build();
I would like to make another Java application which connect to this Database and access the data from that.
I can connect to the database with this code:
Connection connecton = DriverManager.getConnection("jdbc:h2:mem:testdb","sa","");
But this returns me an empty database, without tables and records.
Here is a description which tell how to solve this problem.
Unfortunately I cannot understand how to do work with Spring container and the MethodInvokingBean
, because I don't know where to code, how to use, how it works etc.
I would appreciate that someone make a short tutorial.
Thanks
You can start a TCP server to share the database. Adding following configuration:
<bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,9092"/>
</bean>
Once the server start up, other Java application can connect to it using jdbc:h2:tcp://localhost:9092/mem:testdb