Cara Backup Export Database dengan Java Swing
esNesia | Bagi Anda-Anda yang sedang cari tahu bagaimanakah cara membuat backup database dengan cara simple menggunakn Jframe atau Swing menggunakan bahasa pemrograman java?
nah.. Anda sedang beruntung mengunjungi artikle ini. dalam artikle ini akan dijelaskan bagaimananya.. langsung saja ikuti tutorial berikut ini:
- Buatlah Form seperti gambar berikut
Form backup database java - Rubahlah nama variable jTextfield secara berurutan dengan txt_host, txt_dbname, txt_username, txt_pass, dan txt_path
- Buatlah methode getBackUpPath() dalam class form yang Anda buat seperti ini :
method getBackupPath() - Klik kanan pada tombol Browse --> Event --> Action --> ActionPerformed, ketikkan kode txt_path.setText(getBackUpPath());
- Buatlah Class Backup.java
- Buatlah Form seperti gambar berikut
Backup.java
[package Database;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Backup{
private static ResultSet res;
private static Connection con;
private Statement st;
private final int BUFFER = 99999;
public String getData(String host, String port, String user, String password, String db) {
String Mysqlpath = getMysqlBinPath(user, password, db);
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + db, user, password);
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
JOptionPane.showMessageDialog(null, Mysqlpath);
Process run = null;
try {
System.out.println(Mysqlpath + "mysqldump --host=" + host + " --port=" + port +
" --user=" + user + " --password=" + password +
" --compact --complete-insert --extended-insert " +
"--skip-comments --skip-triggers " + db);
run = Runtime.getRuntime().exec(Mysqlpath + "mysqldump --host=" + host +
" --port=" + port + " --user=" + user + " --password=" +
password + " " + "--skip-comments --skip-triggers " + db);
} catch (IOException ex) {
// Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer temp = new StringBuffer();
int count;
char[] cbuf = new char[BUFFER];
try {
while ((count = br.read(cbuf, 0, BUFFER)) != -1) {
temp.append(cbuf, 0, count);
}
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()
).log(Level.SEVERE, null, ex);
}
try {
br.close();
in.close();
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()
).log(Level.SEVERE, null, ex);
}
return temp.toString();
}
public String getMysqlBinPath(String user, String password, String db) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
System.out.print("yaha dekho");
}
try {
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/" + db, user, password);
st = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (Exception e) {
System.out.print("I am here yaaar");
e.printStackTrace();
}
String a = "";
try {
res = st.executeQuery("select @@basedir");
while (res.next()) {
a = res.getString(1);
}
} catch (Exception eee) {
eee.printStackTrace();
}
a = a + "bin\";
System.err.println("Mysql path is :" + a);
return a;
}
}]
- Klik kanan pada tombol Export Databse --> Event --> Action --> ActionPerformed, ketikkan kode
Event Action Export Database - Selesai dan coba jalankan
semoga manfaat !