-1

I am trying to take mysql database backup by using following code but I am getting exception like below when I run the program. Plz can anyone plz help out to solve this problem.

Java Code:

        String path = "D:/databasbac.sql";
        String username = "root";
        String password = "";
        String dbname = "ranjith";
        String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --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(new String[] { "cmd.exe", "/c", executeCmd });
//            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup created successfully");

            } else {
                System.out.println("Could not create the backup");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
1
  • Exception is not coming now but the the sop is printing like the could not create the backup Commented Feb 14, 2014 at 7:48

1 Answer 1

0

There are at least two issues:

  1. Provide a full path to mysqldump.exe (e.g. C:\Program Files\MySQL\bin\mysqldump.exe)
  2. -p parameter without a password value forces mysqldump to prompt for it. And you don't want it since you running it in a batch mode. Therefore either provide a password -p"your password" or better (and more secure) use an option file to avoid giving the password on the command line. Read more on this End-User Guidelines for Password Security
Sign up to request clarification or add additional context in comments.

1 Comment

I have updated my answer can u plz check and tell me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.