简体   繁体   中英

Default configuration of H2 in-memory database set up by Springboot

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-控制台

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 .

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'

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=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM