2013-01-21 34 views
4

我有一組SQL文件轉換我的原始數據集。目前,我打開每個文件並執行它。我如何在Java程序中執行每個文件?目標是讓這個過程更加自動化。從Java程序運行SQL文件腳本

我想這樣做SqlScript.execute("myScript.sql");

注意這些SQL腳本在一個數據庫上採取行動。我假設我將不得不傳遞某種連接字符串。我正在使用MySQL。

  1. 什麼對象,庫,包等...我需要在Java內執行此操作嗎?
+0

結賬的java.sql API http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/package-summary.html – sasankad

+0

重複的http://stackoverflow.com/questions/1044194/running-a-sql-script-using-mysql-with-jdbc –

回答

9

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(); 
+0

哪裏可以買到這個罐子? – CodeKingPlusPlus

+0

@CodeKingPlusPlus http://code.google.com/p/mybatis/downloads/list?can=1或google'ibatis',有足夠的資源您需要。 –

+0

有關資源參數的任何建議? Netbeans告訴我它找不到資源。我正在把文件放在我通常所做的地方。 – CodeKingPlusPlus

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; 
} 
+0

我試着用這個,在windows下工作正常,但是在linux中總是用退出碼1結束,它與linux兼容? –

4

使用iBatics會更容易。

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

此外,你需要MySQL java driver:com.mysql.jdbc.Driver可以在MySQL網站上找到。

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); 
     } 
    } 
} 
+1

感謝您指出這一點。使用MyBatis非常重要,因爲iBATIS在Apache軟件基金會退役。我用MyBatis 3.2.6試了一下,它工作。 :-) –