是否有任何Mysql备份和恢复java api

时间:2014-10-22 16:28:24

标签: java mysql

.NET使用引用进行mysql备份和还原。但我在java中找不到任何.jar库用于mysql备份和恢复。

1 个答案:

答案 0 :(得分:0)

您可以使用MySQL提供的mysqldump实用程序备份和恢复MySQL。

使用Proces从java访问此实用程序可以很好地完成工作。

Process backup = null;
            try {
                Runtime runtime = Runtime.getRuntime();
                backup = runtime.exec("mysqldump -uroot -pdbpass --add-drop-database -B dbname -r " + "filepath" + "Filename" + ".sql");//change the dbpass and dbname with your dbpass and dbname

                int dumpCompleted = backup.waitFor();

                if (dumpCompleted == 0) {

                    System.out.println("Backup created successfully!");

                } else {
                    System.out.println("Backup created successfully!");("Could not create the backup");
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

要恢复数据库,

Process restoreProcess=null;
        try {
        String restoreCommand= "mysql ", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source"
        Runtime runtime = Runtime.getRuntime();
            restoreProcess = runtime.exec(restoreCommand);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Restored successfully!");
            } else {
                System.out.println("Could not restore the backup!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }