i am trying to add a "create backup" option on one of my desktop applications which i have developed by java. i have searched a lot about this and found some approaches to solve this problem, but all of them destroy a single important concept "portability" . many people suggest to use mysqldump.exe (in windows) to do this, but i guess i need to know the mysql installation folder in order to take this approach . the below is the recommended code which by the way failed to run properly (i dont know why, please tell me if you notice the reason)
private static String dbName = "shams";
private static String dbUserName = "root";
private static String dbPassword = "";
public static boolean backupDB(File file) {
String path = file.getPath();
if (!path.contains(".sql")) {
file = new File(path +".sql");
}
String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + file.getPath();
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
runtimeProcess.destroy();
return true;
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
and some suggest to simply copy the database to a certain location.
so.. what should i do to this in a neat way? any third party jar available?
--edit ---
the problem with the given code is that, when you dont have a password for a user (in this case root user) you should not mention the password in your command. the command should be like this : String executeCmd = "mysqldump -u " + dbUserName + " --add-drop-database -B " + dbName + " -r " + file.getPath();
and also you don't need to specify the mysql installation folder after all. the above code works as it is.