JAVA代码实现备份恢复MYSQL数据库数据
/** * MYSQL数据库备份 * * @return */private boolean backup() {String user = "root"; // 数据库帐号String password = "root"; // 登陆密码String database = "yfkj_shop"; // 需要备份的数据库名String filepath = FileConfig.getBackupPath() + "/yfkj_shop.sql"; // 备份的路径地址String stmt1 = "mysqldump " + database + " -u " + user + " -p"+ password + " --result-file=" + filepath;try {Runtime.getRuntime().exec(stmt1);} catch (IOException e) {e.printStackTrace();}return true;}/** * mysql数据库恢复 * * @return */private boolean load() {String filepath = FileConfig.getBackupPath() + "/yfkj_shop.sql"; // 备份的路径地址// 判断是否存在备份文件File file = new File(filepath);if (!file.exists()) {DialogFactory.openWarning("没有在系统中找到备份文件,无法恢复!");return false;}String stmt1 = "mysqladmin -u root -proot create yfkj_shop";String stmt2 = "mysql -u root -proot yfkj_shop < " + filepath;String[] cmd = { "cmd", "/c", stmt2 };try {Runtime.getRuntime().exec(stmt1);Runtime.getRuntime().exec(cmd);} catch (IOException e) {e.printStackTrace();}return true;}
?
?
-----------------------工作积累 尹当