简体   繁体   中英

Restoring a MySQL backup

I wrote the following code to restore MySQL backup ,

  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;
    }

on calling the above function , my application freezes without restoring any backup.

您没有在字符串中写密码,在用户名后添加“ -p password”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM