用Java应用程序进行mysqldump

6
尝试使用以下命令从Java应用程序(IDE Netbeans)备份mysql数据库,但即使我指定了路径,也似乎找不到该文件:
Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

此外,我并没有收到任何错误提示,因此我认为备份已经完成了。那么我该如何找到这个文件呢?关于fisier.getName()的问题: File fisier = jFileChooserSave.getSelectedFile();


fisier.getName()的值是什么? - Sotirios Delimanolis
这是我通过jFileChooser给出的名称。因此,通过jFileChooser,我成功创建了一个数据库,创建了一个表,但似乎无法创建备份。 - bluesony
我将从返回的Process中读取标准InputStream和错误InputStream。 - Sotirios Delimanolis
我该如何读取它?为了选择文件,我做了以下操作:private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {
if(jFileChooserSave.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){ File fisier = jFileChooserSave.getSelectedFile();
- bluesony
不是的,Runtime#exec()返回一个Process对象。获取并读取它的错误输入流。 - Sotirios Delimanolis
2个回答

10

对于非Windows用户:

String dump = "mysqldump -usome_user -psome_pass database_name   > path/to/file.sql";
String[] cmdarray = {"/bin/sh","-c", dump};
Process p = Runtime.getRuntime().exec(cmdarray);
if (p.waitFor() == 0) {
    // Everything went fine
} else {
   // Something went wrong
}

使用cmd数组很重要。否则exec无法解析'>'和文件名,您将收到一个错误。


10

您可以测试下面提到的代码来测试您的mysqldump命令输出。根据我的假设,文件未创建可能有两个主要原因:

  1. 如果使用的是Windows,则目标位置的UAC权限可能是问题所在。
  2. 您可能会遇到在最终生成的供Java运行时执行的mysqldump命令中出现语法问题。

 //Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});

//Wait for the command to complete, and check if the exit value was 0 (success)
if(exec.waitFor()==0)
{
    //normally terminated, a way to read the output
    InputStream inputStream = exec.getInputStream();
    byte[] buffer = new byte[inputStream.available()];
    inputStream.read(buffer);

    String str = new String(buffer);
    System.out.println(str);
}
else
{
    // abnormally terminated, there was some problem
                //a way to read the error during the execution of the command
    InputStream errorStream = exec.getErrorStream();
    byte[] buffer = new byte[errorStream.available()];
    errorStream.read(buffer);

    String str = new String(buffer);
    System.out.println(str);

}

在使用Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");时,重定向运算符无法正常工作。

这是因为它没有调用命令 shell,所以我们无法获得重定向运算符的功能。为了解决这个问题,我们可以执行一个命令提示符(cmd),然后是mysqldump命令,它就可以正常工作了。

Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});

这里的/c在cmd.exe中表示执行传递的命令并终止进程。 当Java运行时实际的命令将变成

cmd.exe /c yourmysqldumpCommand


我收到了以下错误信息:mysqldump: 找不到表:">" - bluesony
我已经使用下面的代码进行了更改,不再收到错误提示,但仍无法找到转储文件:Process exec = Runtime.getRuntime().exec("C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump ", new String[]{fisier.getName(), ">", fisier.getName()+".sql;"}); - bluesony
尝试像这样执行命令:Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",youMySqlcommand}); - Sanket Parikh
我尝试了以下代码:Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",""C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump ""+fisier.getName()+" > D:\"+fisier.getName()+".sql;"}); 我收到了错误信息:'“C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "fisier”不是内部或外部命令,也不是可运行的程序或批处理文件。 - bluesony
在这种情况下,我建议您将完整的mysqldump命令存储在字符串变量中,然后将其传递到getRuntime()方法的exec方法中。确保打印字符串变量值并尝试在没有Java的情况下从cmd执行,以确保生成的命令正常工作。 - Sanket Parikh
我做到了!感谢Sanket Parikh!它使用以下命令运行成功:String command = "C:\PROGRA1\MySQL\MYSQLS1.6\bin\mysqldump -u root -proot "+fisier.getName()+" > D:\"+fisier.getName()+".sql"; Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",command}); - bluesony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接