Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Console Cant see tables created by JAVA

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?

like image 302
Dunken Avatar asked Jun 09 '14 09:06

Dunken


People also ask

How do I access my H2 database table?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.

How do I view embedded H2 database?

Connect to the embedded H2 database using the H2 console Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .


2 Answers

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>
like image 127
MattC Avatar answered Oct 26 '22 17:10

MattC


This is how you enable memory enable database using h2 module. You need to ensure the following things

  1. You had class that has @Entity annotations.
  2. you need to enable the following in application.properties file spring.h2.console.enabled=true
  3. Run Spring Boot and enter the following URL localhost:8080/h2-console
  4. This will show a connection screen. Enter the following changes in the JDBC URL: -> jdbc:h2:mem:testdb
  5. Hit the connection button.
like image 16
HA S Avatar answered Oct 26 '22 19:10

HA S