Backups database is important,at this time I will post gives an example of how to backup a mysql database using mysqldump and java swing.
This is just an example that can develop again.To choices in making backups file please visit mysql official website
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class BackupMysqlDatabase extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel pnl;
JLabel lbl;
JTextField tf;
JButton btnOpen,btnBackup;
JFileChooser jfc;
URL url;
int s;
/**
* @param args
*/
public BackupMysqlDatabase(){
pnl = new JPanel();
getContentPane().add(pnl);
lbl = new JLabel("Backup Location");
pnl.add(lbl);
tf = new JTextField();
tf.setPreferredSize(new Dimension(200,25));
pnl.add(tf);
btnOpen = new JButton("Open");
btnOpen.addActionListener(this);
pnl.add(btnOpen);
btnBackup = new JButton("Backup");
btnBackup.addActionListener(this);
pnl.add(btnBackup);
setTitle("Backup Mysql Database");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(550,300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new BackupMysqlDatabase();
}
});
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Open")){
jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int i = jfc.showSaveDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
try {
url = jfc.getSelectedFile().toURL();
String backup_location = url.toString().replaceAll("file:/", "")+"backup.sql";
tf.setText(backup_location);
} catch (MalformedURLException e) {
System.out.println(e);
}
}
}
if(ae.getActionCommand().equals("Backup")){
try {
url = jfc.getSelectedFile().toURL();
String host = "Localhost";
String port = "3306";
String dbname = "test";
String user = "root";
String password = "";
String backup_location = tf.getText().trim();
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump -h"+host+" -port"+port+" -u "+user+" -p"+password+" --add-drop-database -B "+dbname+" -r "+backup_location);
s = p.waitFor();
JOptionPane.showMessageDialog(this, "Finis");
} catch (MalformedURLException e) {
System.out.println(e);
}catch(IOException io){
System.out.println(io);
}catch(InterruptedException ie){
System.out.println(ie);
}
}
}
}
This is just an example that can develop again.To choices in making backups file please visit mysql official website
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class BackupMysqlDatabase extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel pnl;
JLabel lbl;
JTextField tf;
JButton btnOpen,btnBackup;
JFileChooser jfc;
URL url;
int s;
/**
* @param args
*/
public BackupMysqlDatabase(){
pnl = new JPanel();
getContentPane().add(pnl);
lbl = new JLabel("Backup Location");
pnl.add(lbl);
tf = new JTextField();
tf.setPreferredSize(new Dimension(200,25));
pnl.add(tf);
btnOpen = new JButton("Open");
btnOpen.addActionListener(this);
pnl.add(btnOpen);
btnBackup = new JButton("Backup");
btnBackup.addActionListener(this);
pnl.add(btnBackup);
setTitle("Backup Mysql Database");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(550,300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new BackupMysqlDatabase();
}
});
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("Open")){
jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int i = jfc.showSaveDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
try {
url = jfc.getSelectedFile().toURL();
String backup_location = url.toString().replaceAll("file:/", "")+"backup.sql";
tf.setText(backup_location);
} catch (MalformedURLException e) {
System.out.println(e);
}
}
}
if(ae.getActionCommand().equals("Backup")){
try {
url = jfc.getSelectedFile().toURL();
String host = "Localhost";
String port = "3306";
String dbname = "test";
String user = "root";
String password = "";
String backup_location = tf.getText().trim();
Process p;
p = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump -h"+host+" -port"+port+" -u "+user+" -p"+password+" --add-drop-database -B "+dbname+" -r "+backup_location);
s = p.waitFor();
JOptionPane.showMessageDialog(this, "Finis");
} catch (MalformedURLException e) {
System.out.println(e);
}catch(IOException io){
System.out.println(io);
}catch(InterruptedException ie){
System.out.println(ie);
}
}
}
}
No comments:
Post a Comment