将转储文件导入mysql JDBC

时间:2013-02-04 16:34:08

标签: java mysql jdbc mysqlimport

我正在使用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'附近

4 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:4)

我实际上使用了@Makan Tayebi自己的答案,但我觉得可以做一些改进。 如果转储文件大小很大,则可能会出现第1个问题,这种方法不是最佳方法。 如果表中的数据包含特殊字符';',则可能会出现第2个问题在''中为数据,拆分在字符串中读取的文件;也将分裂;并且会发生异常。 现在,这是我的解决方案。刚编辑了他的:

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,或者在每个语句结束后中断行的任何其他程序。

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

因为它在SQL statsement的错误中列出,你试图在下面的查询中执行

source C:...\Desktop\dumpfile.sql
上面的

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

q