【发布时间】:2013-09-11 03:55:50
【问题描述】:
我正在尝试使用 Java 程序恢复备份的 .sql 文件。我在下面发布方法。但是当我执行这个程序时,程序会停止很长时间。然后我在命令行(Windows)中执行了相同的 mysql 命令,效果很好。
困惑于我错过的地方。你怎么看 ?
File file;
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
try {
System.out.println(file.getCanonicalPath());
String executeCmd = "mysql -u " + username + " -p" + password +" " + dbName+" < "+" \" "+file.getCanonicalPath()+"\" " ;
Process runtimeProcess;
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
JOptionPane.showMessageDialog(Interface.mainFrame, "Database Backup restored successfully.", "Netmetering", 1);
} else {
System.out.println("Could not restore the backup");
}
} catch (IOException | InterruptedException ex) {}
...
【问题讨论】:
-
进程可能正在等待它的标准输出被刷新,你忽略了。读取进程输入流直到它返回 -1
-
对不起,我不确定我是否理解你。它需要永远完成。但是当我在 cmd 中调用相同的命令时,大约需要 2 秒才能完成。
-
@MadProgrammer 我认为你是对的。谢谢。 Apache Commons Exec 库可能是一个解决方案。我会试试的。
标签: java mysql database restore