簡體   English   中英

恢復MySQL備份

[英]Restoring a MySQL backup

我寫了以下代碼來還原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;
    }

在調用上述函數時,我的應用程序凍結,而未還原任何備份。

您沒有在字符串中寫密碼,在用戶名后添加“ -p password”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM