H2 Console Cant see tables created by JAVA

后端 未结 7 1854
北海茫月
北海茫月 2021-02-02 11:18

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

7条回答
  •  臣服心动
    2021-02-02 12:09

    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:

    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.boot
        spring-boot-devtools
        true
    
    
        org.springframework.boot
        spring-boot-starter-data-rest
    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    
    
        com.h2database
        h2
    
    

提交回复
热议问题