How to see all tables in my h2 database at localhost:8082?

前端 未结 10 1039
误落风尘
误落风尘 2021-02-01 02:43

I use JDBC and created h2 database called usaDB from sql script. Then I filled all tables with jdbc.

The problem is that after I connect to usaDB at localhost:8082 I can

相关标签:
10条回答
  • 2021-02-01 03:13

    For the people who are using H2 in embedded(persistent mode) and want to "connect" to it from IntelliJ(other IDEs probably apply too).

    1. Using for example jdbc url as follows: jdbc:h2:./database.h2
    2. Note, that H2 does not allow implicit relative paths, and requires adding explicit ./
    3. Relative paths are relative to current workdir
    4. When you run your application, your workdir is most likely set to your project's root dir
    5. On the other hand, IDE's workdir is most likely not your project's root
    6. Hence, in IDE when "connecting" to your database you need to use absolute path like: jdbc:h2:/Users/me/projects/MyAwesomeProject/database.h2
    7. For some reason IntelliJ by default also adds ;MV_STORE=false. It disables MVStore engine which in fact is currently used by default in H2.
    8. So make sure that both your application and your IDE use the same store engine, as MVStore and PageStore have different file loyouts.
    9. Note that you cannot "connect" to your database if your application is using it because of locking. The other way around applies too.
    0 讨论(0)
  • 2021-02-01 03:16

    I have met exactly this problem.

    From what you describe, I suppose that you connect your jdbc with the "real" h2 server, but you are connecting on web application to database by the wrong mode (embedded-in-memory mode, aka h2mem). It means that h2 will create a new database in-memory, instead of using your true database stored elsewhere.

    Please make sure that when you connect to this database, you use the mode Generic H2 (Server), NOTGeneric H2 (Embedded). You can refer to the picture below.

    enter image description here

    0 讨论(0)
  • 2021-02-01 03:18

    This problem drove me around the twist and besides this page I read many (many!) others until I solved it.
    My Use Case was to see how a SpringBatch project created in STS using :: Spring Boot :: (v1.3.1.RELEASE) was going to behave with the H2 database; to do the latter, I needed to be able to get the H2 console running as well to query the DB results of the batch run.

    This is what I did and found out:

    1. Created an Web project in STS using Spring Boot:

      • Added the following to the pom.xml of the latter:
      • Added a Spring configuration file as follows to the project: This solves the Web project deficiencies in STS. If you run the project now, you can access the H2 console as follows: http://localhost:8080/console
    2. Now create a SpringBatch project in STS as follows (the alternative method creates a different template missing most of the classes for persisting data. This method creates 2 projects: one Complete, and the other an initial. Use the Complete in the following.):

      • The SpringBatch project created with STS uses an in memory H2 database that it CLOSES once the application run ends; once you run it, you can see this in the logging output.
      • So what we need is to create a new DataSource that overrides the default that ships with the project (if you are interested, just have a look at the log messages and you will see that it uses a default datasource...this is created from: o.s.j.d.e.EmbeddedDatabaseFactory with the following parameters:
        Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa')
      • So, it starts an in memory, and then closes it. You have no chance of seeing the data with the H2 console; it has come and gone.
      • So, create a DataSource as follows:
      • You can of course use a properties file to map the parameters, and profiles for different DataSource instances...but I digress.
      • Now, make sure you set the bit that the red arrow in the picture is pointing to, to a location on your computer where a file can be persisted.
      • Running the SpringBatch (Complete project) you should now have a db file in that location after it runs (persisting Person data)
      • Run the Web project you configured previously in these steps, and you WILL :=) see your data, and all the Batch job and step run data (et voila!): Painful but rewarding. Hope it helps you to really BOOTSTRAP :=)
    0 讨论(0)
  • 2021-02-01 03:20

    Selecting Generic H2 (Server) solved for me. We tempted to use default Generic H2 (Embedded) which is wrong.

    0 讨论(0)
  • 2021-02-01 03:21

    It is an old question, but I came across the same problem. Eventually I found out that the default JDBC URL is pointing a test server rather than my application. After correcting it, I could access the right DB.

    I tried with both Generic H2 (Embedded) and the Generic H2 (Server) options, both worked as long as the JDBC URL: is provided correctly.

    In grails 4.0.1 the jdbc URL for development is jdbc:h2:mem:devDb. Check your application.yml file for the exact URL.

    0 讨论(0)
  • 2021-02-01 03:24

    I had the same issue and the answer seems to be really stupid: when you type your database name you shouldn't add ".h2.db" suffix, for example, if you have db file "D:\somebase.h2.db" your connection string should be like "jdbc:h2:file:/D:/somebase". In other way jdbc creates new empty database file named "somebase.h2.db.h2.db" and you see what you see: only system tables.

    0 讨论(0)
提交回复
热议问题