Execute SQL file from Spring JDBC Template

后端 未结 4 970
我在风中等你
我在风中等你 2021-02-05 06:44

I\'m trying to write a bit of code that reads a SQL file (multiple CREATE TABLE statements separated by ;) and executes all the statements.

In

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 07:43

    I've solved the issue this way:

    public void createDefaultDB(DataSource dataSource) {
        Resource resource = new ClassPathResource("CreateDefaultDB.sql");
        ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(resource);
        databasePopulator.execute(dataSource);
    }
    

    You can inject DataSource as usual:

    import javax.sql.DataSource;
    //...
    @Autowired
    private DataSource dataSource;
    

提交回复
热议问题