[Spring Boot] How to configure H2 database and H2 console
Using Spring Initializer, add the following dependencies: H2 Database, Spring Data JPA, Spring Web.
If you run the main class without doing anything, it will run successfully and it will use in-memory H2 database by default.
To view the H2 console, we have to add some configurations in application.yml
under src/main/resources
directory.
Then, restart the app and go to http://localhost:8080/h2-console on your favorite browser. The screen will look like below:
By default, JDBC URL can be different from what we have configured like the screenshot below.
After changing the path of JDBC URL to what we have configured (jdbc:h2:mem:testdb or whichever value you have used for spring.datasource.url
in application.yml
), click Test Connection button to test login.
Note that input values for User Name and Password on the UI have to match the values given to spring.datasource.username
and spring.datasource.password
as well.
Once connection test runs successfully, click Connect button. Now we can access h2-console and execute queries.
View full source at https://github.com/jskim1991/code-lab/tree/main/spring-boot-h2-sample