Configuring H2 database in Spring Boot

This article shows you how to configure the H2 database within a Spring Boot application.

Thanh Tran
Jan 4 · 3 min read

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 browser-based Console application
h2 database browser-based Console application
H2 database features
H2 database features

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=password
spring.h2.console.enabled=true

In case you don’t want to use the password we can remove the spring.datasource.passwordproperty, 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/.

Image for post
Image for post

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
Image for post
Image for post

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

Programming Sharing

We write about programming. We share about programming.

Sign up for Programming Sharings Newsletter

By Programming Sharing

Get new articles update by signing up our newsletter Take a look.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store