Java Spring H2数据库:通过JDBC语句执行DDL错误



我正在尝试使用Spring构建H2数据库,但我遇到了错误。到目前为止,我已经遵循了这个通道的步骤,但是我遇到了这个错误:

2022-12-30T22:04:01.054+11:00  WARN 18528 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]

(错误继续…)

模型
package com.example.SpringPractice.model;
// imports
@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue
private Long id;
private String code;
private String coursename;
}

package com.example.SpringPractice.repository;
import com.example.SpringPractice.model.Course;
import org.springframework.data.repository.CrudRepository;
public interface CourseRepository extends CrudRepository<Course, Long> {
}

的依赖性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

主要

package com.example.SpringPractice;
// imports
@SpringBootApplication
public class SpringPracticeApplication {
@Autowired
private CourseRepository courseRepository;
public static void main(String[] args) {
SpringApplication.run(SpringPracticeApplication.class, args);
}
@PostConstruct
private void postInit() {
System.out.println("All courses: " + courseRepository.findAll());
}
}

application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url = jdbc:h2:mem:course-db

SQL语句

INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')

我试过改变什么是在application.properties,依赖关系(javax雅加达)。

SQL语句错误。Facepalm指. .

我错过了"在帮助解决问题的错误日志中。

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO courses VALUES (id, code, coursename) [*]VALUES (1, CS150, Intro to Computer Science)"; SQL statement:

修复SQL语句:

INSERT INTO courses(id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')

最新更新