Configuring H2 database in Spring Boot
This article shows you how to configure the H2 database within a Spring Boot application.
What is H2 database?
H2 database is an open-source in-memory database that can be easily embedded into Java application. H2 database provides a list of features such as Pure Java, memory mode, encrypted database, ODBC driver even full-text search. Its size approx around 2MB.
H2 database also provides a browser-based Console application to manage data.


H2 database configuration in spring boot
The first we need to do is add the H2 database to our classpath.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
We only need the H2 database at runtime so we specify the scope to runtime
.
We also need spring-boot-starter-data-jpa
to pre-create database or we have to allow remote database creation(it effectively creates a remote security hole in your system).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Now we have all dependencies we need. Let specify some configuration to get the database actually works.
# H2 database connection info
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=passwordspring.h2.console.enabled=true
In case you don’t want to use the password we can remove the spring.datasource.password
property, or leave it blank.
By specifying spring.h2.console.enable=true
it enables the browser-based Console application. After starting the application we accession via http://localhost:8080/h2-console/.

Through the console, we can easy to execute DML queries or DDL queries.
Addition H2 configuration properties:
# default value h2-console
spring.h2.console.path=/h2-database-console# Enable trace output, default value: false
spring.h2.console.settings.trace=true# Allowing remote access, default value: false
spring.h2.console.settings.webAllowOthers=true# password for accessing H2 Console Preferences
# and Administration Tools
spring.h2.console.settings.web-admin-password=AdminPassword

The Preferences and Tools require web-admin-password to access.
Initialize tables and import data
In most popular situations require the creation of database tables, and data during the application starting process. Foutuanly, we just have to define DML and insert statements in a file named with data.sql and add it to classpath but we recommend that you define your database schema in a file named schema.sql and the insert statement in data.sql.
Conclusion
By reading to the end of this article, we have known how to config H2 for Spring boot application as well as some essential configuration h2 Console.
Reference:
Source code: https://github.com/Programming-Sharing/spring-data-topical/tree/configuration-h2-database-spring-boot
Origin publish: http://www.programmingsharing.com/configuration-embedded-h2-database-in-spring-boot-application/
Give me motivation
To get new article update please follow our publication or follow us on social
Facebook: https://www.facebook.com/programmingsharing
Twitter: http://twitter.com/progsharing