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 回答