Vimal Bhaskar
Vimal Bhaskar

Reputation: 778

Spring boot call to a Sql file

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.

Upvotes: 0

Views: 276

Answers (1)

Binu
Binu

Reputation: 784

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.

Upvotes: 1

Related Questions