In this tutorial show, you how to take a backup of MySQL database using the java application. When you complete this tutorial you can get a better idea about how to take a backup of MySQL database, how to save the backup file on your local machine and how to attach the date and name with the backup file. Follow this video tutorial with source code to complete this task.
These codes use to set file path and name for the backup file. Here using JFileChooser browse the location to save file and add a name for the backup file. Further, attach date with the file name. Then can identify the backup file with date and name.
Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact syntech1994@gmail.com
Choose File path
JFileChooser jfc= new JFileChooser();
jfc.showOpenDialog(this);
// get date
String date=new SimpleDateFormat("yyy-MM-dd").format(new Date());
try {
File f= jfc.getSelectedFile();
path=f.getAbsolutePath();
path=path.replace('\', '/');
// attach date with file path
path = path + "_" + date + ".sql";
txtpath.setText(path);
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e);
}
Using these codes we can run the backup taking process. You should attach database name, database user name and password with this codes.
Codes to take a backup of MySQL database using java
int x=txtpath.getText().length();
if(x>0)
{
try {
Runtime run= Runtime.getRuntime();
pr=run.exec("C://wamp64//bin//mysql//mysql5.7.21//bin//mysqldump.exe -udatabaseUserName --add-drop-database -B databaseName -r"+ path);
int processComplete= pr.waitFor();
if(processComplete==0)
{
JOptionPane.showMessageDialog(rootPane, "Backup Success");
}
else
{
JOptionPane.showMessageDialog(rootPane, "Fail");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e);
}
}
else
{
JOptionPane.showMessageDialog(rootPane, "Select Path");
}