日期:2014-05-16  浏览次数:20505 次

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

?

?

-----------------------工作积累 尹当