1

I am using H2 database with Spring Boot (version 2.3.3.RELEASE) with all default settings for H2 database.

Here are the all files of my application.

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

application.properties

spring.h2.console.enabled=true

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.sql.SQLException;

@SpringBootApplication
public class App {
public static void main(String[] args) {
    SpringApplication.run(App.class, args);
 }

}

After starting the application when i am trying to connect the H2 database (configured and started by springboot with all default configuration) using below credentials,

H2-Console

I am getting error saying

Database "mem:testDB" not found, either pre-create it or allow remote database creation

How can I connect to H2 database configured and started by Spring Boot with all the default credentials.

I don't want to override any configuration in application.properties files except spring.h2.console.enabled=true.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
kverma28
  • 57
  • 1
  • 13
  • 1
    Try lowercase: `jdbc:h2:mem:testdb` – Selindek Sep 19 '20 at 18:22
  • what's the spring.datasource.url value you have supplied, based on that it will configure the in memory db – Soni Sep 19 '20 at 18:26
  • I haven't supplied any property like "spring.datasource.url". I want to let spring configure the DB with default configuration. @Selindek : Also i tried with "jdbc:h2:mem:testdb" all in lower case but still getting the same error – kverma28 Sep 19 '20 at 18:57
  • try jdbc:h2:~/test because h2 creates a test db under your Users directory automatically. Check out the quickstart: https://www.h2database.com/html/quickstart.html – Onur Baştürk Sep 19 '20 at 20:42
  • Does this answer your question? [spring boot default H2 jdbc connection (and H2 console)](https://stackoverflow.com/questions/24655684/spring-boot-default-h2-jdbc-connection-and-h2-console) – flaxel Sep 19 '20 at 21:21

2 Answers2

2

In newer version of Spring Boot (2.2+), look for the following log message on your console: Use the JDBC URL to connect on /h2-console page:

Spring Boot 2.2+:

INFO H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'

Spring Boto 2.3+:

INFO H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:621dd224-01db-4137-807f-b9c3046de64d'

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
-1

Only enabling console wouldn't be sufficient, you also need to mention which db you want connect. In your case if you want connect in memory db add below properties as well, then try to connect your in memory db using same creds

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Mayuresh
  • 11
  • 3