我正在使用带有Spring Boot(2.3.3.RELEASE版本)的H2数据库,并具有H2数据库的所有默认设置。
这是我的应用程序中的所有文件。
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);
}
}
在我尝试使用以下凭据连接H2数据库(使用所有默认配置由springboot配置和启动)启动应用程序后,
我说错了
找不到数据库“ mem:testDB”,或者预先创建它或允许远程 数据库创建
如何使用所有默认凭据连接到Spring Boot配置和启动的H2数据库。
除application.properties
之外,我不想覆盖spring.h2.console.enabled=true
文件中的任何配置。
答案 0 :(得分:0)
在较新版本的Spring Boot(2.2+)中,在控制台上查找以下日志消息:使用JDBC URL在/h2-console
页面上进行连接:
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'
答案 1 :(得分:-1)
仅启用控制台是不够的,您还需要提及要连接的数据库。如果您要连接内存数据库,请添加以下属性,然后尝试使用相同的凭据连接内存数据库
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=