gpt4 book ai didi

java - 使用java命令在mysql中恢复数据库

转载 作者:行者123 更新时间:2023-11-30 23:05:50 25 4
gpt4 key购买 nike

这是我使用 java 命令恢复 mysql 数据库的代码。

当我调试代码时,它卡在了 runtimeProcess.waitFor();

谁能解决这个问题?

public static boolean restoreDB(String dbUserName, String dbPassword,String dbName, String path) {

String command ="mysql -u"+dbUserName+" -p"+dbPassword+" "+dbName+" < \""+path+"\"";

Process runtimeProcess;
try {

runtimeProcess = Runtime.getRuntime().exec(command);
int processComplete = runtimeProcess.waitFor();

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

return false;
}

最佳答案

进程可能正在阻塞等待输入,或者它已经写入了太多输出以致于填满了进程输出缓冲区并正在等待您读取输出。无论哪种方式,如果您确保打印出该过程的标准输出和标准错误,您可以获得更多信息:

private static class StreamReader implements Runnable {

private InputStream stream;
private volatile boolean finished = false;

public StreamReader(InputStream stream) {
this.stream = stream;
}

public void run() {
Scanner scanner = new Scanner(stream);

while(!finished) {
System.out.println(scanner.nextLine());
}

}

}

//...
runtimeProcess = Runtime.getRuntime().exec(command);
StreamReader stdoutReader = new StreamReader(runtimeProcess.getInputStream());
StreamReader stderrReader = new StreamReader(runtimeProcess.getErrorStream());
new Thread(stdoutReader).start();
new Thread(stderrReader).start();
int processComplete = -1;
try {
processComplete = runtimeProcess.waitFor();
} finally {
stdoutReader.finished = true;
stderrReader.finished = true;
}

关于java - 使用java命令在mysql中恢复数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com