To take mysql database backup we have Runtime.getRuntime().exec(executeCmd). executeCmd is command that will take backup of mysql database.
String executeCmd contains the mysqldump to backup database.
filePath variable contains the downloaded sql file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class dump { public static void main(String s[]) { String dbName = "Db"; String dbUser = "user"; String dbPass = "pass"; String filePath = "C:\\myFirstDump.sql"; int status ; String executeCmd = "C:\\wamp\\bin\\mysql\\mysql5.5.8\\bin\\mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r " + filePath; try { Process runtimeProcess = Runtime.getRuntime().exec(executeCmd); status = runtimeProcess.waitFor(); if (status == 0) { System.out.println("Backup taken successfully"); } else { System.out.println("Could not take mysql backup"); } } catch (Exception e) { System.out.println("Exception " + e); } } } |