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
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();
}
}