Enterprise Java

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.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.

spring boot

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 https://start.spring.io/

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.

spring boot

Step 3

Import the extracted folder in eclipse as existing Maven Project.

spring boot

Step 4

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

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

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 springBootAndH2DbIntegrationApplicaiton.java and run it as Java application. This will start embedded Tomcat Server as well and deploy the application on Tomcat.

spring boot

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

http://localhost:8080/h2/

We will get the following screen :

spring boot

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 application.properties file

# H2

spring.h2.console.enabled=true

spring.h2.console.path=/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.

http://localhost:8080/h2/

spring boot

Step 8

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

spring boot

Step 9

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

spring boot

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.

spring boot
By clicking on this icon, we are just disconnecting from the database, however, h2 console server is still up and running.so 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.

References

http://www.h2database.com/html/main.html

Published on Java Code Geeks with permission by Gaurav Bhardwaj, partner at our JCG program. See the original article here: How to Integrate H2 database in Spring Boot App

Opinions expressed by Java Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer Applications(MCA) and is working in Software development field for more than 10 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, Spring Boot, Angular JS, JSF, Velocity, iBatis, MyBatis, Hibernate, JUnit, Mockito, Dozzer. He likes to explore new technologies and share his thoughts by writing a technical blog. He is the founder of JavaSolutionsGuide.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button