0

I am running a spring boot application where I have to run SQL statements that I have written .sql file is saved in the resource directory.

I don't want to run the script on the application startup. I have scheduler which run every 5 hour which does this job

Here is how my folder structure looks like

enter image description here

How can I pick those SQL files in my service class and execute those statements written in the file?

I am very new in spring boot I have used spring data JPA but never have executed SQL statements from the file. How to use Spring JDBC to do that?

How can I load these files in-service classes and execute them?

8

1 Answer 1

0

Try this:

import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.core.io.ClassPathResource;

ScriptUtils.executeSqlScript(connection, new ClassPathResource("sql/info/newInfo.sql"));

Reference.

Sign up to request clarification or add additional context in comments.

3 Comments

How can i use this entityManager.createNativeQuery("sql2").executeUpdate();
@Learners Use it according to where you found it from, if it doesn't work, try to fix it, if it doesn't get fixed, try something else. As simple as that :D
I was more looking to use Spring Data JPA to execute those queries present inside the SQL file. As there are multiple queries present inside that SQL file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.