Java 将转储文件导入mysql JDBC

Java 将转储文件导入mysql JDBC,java,mysql,jdbc,mysqlimport,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 = "sourc

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


由于它列在错误SQL语句中,所以您正在尝试执行下面的查询

source C:...\Desktop\dumpfile.sql
上面的SQL语句无效,因此在第1行出现错误。 您需要打开包含SQL的文件,然后将其主体用作

q

由于它列在错误SQL语句中,所以您正在尝试执行下面的查询

source C:...\Desktop\dumpfile.sql
上面的SQL语句无效,因此在第1行出现错误。 您需要打开包含SQL的文件,然后将其主体用作

q

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

  • 以空命令字符串开始
  • 读每一行
  • 修剪线
  • 丢弃以--
  • 将行添加到命令字符串中
  • 如果行以结束;运行命令并重复步骤1

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

  • 以空命令字符串开始
  • 读每一行
  • 修剪线
  • 丢弃以--
  • 将行添加到命令字符串中
  • 如果行以结束;运行命令并重复步骤1

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

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

      这段代码假设sql转储是使用mysqldump或任何其他在每条语句结束后换行的程序创建的。

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

      将executeUpdate更改为executeQuery,并查看它是否工作否我刚刚测试了“executeQuery”和“execute”但是他们都给了我一个完全相同的错误。如果您使用
      mysqldump
      程序生成mySQL转储文件,加载它的最简单方法是使用
      mysqlimport
      程序。将executeUpdate更改为executeQuery,看看它是否工作否我刚刚测试了“executeQuery”和“execute”但是他们都给了我一个完全相同的错误。如果您使用
      mysqldump
      程序生成mySQL转储文件,加载它的最简单方法是使用
      mysqlimport
      程序。这可能会对您的想法有所帮助。谢谢我刚刚补充了完整的答案。(请注意,在使用q之前,我们应该将查询分开,否则我们将面临错误。)这可能会帮助您的想法实际上帮助了很多。谢谢我刚刚补充了完整的答案。(请注意,在使用q之前,我们应该分开查询,否则我们将面临错误。)谢谢。我实现了这样一个代码,并将其放在答案中。一开始看起来很棘手,但这是一个完美的解决方案:)谢谢。我实现了这样一个代码,并将其放在答案中。一开始看起来很棘手,但这是一个完美的解决方案:)谢谢你的努力。此外,我们应该考虑到一条线可能会有一个注释或一个空白。这就是我所说的原因,我假设转储文件是由MySQL转储生成的,而且我在MySQL LoDp和SQLYog的转储上使用了这个方法,这是完全正确的,而且我发现一些转储在单个事务中,因此,在这种情况下,您的方法和我的方法都将失败,对于这种情况,我们应该读取整个文件并在单个查询中执行它。感谢您的努力。此外,我们应该考虑到一条线可能会有一个注释或一个空白。这就是我所说的原因,我假设转储文件是由MySQL转储生成的,而且我在MySQL LoDp和SQLYog的转储上使用了这个方法,这是完全正确的,而且我发现一些转储在单个事务中,因此,在这种情况下,您的方法和我的方法都将失败,对于这种情况,我们应该读取整个文件并在单个查询中执行它。这是怎么做到的?好了:)你能提供更多关于connectToDB方法的信息吗。怎么做的?给你:)