Tutorial Backup & Restore Database MySQL In Netbeans
Preview
Hello friends, on this occasion I will discuss about how to create Backup & Restore MySQL Database program without going through a browser using a form that I would design with Netbeans.
Okay, just get started.
After creating a new project, you need a form in which there are only a few components:
- 1 Label
- 1 Textfield
- 2 Button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().equals("")){
JOptionPane.showMessageDialog(rootPane,"Write Database Name");
}
else{
try{
String dbname = jTextField1.getText();
String dbuser = "root";
String dbpass = "root";
String folderpath = "src" + "\\Backup database";
File fl = new File(folderpath);
fl.mkdir();
String savepath = "\"" + folderpath + "\\" + "" + dbname + ""+".sql\"";
String execudecmd = "mysqldump -u" + dbuser + " -p" + dbpass + " --database " + dbname + " -r " + savepath;
Process runtimeprocess = Runtime.getRuntime().exec(execudecmd);
int processcomplete = runtimeprocess.waitFor();
if (processcomplete == 0){
JOptionPane.showMessageDialog(rootPane,"Backup Successfully");
jTextField1.setText(null);
}
else{
JOptionPane.showMessageDialog(rootPane,"Backup Failed");
File f2 = new File("src\\Backup Database\\"+ dbname+".sql");
f2.delete();
}
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane,e);
}
}
}
NOTES :
Backup Database is a folder that will be created inside the package as the storage of your .sql database file.
Backup Database is a folder that will be created inside the package as the storage of your .sql database file.
MORE ARTICLE
Tutorial Login With Netbeans and MySQL
When the program starts and you type the database name into TextField and click Button 1, then all the contents of your database will be saved in one .sql file that resides in the Backup Database folder.
Then double-click Button 2, and enter the following code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().equals("")){
JOptionPane.showMessageDialog(rootPane,"Write Database Name");
}
else{
try{
String dbname = jTextField1.getText();
String dbuser = "root";
String dbpass = "root";
String folderpath = "src" + "\\Backup Database";
String restorepath = "src\\Backup Database\\"+ dbname+".sql";
String[] execudecmd = new String[] {"mysql", "--user=" +dbuser, "--password=" +dbpass, dbname,"-e"," source " + restorepath};
Process runtimeprocess = Runtime.getRuntime().exec(execudecmd);
int processcomplete = runtimeprocess.waitFor();
if (processcomplete == 0){
JOptionPane.showMessageDialog(rootPane,"Restore Successfully");
jTextField1.setText(null);
}
else{
JOptionPane.showMessageDialog(rootPane,"Restore Failed");
}
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane,e);
}
}
}
After the Backup process, you will perform the Restore process. Same as before you have to type the name of the database you want to restore, then click Button 2. Then all the data contained in the .sql file will be inserted into your database.