Search⌘ K

Setting up the H2 Database

Explore how to set up the in-memory H2 database within a Spring Boot application. Understand configuring connection settings, creating tables with schema.sql, and populating data using data.sql files to initialize the database automatically at startup.

In this lesson, we will set up the in-memory H2 database. Let’s have a look the required steps:

Configuring database connection

  • The in-memory database H2 has automatically been configured in our application. The URL of the database can be found from the console log. This value is randomly generated each time the server is restarted. To make the database URL a constant, we need to configure this in application.properties as follows:

spring.datasource.url=jdbc:h2:mem:testdb
Setting up the H2 base URL in application.properties file
  • The next task is connecting to the H2 database. One of the reasons for using Spring Boot is that its autoconfiguration feature looks at the H2 ...