这是我的mysql数据库恢复代码的代码。当我尝试这个代码应用程序工作无例外但应用程序获取挂起并且数据库未恢复..请帮助我
String databaseName = "sample"; //database name
String userName = "root"; // MySQL username
String password = ""; // MySQL password
int processComplete; // this variable for verify the process
String[] executeCmd = new String[]{"C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql",
databaseName, "-u" + userName, "-p" + password, "-e", " source D:/data.sql"};
System.out.println(executeCmd);
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);// execute the command
processComplete = runtimeProcess.waitFor();
System.out.println(processComplete);
if (processComplete == 1) { // if return value equal to 1 then failed the process
JOptionPane.showMessageDialog(null, "Restore Failed");
} else if (processComplete == 0) {{// if return value equal to 0 then failed the process
JOptionPane.showMessageDialog(null, "Restore Completed");
}
答案 0 :(得分:0)
我怀疑最后一个参数被错误处理
String[] executeCmd = new String[]{
"C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql",
databaseName,
"-u" + userName,
"-p" + password,
"-e",
" source D:/data.sql" }
应该看起来更像......
String[] executeCmd = new String[]{
"C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysql",
databaseName,
"-u" + userName,
"-p" + password,
"-e",
"source",
"D:/data.sql" }
数组中的每个元素都是传递给命令的单独参数,这使您可以灵活地传递在其中包含空格的参数,而无需先使用引号将它们转义
您应该考虑使用ProcessBuilder
而不是尝试自己构建Process
,除了允许您将错误流重定向到输入流之外,它还允许您指定起始上下文对于这个过程。
您还应该阅读进程的输出(通过它的InputStream
),这可能会突出显示问题并且还可能允许进程退出(因为某些进程在完全读取stdout之前不会退出)