恢复MySQL备份

时间:2013-03-14 11:48:49

标签: java mysql swing

我编写了以下代码来恢复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;
    }

在调用上述函数时,我的应用程序冻结而不恢复任何备份。

1 个答案:

答案 0 :(得分:0)

你没有用字符串写密码 在用户名

后添加'-p password'