使用Java GUI进行MySQL数据库备份和恢复

时间:2013-12-05 05:40:40

标签: java mysql netbeans database-backups database-restore

我找到了一个用于数据库备份的java代码。这项工作正常

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");

还有我老师的恢复代码作为休闲

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

问题是我不知道如何在我的应用程序中实现此代码。您可以提供与我的备份代码相关的答案。

1 个答案:

答案 0 :(得分:0)

您可以使用此简单代码恢复数据库

        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);
    }