Home > Enterprise >  Java Spring H2 database: error executing DDL via JDBC statement
Java Spring H2 database: error executing DDL via JDBC statement

Time:01-02

I'm trying to build a H2 database using Spring, but I'm running into errors. I've so far followed the steps from this channel, but I'm running into this error:

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]

(the error continues...)

Model

package com.example.SpringPractice.model;

// imports

@Entity
@Table(name = "courses")
public class Course {

    @Id
    @GeneratedValue
    private Long id;

    private String code;

    private String coursename;
}

Repository

package com.example.SpringPractice.repository;

import com.example.SpringPractice.model.Course;
import org.springframework.data.repository.CrudRepository;

public interface CourseRepository extends CrudRepository<Course, Long> {
}

Dependencies

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

Main

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 statement

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

I've tried changing what's in application.properties, dependencies (javax to jakarta).

CodePudding user response:

Mistake in the SQL statement. Facepalm..

I missed the "caused by" in the error logs which helped answer the problem.

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:

Fixed SQL statement with:

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