备份mysql数据库的java代码

时间:2012-11-14 09:30:32

标签: java mysql database database-backups

我尝试运行以下代码来创建我的数据库备份,但它显示了一些运行时错误。

但是,我尝试在mysql shell中运行 System.out.println()输出部分(我在给定代码中评论过)并且它有效

显示io文件问题。 Plz有人帮助我。

package files;

 public class tableBackup_1 {

public boolean tbBackup(String dbName,String dbUserName, String dbPassword, String path) {

    String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;
    Process runtimeProcess;
        try
        {
            System.out.println(executeCmd);//this out put works in mysql shell
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

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

public static void main(String[] args){

        tableBackup_1 bb = new tableBackup_1();
        bb.tbBackup("test","harin","1234","C:/Users/Master/Downloads/123.sql");

}
}
    mysqldump -u harin -p1234 --add-drop-database -B test -r C:/Users/Master/Downloads/123.sql
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
        at java.lang.Runtime.exec(Runtime.java:593)
        at java.lang.Runtime.exec(Runtime.java:431)
        at java.lang.Runtime.exec(Runtime.java:328)
        at files.tableBackup_1.tbBackup(tableBackup_1.java:12)
        at files.tableBackup_1.main(tableBackup_1.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
        ... 5 more

3 个答案:

答案 0 :(得分:6)

请检查您的Global PATH环境变量是否包含&lt;路径到MySQL&gt; \ bin(执行echo %PATH%并查看)。实际上,您应该能够在Plain DOS提示符下键入System.out.println()内容,并且应该能够运行它。

如果它不起作用,请尝试更改代码以执行如下

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

理想情况下,这应解决问题。

<强>更新

如果在PATH环境变量中没有它,请将代码更改为以下

String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;

答案 1 :(得分:0)

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

我试过这个,但它没有用,所以我用

替换它

this runtimeProcess = Runtime.getRuntime().exec(executeCmd);

并且有效

答案 2 :(得分:0)

我通过提供mysqldump.exe

的完整路径解决了这个问题

您可以通过

获取SO环境变量
Map<String, String> env = System.getenv();
final String LOCATION = env.get("MYSQLDUMP");

我设置了这样的系统变量:

  • 变量名称:MYSQLDUMP
  • 价值:D:\xampp\mysql\bin\mysqldump.exe

现在只需执行以下代码,

String executeCmd = LOCATION+" -u " + DBUSER + " --add-drop-database -B " + DBNAME + " -r " + PATH + ""+FILENAME
Process runtimeProcess = = Runtime.getRuntime().exec(executeCmd);

注意:如果您没有为mysql服务器配置密码,只需从代码中删除-p PASSWORD属性即可。并且不要忘记在创建新的系统变量后重新启动计算机。