使用java GUI进行MYSQL备份

时间:2013-03-28 00:56:20

标签: java mysql mysqldump restore database-backups

我正在尝试备份我的数据库,这是我写的代码,但由于某种原因它没有备份?我正在使用local host (MAMP),我使用的操作系统是MAC OSX

public boolean databaseBackup(String dbName, String dbUserName, String dbPassword, String path) {
        String qu = "/Applications/MAMP/Library/bin/mysqldump -u" + dbUserName + " -p" + dbPassword + " --database" + dbName + " -r " + path;

System.out.println(qu);

Process runtimeProcess;

Properties pr = new Properties();

pr.setProperty("user", "username");

pr.setProperty("password", "password");

    Connection con = null;

    PreparedStatement stmt = null;

    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:8889/Database", pr);
        runtimeProcess = Runtime.getRuntime().exec(qu);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("5");

            System.out.println("Backed up");
            return true;
        } else {
            System.out.println("Not Backed up");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

}


以我的jframe形式写了这个:

code.databaseBackup("Database","root", "root", "/Users/dipeshramesh/Dropbox/TeamProject/TeamProject2.sql");

所以当一个人按下备份按钮时,它会调用code.databaseBackup方法并执行其工作。

如果我运行它会显示一条消息“Not Backed up”剂量知道这个吗?

1 个答案:

答案 0 :(得分:1)

使用String qu = "/Applications/MAMP/Library/bin/mysqldump -u" + dbUserName + " -p" + dbPassword + " --database" + dbName + " > " + path;

命令 - /Applications/MAMP/Library/bin/mysqldump -u yourUser -p --opt yourdb > yourdump.sql