1
String[] executeCmd = new String[] { "mysql", "-u",DB_USER,"-p"+DB_PASSWORD,DB_NAME, " < ", "\""+FileName+"\"" };

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete: " + processComplete);

これは私が使用したコードです。「waitFor()」メソッドが呼び出されると、プログラムがハングします。

これを解決する方法は?

4

3 に答える 3

5

パラメータの後にスペースを追加する必要があります。配列は使用しないでください

String executeCmd = "mysqldump ", " -u ",DB_USER," -p " + DB_PASSWORD, + " " + DB_NAME, " < ", "\""+FileName+"\"" ;

よりクリーンな方法は使用することですString.Format()

String executeCmd = String.Format("mysqldump -u %s -p %s %s < \"%s\"", DB_USER, DB_PASSWORD, DB_NAME, FileName)
于 2012-09-28T04:16:28.870 に答える
0
    String[] command = new String[]{"mysql", Constants.DB_NAME, "-u" + Constants.DB_USER, "-p" + Constants.DB_PASSWORD, "-e", " source "+FileName };

    try {
            Process runtimeProcess = Runtime.getRuntime().exec(command);
            int processComplete = runtimeProcess.waitFor();
            if (processComplete == 0)
            {
                System.out.println("DatabaseManager.restore: Restore Successfull");

            }
            else 
            {
                System.out.println("DatabaseManager.restore: Restore Failure!");
            }

        return true;

    }

    catch (final Exception ex) 
    {
        Application.showErrorInConsole(ex);
        NmsLogger.writeErrorLog("Database Restore failed ", ex.toString());
        NmsLogger.writeDebugLog(ex);
        return false;

    }

上記のコードは私にとってはうまくいきました:)

于 2012-10-01T04:35:06.303 に答える
0

XAMPPのパスワードを設定しない場合は、-pを使用しないでください

public class Testbackupmysql {
public boolean tbBackup(String dbName,String dbUserName, String path) {

    String executeCmd = "C:\\xampp\\mysql\\bin\\mysqldump -u " + dbUserName + " --add-drop-database -B " + dbName + " -r " + path;
    Process runtimeProcess;
        try
        {
            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

                if (processComplete == 0)
                {
                    System.out.println("Backup created successfully");
                    return true;
                }
                else
                {
                    System.out.println("Could not create the backup");
                }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
return false;
}

public static void main(String[] args){

    Testbackupmysql bb = new Testbackupmysql();
        bb.tbBackup("your_database_name","your_db_username","D:\\123.sql");

}

}

于 2018-06-05T22:56:58.697 に答える