How to Connect to H2 Database in Spring Boot Easily
To connect to an
H2 database in Spring Boot, add the spring-boot-starter-data-jpa and com.h2database:h2 dependencies, then configure the datasource in application.properties. Spring Boot auto-configures the connection, allowing you to use JdbcTemplate or JPA repositories to access the database.Syntax
To connect Spring Boot to an H2 database, you need to add dependencies and configure connection properties.
- Dependencies: Include
spring-boot-starter-data-jpafor JPA support andcom.h2database:h2for the H2 driver. - Configuration: Set datasource URL, username, and password in
application.properties. - Auto-configuration: Spring Boot automatically sets up the datasource and JPA based on these settings.
properties
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
}
# application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=trueExample
This example shows a simple Spring Boot application connecting to an H2 in-memory database with a JPA entity and repository. The H2 console is enabled for easy web access.
java
package com.example.demo; import jakarta.persistence.Entity; import jakarta.persistence.Id; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @Entity class User { @Id private Long id; private String name; public User() {} public User(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public String getName() { return name; } } @Repository interface UserRepository extends JpaRepository<User, Long> {} @RestController class UserController { private final UserRepository repo; public UserController(UserRepository repo) { this.repo = repo; repo.save(new User(1L, "Alice")); } @GetMapping("/users") public java.util.List<User> getUsers() { return repo.findAll(); } }
Output
[{"id":1,"name":"Alice"}]
Common Pitfalls
Common mistakes when connecting to H2 in Spring Boot include:
- Forgetting to add the
com.h2database:h2dependency, causing driver not found errors. - Not enabling the H2 console with
spring.h2.console.enabled=true, making it hard to inspect the database. - Using incorrect JDBC URL format; for in-memory use
jdbc:h2:mem:testdb. - Not setting the correct Hibernate dialect, which should be
org.hibernate.dialect.H2Dialect.
Example of a wrong and right property setting:
properties
# Wrong - missing driver and dialect spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.username=sa spring.datasource.password= # Right - complete configuration spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true
Quick Reference
Summary tips for connecting Spring Boot to H2:
- Add
spring-boot-starter-data-jpaandcom.h2database:h2dependencies. - Use
jdbc:h2:mem:testdbfor in-memory database. - Enable H2 console with
spring.h2.console.enabled=truefor browser access at/h2-console. - Set Hibernate dialect to
org.hibernate.dialect.H2Dialect. - Use JPA repositories or
JdbcTemplateto interact with the database.
Key Takeaways
Add H2 and Spring Data JPA dependencies to your project to enable database support.
Configure datasource URL, driver, username, password, and Hibernate dialect in application.properties.
Enable the H2 console for easy database inspection during development.
Use JPA repositories or JdbcTemplate to access the H2 database in your Spring Boot app.
Ensure the JDBC URL uses the correct format for in-memory or file-based H2 databases.