Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

backup mysql database java code

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

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

It shows io file problem. Plz somebody help me.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please check whether your Global PATH environment variable has <Path to MySQL>in 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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...