英文:
Default configuration of H2 in-memory database set up by Springboot
问题
我正在使用带有 Spring Boot 的 H2 数据库(版本为 2.3.3.RELEASE),使用了 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 数据库(由 Spring Boot 配置和启动,使用所有默认配置)时,
我收到错误消息
数据库 "mem:testDB" 未找到,请预先创建它或允许远程数据库创建。
如何使用所有默认凭据连接由 Spring Boot 配置和启动的 H2 数据库。
我不想覆盖 application.properties
文件中的任何配置,除了 spring.h2.console.enabled=true
。
英文:
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,
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
.
答案1
得分: 2
在较新版本的Spring Boot(2.2+)中,在您的控制台上查找以下日志消息:在/h2-console
页面上使用JDBC URL连接:
Spring Boot 2.2+:
INFO H2ConsoleAutoConfiguration : H2控制台位于'/h2-console'。数据库位于'jdbc:h2:mem:testdb'
Spring Boto 2.3+:
INFO H2ConsoleAutoConfiguration : H2控制台位于'/h2-console'。数据库位于'jdbc:h2:mem:621dd224-01db-4137-807f-b9c3046de64d'
英文:
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'
答案2
得分: -1
仅仅启用控制台是不够的,你还需要说明你想连接哪个数据库。在你的情况下,如果你想连接内存数据库,在下面也添加以下属性,然后尝试使用相同的凭据连接你的内存数据库。
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
英文:
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=
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论