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.
We only need the H2 database at runtime so we specify the scope to
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).
Now we have all dependencies we need. Let specify some configuration to get the database actually works.
# H2 database connection info
In case you don’t want to use the password we can remove the
spring.datasource.passwordproperty, or leave it blank.
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
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.
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.
H2 Database Engine
You can contribute to the development of H2 by sending feedback and bug reports, or translate the H2 Console…
Give me motivation
To get new article update please follow our publication or follow us on social