【问题标题】:import a dump file to mysql JDBC将转储文件导入 mysql JDBC
【发布时间】:2013-02-04 16:34:08
【问题描述】:

我正在使用 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 行的“源 C:...\Desktop\dumpfile.sql”

【问题讨论】:

  • 把executeUpdate改成executeQuery看看能不能用
  • 不,我刚刚测试了“executeQuery”和“execute”,但它们都给了我完全相同的错误。
  • 如果您使用mysqldump 程序生成了mySQL 转储文件,加载它的最简单方法是使用mysqlimport 程序。

标签: java mysql jdbc mysqlimport


【解决方案1】:

我实际上使用了@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 或任何其他在每个语句结束后换行的程序创建的。

【讨论】:

  • 感谢您的努力。此外,应该考虑到一行可能以注释或空格结尾。
  • 这就是我提到的原因,我假设转储文件是由 mysqldump 生成的,我在 mysqldump 和 SQLYog 的转储上使用这种方法,这现在工作得很好,除了我发现了一些转储在单个事务中,因此您的方法和我的方法在这种情况下都会失败,对于这种情况,我们应该读取整个文件并在单个查询中执行它。
【解决方案2】:

感谢大家的帮助,看了他们的想法,终于导入了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;
    }
}

【讨论】:

  • 您可以提供有关 connectToDB 方法的更多信息。它是怎么做的?
  • 你去 :)
【解决方案3】:

您需要单独运行每个语句并删除 cmets

  • 以空命令字符串开始
  • 阅读每一行
  • 修剪线
  • 丢弃以-开头的那些
  • 在命令字符串中添加一行
  • 如果行以 ; 结尾运行命令并重复到步骤 1

【讨论】:

  • 谢谢。我实现了这样的代码并将其放在答案中。一开始看起来很棘手,但这是完美的解决方案:)
【解决方案4】:

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

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

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

q

【讨论】:

猜你喜欢
  • 2012-03-02
  • 1970-01-01
  • 2013-09-06
  • 2018-08-15
  • 2016-02-24
  • 1970-01-01
  • 2016-10-23
  • 2021-04-11
  • 2013-10-05
相关资源
最近更新 更多