Spring boot H2 database is one of the most important feature. As we know spring boot provides many build in feature and one of them is spring boot H2 database. If we are developing a spring boot application and we want to do some database activity but we don’t have any database installed in our system. So we don’t need to worry as spring boot provide a inbuild database called H2.
For Enabling spring boot H2 database we just have to follow below steps:
- Add H2 and JPA maven dependency in your application pom.xml file
- Enable H2 database by adding few properties in the application.properties file.
Look how easy it is to use H2 database in our spring boot application. We just have to follow the above two given steps and we are good to go. So let’s do the implementation.
We ‘ll develop this application from the scratch. So let’s start, Go to the https://start.spring.io/ and create a spring boot application. It’s totally upto you how you want to create the project if you have Spring boot plugin in your IDE you can create the project from IDE itself else you can go to this website and create your spring boot project. We are using Java 17 so make sure you have Java 17 installed in your system.
How to check which java version installed in our system
It is very easy to check which java version installed in your system. Just open your Command Prompt. Press win+R if you are on window a pop up will come type cmd and click on Ok. Command Prompt will open just type command java –version and press enter button


Let’s create a Spring boot application now. Go to https://start.spring.io/ and create a spring boot project with the required dependency. Please follow below image for your reference.

After click on Generate button you ‘ll get the project in your download folder. Import in your favorite IDE . Here i am using Eclipse IDE and project structure look like below image.

So we have our project ready in our IDE. Now open your application.properties file and put the below properties there.
spring.application.name=SpringbootH2Database
# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=javadream
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
For brief description of the above property please refer the image given below:

Just run your application. Once your application start try to access the H2 database url in your browser. H2 database screen ‘ll come up. Now provide the username and password that you have given in your application.properties file. After successful login you can see the H2 database. Now run any query or play with this dashboard.


Look how easy it is to use H2 database in the spring boot application. You have your h2 console ready now. You can play with this, write any sql query try some tables creation etc.
Or what we can do is we can create an class and make it is an entity. As we know that while we use JPA(Java persistence API) it ‘ll automatically map our java class objects to the database tables. So go to your project and try to create a Entity class and while we run our application we ‘ll see that in our H2 database a table must be created.
Open you Spring boot application and make a Employee class with below code:
package com.javadream.SpringbootH2Database.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String emailId;
private Double salary;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
After creatin of Employee class just run the application. And open the H2 database you ‘ll see Employee table has been created there.


Reference: Spring boot database integration official Docs