簡體   English   中英

將轉儲文件導入到mysql JDBC

[英]import a dump file to mysql JDBC

我正在將Java與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服務器版本對應的手冊以獲取正確的語法,以在第1行的'source C:... \\ Desktop \\ dumpfile.sql'附近使用

感謝大家的幫助,閱讀他們的想法,我終於導入了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;
    }
}

我實際上使用@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);

此代碼假定使用mysqldump或任何其他在每個語句結束后換行的程序創建sql dump。

您需要分別運行每個語句並刪除注釋

  • 以空命令字符串開頭
  • 閱讀每一行
  • 修邊線
  • 丟棄以-開頭的
  • 在命令字符串中添加行
  • 如果行以結尾; 運行命令並重復步驟1

由於它在SQL陳述式錯誤中列出,因此您嘗試執行以下查詢

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

上面的代碼不是有效的SQL語句,因此在第1行會給您錯誤。您需要打開包含SQL的文件,然后將其主體用作

q

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM