Berikut ini code untuk export database MySQL dengan menggunakan java.
public static void backup(String db, String user, String pass, String path) {
try {
String executeCmd;
executeCmd = "mysqldump -u " + user + " -p" + pass + " " + db + " -r " + path + "";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
JOptionPane.showMessageDialog(null, "Export berhasil");
} else {
JOptionPane.showMessageDialog(null, "Export gagal");
}
} catch (InterruptedException | IOException ex) {
SplashLauncher.logger.log(Level.SEVERE,null, ex.getMessage());
}
}
cara pemanggilan method nya seperti ini, asumsi method yg kita buat ada di class Backup :
String path = "dump" + new SimpleDateFormat("dd-mm-yyyy").format(new Date()) + ".sql";
Backup.backup("db_mahasiswa","adicodes", "1234", path);
ket :
Selamat mencoba.
public static void backup(String db, String user, String pass, String path) {
try {
String executeCmd;
executeCmd = "mysqldump -u " + user + " -p" + pass + " " + db + " -r " + path + "";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
JOptionPane.showMessageDialog(null, "Export berhasil");
} else {
JOptionPane.showMessageDialog(null, "Export gagal");
}
} catch (InterruptedException | IOException ex) {
SplashLauncher.logger.log(Level.SEVERE,null, ex.getMessage());
}
}
cara pemanggilan method nya seperti ini, asumsi method yg kita buat ada di class Backup :
String path = "dump" + new SimpleDateFormat("dd-mm-yyyy").format(new Date()) + ".sql";
Backup.backup("db_mahasiswa","adicodes", "1234", path);
ket :
- berikan privilages pada database. misalnya disini pada db_mahasiswa dengan nama user "adicodes" dan password "1234". lihat disini
- copy kan \xampp\mysql\bin\mysqldump.exe ke system32
file export yang dihasilkan :
Selamat mencoba.
No comments:
Post a Comment