Spring Boot – Loading Data Using a SQL Script

This post demonstrates how loading data from an SQL script for a particular database platform. This is ideal for development stage when you need some seed data to fill in existing database tables.

Spring Boot Application

Let’s create a new Spring Boot Application using IntelliJ (optional) and Spring Initialzr (or http://start.spring.io/).

Step 1: Create a New Project

Choose Spring Initialzr. Then click Next.

Step 2: Provide project information

Step 3: Choose JPA and H2

Step 4: Save the new project

Step 5: Create an entity class

Step 5: Modify application.properties

Update the application.properties file with the following value.

With this setting, Spring will look for an SQL script data-h2.sql in the src/main/java/resources/ directory. Then, it will execute all the SQL statements in the script.

If we had

Spring will try to load an SQL script named data-hsqldb.sql

For more information, please visit https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

Step 6: Unit Test

Lastly, we test our codes.

Download the codes



Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert