1

我正在将 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' 附近使用正确的语法

4

4 回答 4

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;
    }
}
于 2013-02-04T20:16:58.517 回答
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);

此代码假定使用 mysqldump 或任何其他在每个语句结束后换行的程序创建 sql 转储。

于 2015-08-04T08:08:16.943 回答
2

您需要分别运行每个语句并删除注释

  • 以空命令字符串开头
  • 阅读每一行
  • 修剪线
  • 丢弃以-开头的那些
  • 将行添加到您的命令字符串
  • 如果行以 ; 结尾 运行命令并重复到步骤 1
于 2013-02-04T16:49:03.427 回答
1

因为它在 SQL 语句的错误中列出,所以您正在尝试执行以下查询

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

以上不是有效的 SQL 语句,因此它会在第 1 行给您错误。您需要打开包含 SQL 的文件,然后将其主体用作

q
于 2013-02-04T16:47:22.737 回答