CodeKingPlusPlus CodeKingPlusPlus - 9 months ago 65
MySQL Question

Running SQL files scripts from a Java program

I have a set of SQL files that transform my original dataset. Currently, I open each file and execute it. How can I execute each file inside a Java program? The goal is to make this process a lot more automatic.

I would like to do something like


NOTE these SQL scripts act on one database. I assume I would have to pass some kind of connection string. I am using MySQL.

  1. What objects, libraries, packages, etc... do I need to perform this inside Java?


Ibatis provides a ScriptRunner that will help you. Simple code snippets you can refer:

Connection conn=getConnection();//some method to get a Connection
ScriptRunner runner=new ScriptRunner(conn, false, false);
InputStreamReader reader = new InputStreamReader(new FileInputStream("foo.sql"));