springboot h2无法创建数据库并无法登录到h2-console

时间:2019-09-05 15:25:09

标签: java spring spring-boot h2

我正在使用Spring Boot和Maven构建我的用户Onlineshop。我想访问我的h2-console数据库 onlineshop ,但是我无法使用默认的所有内容登录。此外,未创建数据库 onlineshop 。自从使用嵌入式tomcat以来,Spring Boot不会引发任何错误。 预先感谢您的帮助。

这是我的依赖

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>runtime</scope>

application.properties

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:onlineshop

错误

enter image description here

2 个答案:

答案 0 :(得分:1)

application.properties

中设置以下属性
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:onlineshop;IFEXISTS=FALSE;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.path=/h2-console

IFEXISTS

  

默认情况下,当应用程序调用DriverManager.getConnection(url,   ...)具有嵌入的URL,并且URL中指定的数据库没有   还存在,则创建一个新的(空)数据库。在某些情况下   最好限制创建新数据库,只允许打开   现有数据库。为此,向数据库添加; IFEXISTS = TRUE   网址。在这种情况下,如果数据库尚不存在,则   尝试连接时引发异常

答案 1 :(得分:0)

我通过简单地降级com.h2database

来解决了这个问题
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.170</version>
        <scope>runtime</scope>
    </dependency>