2013-02-04 148 views
1

我使用Java with MySQL(JDBC),並且我想將轉儲文件導入數據庫。什麼是正確的方法來做到這一點? 我曾嘗試下面的代碼:將轉儲文件導入到mysql JDBC

// function "connectToDB" connects to the Database, and not the server. 
// variable sourcePath refers to the dumpfile. 
    Connection con = connectToDB(USERNAME, PASSWORD); 
    String q = "source " + sourcePath; 
    System.out.println("Q is: " + q); 
    try { 
     Statement statement = con.createStatement(); 
     statement.executeUpdate(q); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    closeConnection(con); 

,但我得到一個MySQLSyntaxErrorException:

您的SQL語法錯誤;檢查 對應於你的MySQL服務器版本正確的語法使用 接近「源C:\桌面\ dumpfile.sql」手動位於第1行

+0

更改executeUpdate執行查詢,看它是否工作 – Satya

+0

不,我只是測試「executeQuery」和「執行」,但他們都給了我完全相同的錯誤。 –

+1

如果您使用'mysqldump'程序生成mySQL轉儲文件,加載它的最簡單方法是使用'mysqlimport'程序。 – DwB

回答

4

感謝大家對他們的幫助,閱讀他們的想法,我終於進口dumpfile.sql 因此,如果任何人有同樣的問題,爲我工作的樣本代碼是這樣的:

Connection con = connectToDB(USERNAME, PASSWORD); 
/* Note that con is a connection to database, and not the server. 
if You have a connection to the server, the first command in the dumpfile should be the 
USE db_name; */ 
String q = ""; 
File f = new File(sourcePath); // source path is the absolute path of dumpfile. 
try { 
    BufferedReader bf = new BufferedReader(new FileReader(f)); 
     String line = null; 
     line = bf.readLine(); 
     while (line != null) { 
      q = q + line + "\n"; 
      line = bf.readLine(); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
// Now we have the content of the dumpfile in 'q'. 
// We must separate the queries, so they can be executed. And Java Simply does this: 
String[] commands = q.split(";"); 

try { 
    Statement statement = con.createStatement(); 
    for (String s : commands) { 
     statement.execute(s); 
    } 
} catch (Exception ex) { 
} 
closeConnection(con); 

編輯:添加connectToDB功能:

private Connection connectToDB(String username, String password) { 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     String url = "jdbc:mysql://localhost:3306/" + DATABASE; 
     Properties objProperties = new Properties(); 
     objProperties.put("user", username); 
     objProperties.put("password", password); 
     objProperties.put("useUnicode", "true"); 
     objProperties.put("characterEncoding", "utf-8"); 

     Connection con = DriverManager.getConnection(url, objProperties); 
     return con; 
    } catch (Exception ex) { 
     System.out.println("Connection to sql database failed."); 
     ex.printStackTrace(); 
     return null; 
    } 
} 
+0

你可以給connectToDB方法提供更多信息。它是如何完成的? – Lealo

+1

有你去:) –

1

,因爲它是在錯誤中列出的SQL statsement,您要執行以下查詢

source C:...\Desktop\dumpfile.sql 

上面沒有有效的SQL語句,因此它給你1號線。 錯誤您需要打開文件,其中包含SQL,然後用其身體

q 
+0

這可能有助於http://www.coderanch。com/t/306966/JDBC/databases/Execute-sql-file-java –

+0

你的想法確實幫了很大忙。謝謝。我剛剛添加完整的答案。 (請注意,在使用q之前,我們應該分開查詢,否則我們將面臨錯誤。) –

2

你需要用空命令字符串

  • 單獨運行每個語句並刪除評論

    • 開始閱讀每一行
    • 修剪線
    • 放棄與開始的 -
    • 將行添加到您的命令字符串
    • 如果行結束;運行命令,並重復步驟1
  • +0

    謝謝。我實現了這樣的代碼並將其放入答案中。它起初看起來相當棘手,但它是完美的解決方案:) –

    4

    我實際使用@Makan Tayebi自己的答案,但我覺得一些改進可以被製造。 如果轉儲文件大小過大,可能會出現第一個問題,因爲此方法不是最佳方法。 如果表中的數據包含特殊字符';',則可能發生第二個問題。在數據的內部,將讀入的文件拆分爲字符串;也會分裂這個;並會發生異常。 現在,這是我的解決方案。剛編輯他:

    Connection con = connectToDB(USERNAME, PASSWORD); 
    /* Note that con is a connection to database, and not the server. 
    if You have a connection to the server, the first command in the dumpfile should be the 
    USE db_name; */ 
    //String q = ""; 
         try { 
          File f = new File(path); // source path is the absolute path of dumpfile. 
    
          BufferedReader bf = new BufferedReader(new FileReader(f)); 
          String line = null,old=""; 
          line = bf.readLine(); 
          while (line != null) { 
           //q = q + line + "\n"; 
           if(line.endsWith(";")){ 
            stmt.executeUpdate(old+line); 
            old=""; 
           } 
           else 
            old=old+"\n"+line; 
           line = bf.readLine(); 
          } 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
    closeConnection(con); 
    

    此代碼假定SQL轉儲使用mysqldump,或每個語句結束後,打破行任何其他程序創建的。

    +0

    謝謝你的努力。另外,人們應該考慮一條線可能以評論或空白區結束。 –

    +1

    多數民衆贊成我提到的原因,我假設轉儲文件是由mysqldump生成的,我使用這種方法從mysqldump和SQLYog的轉儲,這是完全正確的工作,除了我發現一些轉儲在單個事務,所以在這種情況下,您的方法和我的方法都會失敗,因此,我們應該讀取整個文件並在單個查詢中執行它。 –