1. Overview
In this article, we will learn to use the H2 database with the Spring boot application. The H2 is a lightweight database that can run as an in-memory database, meaning by default data will not persist on the disk. However, you can use file-based storage to preserve the data in a file across the application restart.
2. Spring boot H2 Dependencies
Let’s add the following dependencies in your project to use the H2 database.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
The spring-boot-starter-data-jpa starter dependency includes JPA APIs, JDBCand other required libraries. This also brings in Hibernate which is the default JPA implementation.
3. Spring boot h2 console
H2 provides an embedded console for browsing the contents of a database and running SQL queries. By default, Spring does not enable the H2 console. To enable it, you had to add the following property in your application.properties:
spring.h2.console.enabled=true
After you enable and run, you would see the following statement in your console. By default, Spring Boot configures the application to?connect to an in-memory H2 database.
H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:98a4ddfb-0f22-adcd-0d2cc23f116b'
You can access the h2 console using the localhost URL http://localhost:8080/h2-console from your browser. Your JDBC URL in your h2 console login page should match the one printed in your logs (jdbc:h2:mem:testdb).


2.1. h2 database url
The h2 in-memory database JDBC URL would change for every run of your project. To make it static, add spring.datasource.url with your custom DB URL as the value in your application.properties file.
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.driverClassName=org.h2.Driver
2.2. h2 db credentials
By default, the username?is sa?and the password is empty. However, you can change the credentials using the following properties in your application.properties file.
spring.datasource.username=admin spring.datasource.password=admin
2.3. Preserve data in Spring boot h2 database
By default, the H2 in-memory database is volatile, and we will lose data whenever we restart the application.
However, you can change this default behavior by using file-based storage. To do so, update your?spring.datasource.url with your file path as below in your application.properties file:
jdbc:h2:file:C:/StudentDatabase
2.3. h2 console configuration settings
Following are the additional configuration options available for the H2 console:
trace: to prevent or enable traceweb-allow-others: allow other machines to connect the h2 console.web-admin-password: Password to access preferences and tools of H2 Console.
spring.h2.console.settings.trace=false spring.h2.console.settings.web-allow-others=false spring.h2.console.settings.web-admin-password=admin spring.h2.console.path=/h2-console-path
You can also change the h2 console path using the property spring.h2.console.path.
3. Spring boot hibernate JPA Entity
In this article, we will create a STUDENT table using JPA. To do so, create a JPA entity (object model) class with necessary attributes and run the project. Hibernate would convert the below Student class to a table in the database, referred to as Object-Relational mapping.
@Entity
public class Student {
@Id
@GeneratedValue
private Long id;
private String studentName;
public Long getId() {
return id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@Override
public String toString() {
return "Student [id=" + id + ", studentName=" + studentName + "]";
}
} After you run the project with the above code, the h2 console will reflect the STUDENT table in its in-memory database. The STUDENT table would have columns ID, STUDENT_NAME that are generated by using the Student class variables.

4. Inserting data to the table
You can create an import.sql or data.sql file in your resources folder and keep your insert queries there.
insert into STUDENT values(123, 'Siv'); insert into STUDENT values(124, 'Praj');


5. Creating Repository
Now, let’s interact with our database using the Repository class. A Repository is the Data access layer that allows you to access our real database and provides CRUD (Create, Read, Update and Delete) methods.
In the below code, we have annotated the StudentRepository interface class with @Repository that extends JpaRepository. The JpaRepository contains all the APIs of CrudRepository and PagingAndSortingRepository . Thus, it supports CRUD operations and also pagination and sorting.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tedblob.springboot.hibernate.entity.Student;
@Repository
public interface StudentRepository extends JpaRepository<Student, Long>{
} Now, you can access the database using the APIs of StudentRepository class. In the below SpringBootApplication class, Spring would inject the StudentRepository as a dependency that can be used for database access.
@SpringBootApplication
public class HibernateApplication implements CommandLineRunner {
@Autowired
StudentRepository studentRepository;
public static void main(String[] args) {
SpringApplication.run(HibernateApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(studentRepository.findAll());
}
} 6. Conclusion
To sum up, we have seen the various configuration and customization options available in the H2 database of the Spring boot project.