Reputation: 1268
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Backup extends javax.swing.JFrame {
String path = null;
String filename;
public Backup() {
initComponents();
setLocationRelativeTo(null);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(this);
String date = new SimpleDateFormat("YYYY/MM/dd").format(new Date());
try {
File f = fc.getSelectedFile();
path = f.getAbsolutePath();
path = path.replace("\\", "/");
path = path + " _ " + date + ".sql";
jTextField5.setText(path);
} catch (Exception e) {
e.printStackTrace();
}
Process p;
try {
Runtime runtime = Runtime.getRuntime();
p = runtime.exec("C:/Program Files/MySQL/MySQL Server5.1/bin/mysqldump.exe -u root -p123 --add-drop-database -B tsms -r" + path);
int Processcomplete = p.waitFor();
System.out.println(p);
System.out.println("" + Processcomplete);
if (Processcomplete == 0) {
JOptionPane.showMessageDialog(rootPane, "Database Backup Successfully");
} else {
JOptionPane.showMessageDialog(rootPane, "Error");
}
jTextField5.setText(null);
System.gc();
} catch (Exception e) {
e.printStackTrace();
System.gc();
}
}
}
I have developed a Java Swing Application with MySQL Server database and now I want to provide backup and restore option in my java swing application that is on click of a button.
It should backup the database and restore the database any possibilities this is my backup code. But when I run this code always (Processcomplete == 2)
what is the error in this code?
Upvotes: 1
Views: 179
Reputation: 8796
There are a number of problems in your code. I'll try to explain them all. But the first one is the biggest mistake which I see to return error code 2.
File names cannot contain special characters like
/
.
One of the major problems I see in your code is the file name that you are trying to save. Windows does not allow /
or \
to be entered as file names. So adding YYYY/MM/DD
to the file name is not a good idea.
Only one backup file per day
By adding only the date you can create only one backup file per day the same file will be replaced when trying to get several backups in the same day. Use getTime()
of util.Date
object to get a unique number which is being refreshed in every millisecond.
Replace this part;
try {
File f = fc.getSelectedFile();
path = f.getAbsolutePath();
path = path.replace("\\", "/");
path = path + " _ " + new Date().getTime() + ".sql";
jTextField5.setText(path);
} catch (Exception e) {
e.printStackTrace();
}
Both of the above mentioned problems are solved by this.
Giving the absolute path of
mysqldump.exe
is a bad idea
Because in different computers it may change. You have given C:/Program Files/MySQL/MySQL Server5.1/bin/mysqldump.exe
as the path, instead add it as an Environment Variable after installing MySQL and just use mysqldump.exe
.
p = runtime.exec("mysqldump.exe -u root -p123 --add-drop-database -B tsms -r" + path);
File naming part was the problem, it should work now.
Upvotes: 1