java - How do you connect to H2 as a remote database instead of embedded mode using Spring Boot? -


i have configuration under src/main/resources little spring boot application:

server.port = 8090 spring.datasource.driverclassname = org.h2.driver spring.datasource.url = jdbc:h2:file:~/stapler 

i know configuration picked properly, cause there valid port number 8090 in application startup log. there @postconstruct initdb() method creates , inserts data 2 tables of database:

package com.avk.stapler.init;  import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.jdbc.core.jdbctemplate;  import javax.annotation.postconstruct;  @springbootapplication public class dbinitializer {     @autowired     private jdbctemplate jdbctemplate;      public static void main(string[] args) {         springapplication.run(dbinitializer.class, args);     }      @postconstruct     private void initdb() {         system.out.println("creating table employees");         jdbctemplate.execute("drop table employees if exists");         jdbctemplate.execute("create table employees(id serial, name varchar(255), surname varchar(255))");         jdbctemplate.execute("insert employees(name, surname) values('jan', 'kowalski')");         jdbctemplate.execute("insert employees(name, surname) values('stefan', 'nowak')");           system.out.println("creating table allocations");         jdbctemplate.execute("drop table allocations if exists");         jdbctemplate.execute("create table allocations(id serial, week int, year int, shift int, employee_id bigint)");         jdbctemplate.execute("insert allocations(week, year, shift, employee_id) values(29, 2015, 1, 1)");         jdbctemplate.execute("insert allocations(week, year, shift, employee_id) values(28, 2015, 2, 1)");         jdbctemplate.execute("insert allocations(week, year, shift, employee_id) values(29, 2015, 3, 2)");         jdbctemplate.execute("insert allocations(week, year, shift, employee_id) values(28, 2015, 2, 2)");     } } 

i can see logged on startup, don't think there more logs regarding db:

2015-09-30 22:41:22.948  info 2832 --- [           main] o.s.j.d.e.embeddeddatabasefactory        : creating embedded database 'testdb' creating table employees creating table allocations 

and result of above, i'd see "stapler.h2.db" file in home directory, not case. should changed here db file appear?

make sure maven dependencies this:

<dependencies>     <dependency>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-jdbc</artifactid>     </dependency>     <dependency>         <groupid>com.h2database</groupid>         <artifactid>h2</artifactid>     </dependency>     <dependency>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-test</artifactid>         <scope>test</scope>     </dependency> </dependencies> 

if want use h2 remote database using jdbc, need make sure running h2 database @ specified file path in connection url.

if haven't installed h2, can instructions run h2 in server mode here: http://www.h2database.com/html/tutorial.html#tutorial_starting_h2_console

once have running, can connect using same jdbc connection url you've provided. use following application properties.

spring.datasource.url=jdbc:h2:tcp://localhost/~/stapler spring.datasource.username=sa spring.datasource.password= 

if you'd rather embedded h2 database create h2 file, that's possible. use configuration below.

spring.datasource.url=jdbc:h2:file:~/stapler;auto_server=true spring.datasource.username= spring.datasource.password= 

it's possible file created named stapler.mv.db. tell h2 embedded use stapler.h2.db instead, can learn how here: why embedded h2 program writing .mv.db file

(big stéphane nicoll helping me answer one)


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -