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?

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 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 – MadProgrammer Mar 28 '13 at 01: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 –  Mar 28 '13 at 03:06
  • Take a look at [this](http://stackoverflow.com/questions/15286042/im-not-getting-any-output-and-probably-the-machine-hangs-with-the-code/15286128#15286128) for a basic example – MadProgrammer Mar 28 '13 at 03: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?? –  Mar 28 '13 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 – MadProgrammer Mar 28 '13 at 20:28
  • Also. Can you run the Dom and for the command line? Does it work? – MadProgrammer Mar 28 '13 at 20:29

1 Answers1

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

Kiran Jujare
  • 178
  • 10
  • 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?? –  Mar 28 '13 at 19:06
  • Try executing the command alone, if command works with expected result, then problem might be in framing the command in java. – Kiran Jujare Mar 29 '13 at 04:25
  • 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. –  Mar 29 '13 at 12:37
  • Actually it worked :) the problem was that i added an extra speech mark that stop backing up. Thank you everybody for helping. –  Mar 29 '13 at 12:45