gpt4 book ai didi

java - 使用 java 代码恢复数据库 - 程序没有响应

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

我正在尝试使用 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) {}

...

最佳答案

String executeCmd = "mysql -u " + username + " -p" + password +" " + dbName+" < "+" \" "+file.getCanonicalPath()+"\" " ;
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
InputStream is = runtimeProcess.getInputStream();

// Do one OR the other, but not both ;)

// If you don't care about the output, but I think it's a bit of waste personally...
while (is.read() != -1) {}

// I'd at least dump the output to the console...
int byteRead = -1;
while ((byteRead = is.read()) != -1) {
System.out.print((char)byteRead );
}

int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {...}

我还建议使用ProcessBuilder而不是像这样手动创建Process,它可以更好地处理参数 - 恕我直言

关于java - 使用 java 代码恢复数据库 - 程序没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18732403/

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