java 备份mysql数据库java代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13376132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:30:32  来源:igfitidea点击:

backup mysql database java code

javamysqldatabasedatabase-backups

提问by Harindra Singh

I tried to run the following code which is to create a backup of my databasebut it shows some run time errors.

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

But, I tried to run the System.out.println() outputpart, (which I've commented in the given code) in mysql shell and it worked.

但是,我尝试在 mysql shell 中运行System.out.println() 输出部分(我已在给定代码中对其进行了注释)并且它工作正常

It shows io file problem. Plz somebody help me.

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

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

回答by shazin

Please check whether your Global PATH environment variable has <Path to MySQL>\bin in it (Do a echo %PATH%and see). Effectively you should be able to type your System.out.println() content in a Plain DOS prompt and should be able to run it.

请检查您的 Global PATH 环境变量中是否有 <Path to MySQL>\bin (做一个echo %PATH%看看)。实际上,您应该能够在普通 DOS 提示符下键入 System.out.println() 内容并且应该能够运行它。

Even with that if it doesn't work try changing the code to execute like below

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

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

This should ideally fix the issue.

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

UPDATE:

更新:

If you don't have it in the PATH environment variable change the code to following

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

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

回答by Thilan Madusanka

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

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

i tried this one but it didn't work so i replaced it with

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

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

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

and it worked

它起作用了

回答by Sahan

I solved this problem by giving full path to mysqldump.exe

我通过提供 mysqldump.exe 的完整路径解决了这个问题

You can get SO environment variables by

您可以通过以下方式获取 SO 环境变量

Map<String, String> env = System.getenv();
final String LOCATION = env.get("MYSQLDUMP");

I setup the system variable like this :

我像这样设置系统变量:

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

Now just execute this code below,

现在只需执行下面的这段代码,

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

Note : If you don't have configured password for mysql server just remove the -p PASSWORDattribute from the code. And don't forget to restart your computer after creating a new system variable.

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