2013-03-14 102 views
0

我写了下面的代码来恢复MySQL的备份,还原MySQL的备份

public boolean restoreDB(String dbUserName, String dbPassword) { 
     try { 

      NewClassx n = new NewClassx(); 
      n.myf(2); 
      String source = jTextField1.getText(); 
      Process runtimeProcess; 
      Connection con = CPool.getConnection(); 
      Statement stmt = con.createStatement(); 
      ResultSet res = null; 
      res = stmt.executeQuery("select @@datadir"); 
      String Mysqlpath = ""; 

      while (res.next()) { 
       Mysqlpath = res.getString(1); 
      } 

      Mysqlpath = Mysqlpath.replace("data", "bin"); 
      CPool.closeConnection(con); 
      CPool.closeStatement(stmt); 
      CPool.closeResultSet(res); 
      // String[] executeCmd = new String[]{Mysqlpath + "\\mysql", "--user=" + dbUserName, " -e", "source " + "\"" + source + "\""}; 
      // String executeCmd = Mysqlpath + "\\mysql - u "+ dbUserName +" -e "+ "\"" + source + "\""; 
      //mysql - u admin -p admin accounts < 
      String[] executeCmd = new String[]{Mysqlpath + "\\mysql", " project_db ", " --user=" + dbUserName, " -e", " source "+ " \"" + source + "\""}; 
      // System.out.println(executeCmd); 

      runtimeProcess = Runtime.getRuntime().exec(executeCmd); 
      int processComplete = runtimeProcess.waitFor(); 

      if (processComplete == 0) { 
       JOptionPane.showInternalMessageDialog(this, "Backup restored successfully"); 
       return true; 
      } else { 
       JOptionPane.showInternalMessageDialog(this, "Backup was not restored"); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     return false; 
    } 

上调用上述功能,我的应用程序冻结没有恢复任何备份。

+0

毫不奇怪的,那youre调用'的'EDT'进程#waitFor'。请尝试使用“SwingWorker”。 – Reimeus 2013-03-14 11:52:33

回答

0

你没有用户名后的字符串写入密码 添加“-p密码”