2013-08-16

Java Restore Mysql Database

This time I want to share how to restore a mysql database from a backup file using java.
About how backups mysql database you can see here
Swing components needed are a JFrame, JPanel,JTextfield and JButton
We also need a JFilechooser to seek the path of a file that will be on make a URL,Class Process to call mysql And one more components JOptionpane as a sign that the process of restore a database mysql has been completed

The process is we search for a location file backups by using JFileChooser that will produce the path a file and will be used as a Url, then the Url will be set to JTextField.
then call restoreMysql() method that will run Class Process with parameter mysql path and file bacukup
This is an example code..

import java.awt.FlowLayout;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class RestoreMysql extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JPanel pnl;
    JTextField tf;
    JButton bopen;
    JButton brestore;
   
    public RestoreMysql(){
       
        pnl = new JPanel();
        pnl.setLayout(new FlowLayout());
        this.getContentPane().add(pnl);
       
        tf = new JTextField(null,25);
        pnl.add(tf);
       
        bopen = new JButton("Open");
        pnl.add(bopen);
        bopen.addActionListener(new ActionListener(){

            @SuppressWarnings("deprecation")
            @Override
            public void actionPerformed(ActionEvent ae) {
                JFileChooser jfc = new JFileChooser();
                int i = jfc.showOpenDialog(pnl);
                if(i==JFileChooser.APPROVE_OPTION){
                    URL url;
                    try {
                        url = jfc.getSelectedFile().toURL();
                        String file = url.toString().replaceAll("file:/", "").trim();
                        tf.setText(file);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                   
                }
               
            }
           
        });
       
        brestore = new JButton("Restore");
        pnl.add(brestore);
        brestore.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                restoreMysql(tf.getText());
            }
           
        });
       
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 150);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
   
    private void restoreMysql(String file) {
        String user = "root";
        String pass = "";
       
        try{
            String[] executeCmd = new String[]{"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql","-hlocalhost","-port3306", "--user=" + user, "--password=" + pass, "-e", "source "+file};
            Process p = Runtime.getRuntime().exec(executeCmd);
           
            int in = p.waitFor();
            JOptionPane.showMessageDialog(this, "Restore finis..");
        } catch (InterruptedException ex) {
            System.out.println(ex);
            JOptionPane.showMessageDialog(this, ex);
        }catch(IOException e){
            System.out.println(e);
            JOptionPane.showMessageDialog(this,e);
        }
    }
   
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new RestoreMysql();
            }
        });
    }

}

hopefully helpful...

1 comment:

  1. Anonymous7:39 PM


    mas saya udah nyoba sesuai source yang mas berikan kok g bisa ke restore y mas??

    padahal restore succes mas.
    itu masalh a bagaimana mas?

    ReplyDelete