4

元のデータセットを変換する一連の SQL ファイルがあります。現在、各ファイルを開いて実行しています。Java プログラム内で各ファイルを実行するにはどうすればよいですか? 目標は、このプロセスをより自動化することです。

私は次のようなことをしたいと思いますSqlScript.execute("myScript.sql");

これらの SQL スクリプトは、1 つのデータベースで動作することに注意してください。ある種の接続文字列を渡す必要があると思います。私はMySQLを使用しています。

  1. Java内でこれを実行するために必要なオブジェクト、ライブラリ、パッケージなどは何ですか?
4

3 に答える 3

12

Ibatis は、あなたを助けるScriptRunnerを提供します。参照できる簡単なコード スニペット:

Connection conn=getConnection();//some method to get a Connection
ScriptRunner runner=new ScriptRunner(conn, false, false);
InputStreamReader reader = new InputStreamReader(new FileInputStream("foo.sql"));
runner.runScript(reader);
reader.close();
conn.close();
于 2013-01-21T03:58:57.550 に答える
4

iBatics を使用するとより簡単になります。

http://repo1.maven.org/maven2/org/mybatis/mybatis/3.2.3/mybatis-3.2.3.jar

さらに、mysql サイトにあるcom.mysql.jdbc.DriverというMySQL Java ドライバーが必要です。

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.DriverManager;

import org.apache.ibatis.jdbc.ScriptRunner;

public class Main {
    public static void main(String[] args) {

        String script = "scriptname.sql";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            new ScriptRunner(DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mysql", "root", "root`"))
                    .runScript(new BufferedReader(new FileReader(script)));
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}
于 2013-10-12T02:28:18.473 に答える
3

次のようなものを試すことができます: http://www.tonyspencer.com/2005/01/20/execute-mysql-script-from-java/

public static String executeScript (String dbname, String dbuser,
        String dbpassword, String scriptpath, boolean verbose) {
    String output = null;
    try {
        String[] cmd = new String[]{"mysql",
            dbname,
            "--user=" + dbuser,
            "--password=" + dbpassword,
            "-e",
            "\"source " + scriptpath + "\""
            };
        System.err.println(cmd[0] + " " + cmd[1] + " " +
        cmd[2] + " " + cmd[3] + " " +
        cmd[4] + " " + cmd[5]);
        Process proc = Runtime.getRuntime().exec(cmd);
        if (verbose) {
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            // read the output
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                System.out.println(line);
            }

            // check for failure
            try {
                if (proc.waitFor() != 0) {
                    System.err.println("exit value = " +
                    proc.exitValue());
                }
            }
            catch (InterruptedException e) {
                System.err.println(e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output;
}
于 2013-01-21T03:01:26.447 に答える