Execute SQL file from Spring JDBC Template

后端 未结 4 914
我在风中等你
我在风中等你 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:34

    We can also achive through SQLExec. Below code is working for me.

    import java.io.File;

    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.taskdefs.SQLExec;
    
    public class Test {
    
        public static void main(String[] args) {
            Test t = new Test();
            t.executeSql("");
        }
    
        private void executeSql(String sqlFilePath) {
            final class SqlExecuter extends SQLExec {
                public SqlExecuter() {
                    Project project = new Project();
                    project.init();
                    setProject(project);
                    setTaskType("sql");
                    setTaskName("sql");
                }
            }
    
            SqlExecuter executer = new SqlExecuter();
            executer.setSrc(new File("test1.sql"));
            executer.setDriver("org.postgresql.Driver");
            executer.setPassword("postgres");
            executer.setUserid("postgres");
            executer.setUrl("jdbc:postgresql://localhost/test");
            executer.execute();
        }
    }
    

提交回复
热议问题