java Spring Boot,JPA 错误:“通过 JDBC 语句执行 DDL 时出错”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47603204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:42:07  来源:igfitidea点击:

Spring Boot, JPA error: "Error executing DDL via JDBC Statement"

javamysqlspringjpa

提问by Xzenon

I am trying to add an entry in my MySQL database, using a pretty basic (so far) Spring Boot app. I've used some bits and pieces I've found online and this is the code that I'm trying to follow:

我正在尝试使用一个非常基本的(到目前为止)Spring Boot 应用程序在我的 MySQL 数据库中添加一个条目。我使用了一些我在网上找到的零碎部分,这是我试图遵循的代码:

netgloo/spring-boot-samples/spring-boot-mysql-springdatajpa-hibernate

netgloo/spring-boot-samples/spring-boot-mysql-springdatajpa-hibernate

Currently I get the following issues when I run my app.

目前,我在运行我的应用程序时遇到以下问题。

When I first run the app the following Exception is thrown:

当我第一次运行应用程序时,抛出以下异常:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement

And when I make a POST request to the controller, which should create a new entry in the database I get the following Exception:

当我向控制器发出 POST 请求时,它应该在数据库中创建一个新条目,我得到以下异常:

org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

which in turn is caused by the following:

这又是由以下原因引起的:

Caused by: java.sql.SQLException: No database selected

application.properties:

应用程序属性:

# Web pages extension
spring.mvc.view.suffix =.html

# ==============================
# --- DATA SOURCE
# ==============================

# Database url
spring.datasource.url = jdbc:mysql://localhost:3306?useSSL=false

# Database credentials
spring.datasource.username = root
spring.datasource.password = root

# Keep database connection alive
spring.datasource.tomcat.test-while-idle = true
spring.datasource.tomcat.validation-query = SELECT 1

# ==============================
# --- JPA / Hibernate
# ==============================

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

pom.xml:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     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>

<groupId>my.group</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myapp</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20171018</version>
    </dependency>



</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

I have this simple interface:

我有这个简单的界面:

@Transactional
public interface UserDAO extends CrudRepository<User, Long>
{
    public User findByEmail(String email);
}

And here's my controller:

这是我的控制器:

@Controller
public class UserController
{
    @Autowired
    private UserDAO userDAO;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String getRegisterView()
    {
        return "register/registerView";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public @ResponseBody
    ControllerResponse registerRequest(@RequestBody String request)
    {
        User user = null;
        JSONObject json = new JSONObject(request);

        try
        {
            user = new User();

            user.setEmail(json.getString("email"));
            user.setName(json.getString("name"));
            user.setSurname(json.getString("surname"));
            user.setPassword(json.getString("password"));

            userDAO.save(user);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        ControllerResponse response = new ControllerResponse();
        response.setMessage("Controller returned this");
        response.setSuccess(true);
        return response;
    }
}

My User class is defined as follows:

我的 User 类定义如下:

public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    private String email;

    @NotNull
    private String password;

    @NotNull
    private String name;

    @NotNull
    private String surname;

    @Column(name = "authorization_token")
    private String authorizationToken;

    // bunch of getters/setters
}

回答by vsoni

Your datasource url in application.properties is missing database name. Should be like this.

application.properties 中的数据源 url 缺少数据库名称。应该是这样的。

spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useSSL=false