英文:
org.hibernate.tool.schema.spi.CommandAcceptanceException trying to connect a MYSQL database
问题
I'm trying to connect a MySQL database to my Java Spring Boot project, but I got an org.hibernate.tool.schema.spi.CommandAcceptanceException
error. I've already tried changing spring.jpa.hibernate.ddl-auto=update
to spring.jpa.hibernate.ddl-auto=update
, but it's still not working.
错误:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table to_do (id integer not null, desc varchar(255), done bit not null, target_date date, username varchar(255), primary key (id)) engine=InnoDB" via JDBC [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(255), done bit not null, target_date date, username varchar(255), p' at line 1]
属性:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework=info
logging.level.com.pao.springboot.myFirstWebApp=debug
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/todos
spring.datasource.username=todos-user
spring.datasource.password=dummytodos
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>glassfish-jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
实体类:
package com.pao.springboot.myFirstWebApp.toDo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.time.LocalDate;
// 数据库
// 待办事项列表
@Entity(name = "ToDo")
public class ToDo {
@Id
@GeneratedValue
private int id;
private String username;
private String desc;
private LocalDate targetDate;
private boolean done;
public ToDo() {}
public ToDo(int id, String username, String desc, LocalDate targetDate, boolean done) {
this.id = id;
this.username = username;
this.desc = desc;
this.targetDate = targetDate;
this.done = done;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public LocalDate getTargetDate() {
return targetDate;
}
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
@Override
public String toString() {
return "ToDo{" +
"id=" + id +
", username='" + username + '\'' +
", desc='" + desc + '\'' +
", targetDate=" + targetDate +
", done=" + done +
'}';
}
}
希望这可以帮助你,谢谢阅读。我已经尝试将 spring.jpa.hibernate.ddl-auto
更改为 spring.jpa.hibernate.ddl-auto=update
。
英文:
Im trying to connect a MySQL database to my java springboot project, but i got a org.hibernate.tool.schema.spi.CommandAcceptanceException error, i already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update, and its still wrong.
Error:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table to_do (id integer not null, desc varchar(255), done bit not null, target_date date, username varchar(255), primary key (id)) engine=InnoDB" via JDBC [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(255), done bit not null, target_date date, username varchar(255), p' at line 1]
Properties:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp logging.level.org.springframework=info logging.level.com.pao.springboot.myFirstWebApp=debug
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/todos spring.datasource.username=todos-user spring.datasource.password=dummytodos spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>glassfish-jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>`
Entity:
ackage com.pao.springboot.myFirstWebApp.toDo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.time.LocalDate;
//Database
// List of todos
@Entity(name = "ToDo")
public class toDo {
@Id
@GeneratedValue
private int id;
private String username;
private String desc;
private LocalDate targetDate;
private boolean done;
public toDo(){}
public toDo(int id, String username, String desc, LocalDate targetDate, boolean done) {
this.id = id;
this.username = username;
this.desc = desc;
this.targetDate = targetDate;
this.done = done;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public LocalDate getTargetDate() {
return targetDate;
}
public void setTargetDate(LocalDate targetDate) {
this.targetDate = targetDate;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
@Override
public String toString() {
return "toDo{" +
"id=" + id +
", user='" + username + '\'' +
", desc='" + desc + '\'' +
", targetDate=" + targetDate +
", done=" + done +
'}';
}
}
I hope someone can help me, thanks to read me.
I already tried to change spring.jpa.hibernate.ddl-auto=update to spring.jpa.hibernate.ddl-auto=update.
答案1
得分: 1
@Column(name = "description")
private String desc;
import jakarta.persistence.Column;
英文:
The cause is simple - DESC
is a reserved keyword in MySQL (and in SQL in general), used to specify descending order, so you may want to name your column explicitly:
@Column(name = "description")
private String desc;
UPD:
please don't forget to import corrent @Column:
import jakarta.persistence.Column;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论