How to Integrate H2 database in Spring Boot App

Hello Friends,

In this tutorial, we will try to explore that how we can integrate with the H2 database in a Spring Boot application.

Before we check that, let us understand a few of the basics about the H2 database, as mentioned below and then we will discuss the integration of the H2 database with  Spring Boot

What is the H2 database?

What is H2 Console?

Why in memory(H2) database?

How to integrate the H2 Database with Spring Boot?

What is the H2 database?

H2 in nutshell is an in-memory database, which has the following features :

- Open Source
- Very lightweight. The size of h2 DB jar is around 1 MB only
- Very fast
- Written purely in Java.
- Supports Web Console
- Supports Standard SQL and JDBC API

It is called in-memory database because it is created on starting the application and gets destroyed on stopping the application.

What is H2 Console?

H2 console application helps to access the database from a browser.

So basically, it is a client/server application. As we are using h2 in embedded mode(h2 jar is part of our application), so once our application is up and running,h2 console server will also be up and from there we can connect to h2 database.

Why In memory database(h2)?

Say, we want to do some POC(Proof of concept) before starting a project or we are working on prototype, in such case scenario,it will not be ideal to set up an actual database, as it will involve a lot of effort and cost for having a database server, Installing database, creating schemas, tables etc.So in such scenarios we use in memory database like h2.

How to integrate the H2 Database with Spring Boot?

Integrating the H2 Database with Spring Boot is like a cakewalk.

Step 1

Go to

Step 2

Fill the Group, artifactId, and dependencies(web and H2) and click on Generate Project button. It will download a zip with name springBootAndH2DbIntegration on your local drive. Extract the zip.

Step 3 

Import the extracted folder in eclipse as existing Maven Project.

Step 4

Open the pom.xml and it will have following entry for H2 database


Note: We are adding the h2 dependency to connect with H2 in-memory database. As h2 jar will be there on the classpath, Spring boot automatically creates a data source to connect to h2 database.

Step 5

Open and run it as Java application. This will start embedded Tomcat Server as well and deploy the application on Tomcat.

As you can see in the logs that Tomcat started on port 8080 and SprigBootAndH2DbIntegrationApplicaiton started successfully.

Step 6 

Hit the following Url  to open web console of h2 


We will get the following screen :

The reason we are getting above error page is that we have not told Spring boot to enable the web console of h2 database and map /h2.we can do that by making following entry in the file

# H2

Step 7

Now if you hit the following Url, you can see that H2 web console is up and running and you can connect to H2 database.


Step 8

Now click on Test Connection button and you can see that connection is successful as can be seen in below screenshot :

Step 9

Now click on connect button and you will see that database connection with h2 DB is established.

Step 10

To disconnect h2 database, click on the following icon on extreme left of the H2 Console and it will land you back to the screen in step 6.

By clicking on this icon, we are just disconnecting from the database, however, h2 console server is still up and we can again click on connect and connect to the database.

That's all on How to integrate H2 database in Spring boot app. Please share it with someone you think it might help.