2

I am trying to backup my database and this is the code I've written but for some reason it is not backing up?? i am using local host (MAMP) and the operating system I am using is MAC OSX.

public boolean databaseBackup(String dbName, String dbUserName, String dbPassword, String path) {
        String qu = "/Applications/MAMP/Library/bin/mysqldump -u" + dbUserName + " -p" + dbPassword + " --database" + dbName + " -r " + path;

System.out.println(qu);

Process runtimeProcess;

Properties pr = new Properties();

pr.setProperty("user", "username");

pr.setProperty("password", "password");

    Connection con = null;

    PreparedStatement stmt = null;

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:8889/Database", pr);
        runtimeProcess = Runtime.getRuntime().exec(qu);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("5");

            System.out.println("Backed up");
            return true;
        } else {
            System.out.println("Not Backed up");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

}


in my jframe form I wrote this:

code.databaseBackup("Database","root", "root", "/Users/dipeshramesh/Dropbox/TeamProject/TeamProject2.sql");

so when a person press backup button it calls code.databaseBackup method and dose its jobs.

if I run this it shows a message "Not Backed up" dose any know this?

6
  • 2
    You should use ProcessBuilder as it simplifies the process of, well, building a Process :P. In particular, it will allow you to pass the parameters as separate elements in an array, which allows Java to better interact with the native API level, including things like spaces in parameters ;) - You are also ignoring any output from the process, which would probably be proving you with information about why the process failed Commented Mar 28, 2013 at 1:01
  • is it possible to provide an example please. can i just refine this code to make backup work. i just want backup to work Commented Mar 28, 2013 at 3:06
  • Take a look at this for a basic example Commented Mar 28, 2013 at 3:41
  • i seen this example but the thing is that i am using mac OSX and windows have different path something that starts with C:// etc... but my original command works however it only backs up .sql file it dosent have any data inside the file?? Commented Mar 28, 2013 at 19:09
  • it makes no difference. The command you want to execute is /Applications/MAMP/Library/bin/mysqldump, which becomes the first element in your command array, each parameter after that becomes a separate array. I use this method on both windows and macs all the time Commented Mar 28, 2013 at 20:28

1 Answer 1

1

use String qu = "/Applications/MAMP/Library/bin/mysqldump -u" + dbUserName + " -p" + dbPassword + " --database" + dbName + " > " + path;

command - /Applications/MAMP/Library/bin/mysqldump -u yourUser -p --opt yourdb > yourdump.sql

Sign up to request clarification or add additional context in comments.

4 Comments

use String qu = "/Applications/MAMP/Library/bin/mysqldump -u" + dbUserName + " -p" + dbPassword + " --database" + dbName + " > " + path; this command does not work for me, it shows same message. HOWEVER my original command works but it only backs up .sql file it dosent have any data inside the file??
Try executing the command alone, if command works with expected result, then problem might be in framing the command in java.
i tried running it in terminal it dose backup. in my GUI when i press 'Backup' button it created a .sql file. but when i open it only shows commented section not the actual data.
Actually it worked :) the problem was that i added an extra speech mark that stop backing up. Thank you everybody for helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.