Spring Boot,JPA错误:“通过JDBC语句执行DDL错误" [英] Spring Boot, JPA error: "Error executing DDL via JDBC Statement"

查看:123
本文介绍了Spring Boot,JPA错误:“通过JDBC语句执行DDL错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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

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:

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:

<?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>

我有这个简单的界面:

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

这是我的控制器:

@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;
    }
}

我的用户类定义如下:

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
}

推荐答案

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

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

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

这篇关于Spring Boot,JPA错误:“通过JDBC语句执行DDL错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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