127

I am simply trying to see the H2 database content for an embedded H2 database which spring-boot creates when I don't specify anything in my application.properties and start with mvn spring:run. I can see hibernate JPA creating the tables but if I try to access the h2 console at the URL below the database has no tables.

http://localhost:8080/console/

I see suggestions like this one: View content of embedded H2 database started by Spring

But I don't know where to put the suggested XML in spring-boot and even if I did, I don't want the h2console to be available anymore when an external database is configured so it is more likely that I need to handle this with some kind of conditional code (or maybe just allow spring to automatically handle it in the most ideal case where I only include H2 when a maven profile is activated).

Does anyone have some sample code showing how to get the H2 console working in boot (and also the way to find out what the jdbc connection string that spring is using is)?

Community
  • 1
  • 1
Aaron Zeckoski
  • 5,016
  • 8
  • 25
  • 48

14 Answers14

131

This is how I got the H2 console working in spring-boot with H2. I am not sure if this is right but since no one else has offered a solution then I am going to suggest this is the best way to do it.

In my case, I chose a specific name for the database so that I would have something to enter when starting the H2 console (in this case, "AZ"). I think all of these are required though it seems like leaving out the spring.jpa.database-platform does not hurt anything.

In application.properties:

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

In Application.java (or some configuration):

@Bean
public ServletRegistrationBean h2servletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/console/*");
    return registration;
}

Then you can access the H2 console at {server}/console/. Enter this as the JDBC URL: jdbc:h2:mem:AZ

Aaron Zeckoski
  • 5,016
  • 8
  • 25
  • 48
  • 1
    Using `new WebServlet()` give me issues. Which class is this importing for you? It's pulling in javax.servlet.annotation.WebServlet for me as the only option and it is just an interface. – Splaktar Nov 26 '14 at 22:35
  • 5
    Oh I got it, org.h2.server.web.WebServlet is the one that is needed. My project wasn't in sync with Gradle to pull in the new h2 libraries. – Splaktar Nov 26 '14 at 23:16
  • 25
    The application.properties pieces aren't needed. You can just use the Application.java part that you put, then connect to `jdbc:h2:mem:testdb` with a blank username and blank password. http://localhost:8082/ works with this setup. – Splaktar Nov 26 '14 at 23:28
  • 2
    @Splaktar Thank you! I was going nuts trying to figure out how to connect and the missing piece of the puzzle was to use "testdb" for the database name. – nerdherd May 13 '15 at 17:53
  • 1
    @Splaktar - you should make your comment the answer. Like you said the source code for [EmbeddedDatabaseConnection](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDatabaseConnection.java) shows all the default Embedded DB Connection URIs – karthik m Nov 19 '15 at 04:58
  • Using Springboot 1.4.2 it worked just like @azeckoski said, only that I didn't have to add the ServletRegistrationBean. Even better! Thanks a lot – James Freitas Dec 28 '16 at 11:11
60

As of Spring Boot 1.3.0.M3, the H2 console can be auto-configured.

The prerequisites are:

  • You are developing a web app
  • Spring Boot Dev Tools are enabled
  • H2 is on the classpath

Even if you don't use Spring Boot Dev Tools, you can still auto-configure the console by setting spring.h2.console.enabled to true

Check out this part of the documentation for all the details.

Note that when configuring in this way the console is accessible at: http://localhost:8080/h2-console/

Sinisha Mihajlovski
  • 1,811
  • 1
  • 20
  • 34
geoand
  • 60,071
  • 24
  • 172
  • 190
  • Or as explained in the doc you enable it with spring.h2.console.enabled=true. With the prerequisites it's enabled automatically. – keiki Oct 31 '15 at 09:38
  • 1
    What does _You are developing a web app_ exactly mean? – garci560 May 08 '17 at 13:30
  • In the context of Spring Boot, it means that you have added `spring-boot-starter-web` as a dependency – geoand May 09 '17 at 10:30
  • 1
    Also I think when you open http://localhost:8080/h2-console/ you need to write jdbc:h2:mem:testdb in jdbc url to see your tables. And in url http://localhost:8080/h2-console/ after localhost you need to specify port of application. – anujprashar May 17 '18 at 13:00
  • 3
    @anujprashar I am pretty sure `jdbc:h2:mem:testdb` is for the connection URL, not for the URL were the console is accessible – geoand May 17 '18 at 13:27
  • ok. When I opened localhost:8080/h2-console and I clicked connect button without changing jdbc url I was not able to see my table. Next time when I opened url localhost:8080/h2-console i entered jdbc:h2:mem:testdb in jdbc url in dialog and I was able to see tables. – anujprashar May 17 '18 at 13:37
  • IMPORTANT use the default username `sa` with blank password. – David Nov 23 '18 at 14:31
46

I have found a nice tutorial about this topic:

https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

Basically the correct JDBC URL for me was: jdbc:h2:mem:testdb

Krzysztof Kaczor
  • 5,408
  • 7
  • 39
  • 47
  • 1
    dumb question but this seems to be the default JDBC URL because the setting spring.datasource.name defaults to testdb. Why if thats the case if I change the db name to something like foodb is the jdbc still the same and not jdbc:h2:mem:foodb – Dan Vega Nov 23 '15 at 03:04
  • 2
    This jdbc url jdbc:h2:mem:testdb is correct. It helped me finally connect to the default database created by spring data jpa. – Kamal Joshi Dec 19 '16 at 02:16
  • You can set the db name as anything such as: spring.datasource.url=jdbc:h2:mem:test; in application.properties – Aleksei Maide Jul 14 '18 at 09:20
26

From http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

H2 Web Console (H2ConsoleProperties):

spring.h2.console.enabled=true //Enable the console.
spring.h2.console.path=/h2-console //Path at which the console will be available.

Adding the above two lines to my application.properties file was enough to access the H2 database web console, using the default username (sa) and password (empty, as in don't enter a password when the ui prompts you).

mancini0
  • 4,285
  • 1
  • 29
  • 31
  • 1
    Shouldn't be `spring.h2.console.enabled=true`? False will disable it. And `spring.h2.console.path=/h2-console` is redundant because `/h2-console` is the default path from Spring Boot. As per documentation "By default the console will be available at /h2-console. You can customize the console’s path using the spring.h2.console.path property." More details here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-sql-h2-console – georger Apr 05 '17 at 20:53
  • I included the latter property to show OP where he can access the console. Obviously spring.h2.console.enabled.enabled=false will disable the console, the point is that this is configurable as a property. I'll set that property to true for clarity. – mancini0 Apr 06 '17 at 18:10
21

A similar answer with Step by Step guide.

  1. Add Developer tools dependency to your pom.xml or build.gradle

Maven

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Gradle

dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
}
  1. Access the db from http://localhost:8080/h2-console/
  2. Specify jdbc:h2:mem:testdb as JDBC URL
  3. You should see the entity you specified in your project as a table.
biniam
  • 8,099
  • 9
  • 49
  • 58
21

For Spring Boot 2.1.1 straight from Spring Initialzr:

  1. Default with devtools is http://127.0.0.1:8080/h2-console/

    • POM: spring-boot-starter, h2, spring-boot-starter-web, spring-boot-devtools
  2. Without devtools - you need to set it in properties: spring.h2.console.enabled=true spring.h2.console.path=/h2-console

    • POM: spring-boot-starter, h2, spring-boot-starter-web

Once you get there - set JDBC URL: jdbc:h2:mem:testdb (The default one will not work)

Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
17

I had only below properties in /resources/application.properties. After running spring boot, using this URL(http://localhost:8080/h2-console/), the table in H2 console was visible and read to view the table data, also you can run simple SQL commands. One thing, in your java code, while fetching data, the column names are upper-case, even though schema.sql is using lower-case names :)

spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
rak22
  • 338
  • 4
  • 13
16

Check spring application.properties

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

here testdb is database defined Make sure h2 console have same value while connecting other wise it will connect to default db

enter image description here

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
14

For Spring Boot 2.3.3.RELEASE straight from Spring Initialzr:

POM: data jpa, h2, web

application properties: spring.h2.console.enabled=true

When you run the application look for line like below in the run console:

2020-08-18 21:12:32.664  INFO 63256 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:eaa9d6da-aa2e-4ad3-9e5b-2b60eb2fcbc5'

Now use the above JDBC URL for h2-console and click on Connect.

RahulDeep Attri
  • 398
  • 3
  • 13
10

If you use Spring Boot's developer tools, it comes with H2 Console enabled by default. It can be accessed from /h2-console/. On the login interface, for input JDBC URL use value jdbc:h2:mem:testdb. Pay attention to mem string.

If you don't use Spring Boot's developer tools, you can enable the console in application.properties using spring.h2.console.enabled=true. This will enable console under /h2-console. If you want to change the URL then you can add another entry with spring.h2.console.path=my_console_path.

The default schema name is testdb.

More details in Spring Boot Documentation.

georger
  • 1,568
  • 21
  • 24
9

In order to get the tables all you need to do is create 2 sql files schema.sql(for table creation) and data.sql(data for the created tables). These files to be put in src/main/resources folder. Spring boot auto detects them and takes care of the rest during runtime.

If your using more than 2 DB in your project ensure to use specific files like (schema-h2.sql -- for h2 DB , schema-oracle.sql -- for oracle DB). The same to be followed for data.sql too.

Also ensure that you drop tables by adding drop table statement in your schema.sql as first statement. To avoid appending of duplicate records.

The link for spring boot is here.

My application.properties is as follows.

spring.datasource.url=jdbc:h2:~/file/Shiva;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.initialize=true 
spring.error.whitelabel.enabled=true
spring.h2.console.path=/console
spring.datasource.continue-on-error=true
spring.jpa.hibernate.ddl-auto=create
spring.hibernate.hbm2ddl.auto=update
spring.hibernate.show_sql=true

You can follow the steps in the below link.

https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
kemparaj565
  • 379
  • 3
  • 6
3

I found that with spring boot 2.0.2.RELEASE, configuring spring-boot-starter-data-jpa and com.h2database in the POM file is not just enough to have H2 console working. You must configure spring-boot-devtools as below. Optionally you could follow the instruction from Aaron Zeckoski in this post

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
 </dependency>
3

Use jdbc:h2:mem:testdb as your path when logging into the H2 console.

Obviously if you have altered Spring Boot properties your datasource may be different, but it seems like you're struggling with how to find the default. That's all there is to it! You'll see your schema after logging in to H2.

1

I had made a very stupid mistake when I had this same problem. I had added H2 DB for running unit test cases and hence I had set the scope to test in pom.xml. While running the application using mvn spring:run I removed the scope and it works fine now.

AbhishekB
  • 2,111
  • 3
  • 18
  • 23