MySQL database backup and restore with Java GUI

2.3k Views Asked by At

I found a java code for DB backup. This work properly

String sql1 = "C:\\Program Files (x86)\\MySQL\\MySQL Server 4.1\\bin\\mysqldump -uroot -    p123 exepro -r C:\\Users\\Dell\\Desktop\\Backup\\"+jLabel1.getText()+".sql";
        Runtime.getRuntime().exec(sql1);
        JOptionPane.showMessageDialog(rootPane, "Success");

and also a restore code from my teacher as fallows

String[] executeCmd = new String[]{"D:\\mysql",databaseName, "-u"+ username, "-p"+ password, "-e", "source D:/sqlbackup.sql"};

Problem is I have no idea how to implement this code to my application.Can you give an answers related to my backup code.

1

There are 1 best solutions below

0
On

you can use this simple code to restore your database

        try {
        String[] executeCmd = new String[]{"C:\\mysql", "svdb", "-u" + "username", "-p" + "password", "-e", "source d:\\HesabYar_1393-7-24.sql"};
        Process p=Runtime.getRuntime().exec(executeCmd);
        if (p.waitFor()!=-1) {
            System.err.println("Success !");
        }
    } catch (IOException ex) {
        Logger.getLogger(RuntimeProject.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(RuntimeProject.class.getName()).log(Level.SEVERE, null, ex);
    }