0

I have a small query regarding the Spring boot logic of calling the sql files. I know that the schema sql files are called automatically if they are kept inside the resource directory of the spring boot app project. But i would like to call them from a different directory. I have written the below code to try and achieve that but fails with below error message.

Code :

public void executeSqlScript()throws SQLException{
  Connection connection = dataSource.getConnection();
  connection.setAutoCommit(false);
  ScriptUtils.executeSqlScript(connection, new EncodedResource(new ClassPathResource("C:\\react-file-upload-master\\createConfigurationTableAndData.sql")));
  connection.commit();
}

Error Message : java.io.FileNotFoundException: class path resource [C:/react-file-upload-master/createConfigurationTableAndData.sql] cannot be opened because it does not exist.

Its is not reading my directory. Would be greatfull if you could educate me on the same. Thank you.

1 Answer 1

1

try using FileSystemResource instead of ClassPathResource and give absolute path.

public void executeSqlScript()throws SQLException{
  Connection connection = dataSource.getConnection();
  connection.setAutoCommit(false);
  ScriptUtils.executeSqlScript(connection, new FileSystemResource("C:\\react-file-upload-master\\createConfigurationTableAndData.sql"));
  connection.commit();
}

because ClassPathResource will point to class path only. assuming your file is not in classpath.

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

1 Comment

Yea i got to know my mistake and trying to change the logic. I will update you once i try the same. Thank you!!

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.