H2 Console Cant see tables created by JAVA

Dunken picture Dunken · Jun 9, 2014 · Viewed 25.3k times · Source

I have downloaded the H2 console from http://www.h2database.com/html/download.html
and I have configured the URL in my jdbc.properties file
to jdbc:h2:c:/data/Messaging.

I am using the same URL in the file to connect to the database but I cannot see the tables; I can only see the Information Schema and when I try to select * from tables in it I cannot see the tables neither.

Does anybody have any idea what could be wrong?

Answer

MattC picture MattC · Apr 27, 2018

One tricky thing is that the H2 console will not give you an error if you try to connect to a JDBC URL that doesn't exist. It will instead create a new database at that URL! To connect to the in memory DB, use this JDBC URL (http://localhost:8080/h2-console is the default console):

jdbc:h2:mem:testdb

If you were to enter something like jdbc:h2:~/test then a test.mv file would be created under your home directory. But your application would still be using the in memory database.

The console is available if you have the h2 dependency in your pom, and also the spring developer tools dependency. If you don't have the tools dependency, then you can also see it by having the h2 dependency and adding the following to your application.properties file:

spring.h2.console.enabled=true  #not needed if you have spring-boot-devtools dependency

If you want the db as a file, and not in memory, add the following to applications.properties:

spring.datasource.url=jdbc:h2:~/test_db  #You will see the file in your home directory.

H2 isn't meant for persisted data, but if you want to persist for testing purposes, then add:

spring.jpa.hibernate.ddl-auto = update

Then start up the app, and at the console, use this JDBC URL:

jdbc:h2:~/test_db

In case you were wondering, I only have 1 entry in application.properties (for the database file) and here are my dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>