2

我正在尝试备份我的数据库,这是我编写的代码,但由于某种原因它没有备份?我正在使用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方法并执行其工作。

如果我运行它,它会显示一条消息“未备份”,知道吗?

4

1 回答 1

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

于 2013-03-28T05:51:56.747 回答