gpt4 book ai didi

java - 如何在java代码中运行mysqldump命令?

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

我想测试在我的函数中运行 mysqldump 命令,但无法创建 aaadumpdb.sql 文件。我的代码如下:

@Test
public void dumpDB() {
Process p = null;
try {
Runtime runtime = Runtime.getRuntime();
p = runtime
.exec("mysqldump -u root -padmin --add-drop-database aaa_db "
+ "D:\\backupdenemeaaa " + "aaadumpdb.sql");
// change the dbpass and dbname with your dbpass and dbname
int processComplete = p.waitFor();

if (processComplete == 0) {

System.out.println("Backup created successfully!");

} else {
JOptionPane.showMessageDialog(new JDialog(),
"Could not create the backup");
}

} catch (Exception e) {
e.printStackTrace();
}

File f = new File("aaadumpdb.sql");

assertTrue(f.exists());
}

有人可以给我一些建议吗?谢谢。

我在代码中做了一些编辑,但是当我运行时,我的代码进入了 else 结构。可能是什么问题?

我编辑的代码如下:

    @Test
public void dumpDB() {
Process p = null;
try {
Runtime runtime = Runtime.getRuntime();
String mysqldumpExecutable = "C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump.exe";
p = runtime.exec(mysqldumpExecutable + " -uroot -padmin --add-drop-database -B aaa_db -r" + "D:\\backupdenemeaaa " + "\\aaadumpdb.sql");
// change the dbpass and dbname with your dbpass and dbname
int processComplete = p.waitFor();

if (processComplete == 0) {

System.out.println("Backup created successfully!");

} else {
JOptionPane.showMessageDialog(new JDialog(),
"Could not create the backup");
}

} catch (Exception e) {
e.printStackTrace();
}

File f = new File("aaadumpdb.sql");

assertTrue(f.exists());

如何解决这个问题?谢谢。

最佳答案

堆栈跟踪确实有助于调试它,但我相信您可以检查“mysqldump”是否在您的路径中,否则应用程序将简单地找不到可执行文件。如果您不想弄乱您的 PATH,您可以硬编码可执行文件的路径,如下所示:

String mysqldumpExecutable = "C:\\apps\\mysql\\mysqldump.exe";
runtime.exec(mysqldumpExecutable + "-u root -padmin --add-drop-database aaa_db (.....));

当您说无法创建文件时,这到底是什么意思?您收到错误了吗?什么都没发生?帮助我们帮助您。

关于java - 如何在java代码中运行mysqldump命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996149/

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