数据库"C:/数据/样本"找不到,并且IFEXISTS = true,因此我们无法自动创建它-Spring Boot中的错误 [英] Database "C:/data/sample" not found, and IFEXISTS=true, so we cant auto-create it - Error in Spring Boot

查看:163
本文介绍了数据库"C:/数据/样本"找不到,并且IFEXISTS = true,因此我们无法自动创建它-Spring Boot中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Spring Boot应用程序以将h2数据库与其连接.这样做时,将引发错误,显示未找到数据库.请为我提供可以实施和解决该问题的解决方案.

I have created a spring boot application to connect h2 database with it. While doing so, it throws an error showing Database not found. Please help me with the solution which I can implement and resolve the issue.

我在pom.xml文件中添加了com.h2database依赖项,然后它也给出了错误.

I have added com.h2database dependency in the pom.xml file, then also it gives the error.

下面是我的pom.xml文件和application.properties文件

Below is my pom.xml file and application.properties file

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <groupId>com.example</groupId>
    <artifactId>santanderdbproj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>firstproject</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>


    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>

    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
</project>

application.properties

spring.datasource.url = jdbc:h2:file:C:/data/sample
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2

我已在下面附加错误图像,请参考它.

I have attached the error image below, please refer to it.

推荐答案

这是一条不正确的错误消息,仅出现在1.4.198和1.4.199中.下一版本的H2(1.4.200)将显示更好的消息,例如

This is an incorrect error message that appears only in 1.4.198 and 1.4.199. The next version of H2 (1.4.200) will show a better message like

找不到数据库…,请预先创建它或允许远程创建数据库(在安全环境中不建议使用)

Database … not found, either pre-create it or allow remote database creation (not recommended in secure environments)

您看到此错误消息,因为您的数据库尚不存在.解决问题的通常方法是先使用应用程序创建数据库,然后再尝试使用H2 Console登录数据库.

You see this error message because your database doesn't exist yet. The normal way to fix your problem is to create the database first with your application before trying to login into it with H2 Console.

可以将H2控制台配置为允许创建数据库,但是它可能会在您的系统中创建一个安全漏洞,任何可以打开此页面的人都可以在这种配置下以您的访问权限对您的系统进行任何操作.

H2 Console can be configured to allow database creation, but it may create a security hole in your system, anyone who can open this page may do anything with your system with yours access permissions in such configuration.

请注意,在H2从系统托盘中的图标启动的浏览器会话中的H2控制台(您可以简单地将h2-1.4.199 jar作为Java应用程序启动,或使用java -jar h2-1.4.199.jar)授予您创建新数据库的权限以安全的方式.您可以将其用于此目的.如果您不打算将其用作TCP服务器,请在创建数据库后从系统任务栏图标中将其关闭,以确保它不保留您的数据库.您还可以使用命令行命令行管理程序工具: https://h2database.com/html/tutorial.html#creating_new_databases

Note that H2 Console in browser's session launched by H2 from its icon in the system tray (you can simply launch the h2-1.4.199 jar as Java application, or use java -jar h2-1.4.199.jar) gives you permission to create a new database in secure way. You can use it for that purpose. If you're not planning to use it as a TCP Server, close it from the system tray icon after database creation to make sure that it doesn't hold your database. You can also use the command-line Shell tool: https://h2database.com/html/tutorial.html#creating_new_databases

Spring的H2控制台没有提供此功能.

H2 Console from the Spring doesn't ship with such feature.

H2的较早版本(最高1.4.197)还允许创建数据库,包括Spring的控制台,但同样会在您的系统中创建一个安全漏洞.

Older versions of H2 (up to 1.4.197) also allow the database creation, including the console from the Spring, but, again, it creates a security hole in you system.

这篇关于数据库"C:/数据/样本"找不到,并且IFEXISTS = true,因此我们无法自动创建它-Spring Boot中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆