How to use H2 database in Spring boot Application

Home / How to use H2 database in Spring boot Application

As we know we can use many database with spring boot application like MySQL, MongoDB, SQL Server etc. But if we don’t have any database installed in our machine then we can use this embedded database of spring boot. Spring boot provide us this embedded database for performing task. But it is not a good choice for production because this database is accessible only when our application is running if we stop our application it will automatically off the H2 database. To see how to configure h2 database in spring boot we have to follow the below steps:

  1. Add the required dependency in your pom.xml file
  2. Enable the H2 database by defining property in your application.properties file.
  3. Create an Entity using JPA named as student.
  4. Run your application and hit the H2 console URL, Change the JDBC URL in H2 UI to jdbc:h2:mem:testdb and connect to database.

Follow above Steps:

1- Add the required dependency in your pom.xml file

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

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

2- Enable the H2 database by defining property in your application.properties file.

server.port=8081
   
spring.h2.console.path=/h2-console
Spring.h2.console.enabled=true

Spring.jpa.show-sql=true


3- Create an Entity using JPA named as student.

package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "studentId")
	private long id;

	private String name;

	private String emailId;

	public Student() {
		super();
	}

	public Student(long id, String name, String emailId) {
		super();
		this.id = id;
		this.name = name;
		this.emailId = emailId;
	}

	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;
	}

}

4- Run your application and hit the H2 console URL, Change the JDBC URL in H2 UI to jdbc:h2:mem:testdb and connect to database.

As we define port as 8081 and h2 console path as /h2-console in application.properties file. So you have to hit the below URL:

http://localhost:8081/h2-console

Now when you hit the above URL you will get a UI like below, change the JDBC URL to jdbc:h2:mem:testdb and click on connect you will get the table named as Student at we create using JPA.

h2 database spring boot
h2 database spring boot

Complete code on GitHub: Spring Boot H2 Database

You may also like:

Profiles in Spring boot application

Swagger2 in Spring Boot

Spring Boot Security With MySql

Spring boot security using JWT ( JSON WEB TOKENS ).

File Upload in Spring Boot

Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *