java代码--mysql数据的备份和还原

备份数据库的代码:

/**
	 * mysql数据库备份
	 * 
	 * @param savePath
	 * @param fileName
	 * @param hostIP
	 * @param userName
	 * @param passWord
	 * @param databaseName
	 */
	public static void backup(String savePath, String fileName, String hostIP,
			String userName, String passWord, String databaseName) {
		try {
			Runtime rt = Runtime.getRuntime();
			System.out.println("备份开始······");

			Process child = rt.exec("mysqldump -h " + hostIP + " -u" + userName
					+ " -p" + passWord + "  " + databaseName);

			InputStream in = child.getInputStream();

			InputStreamReader xx = new InputStreamReader(in, "utf-8");

			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;

			BufferedReader br = new BufferedReader(xx);
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			File saveFile = new File(savePath);
			if (!saveFile.exists()) {
				saveFile.mkdirs();
			}
			FileOutputStream fout = new FileOutputStream(savePath + "/"
					+ fileName + ".sql");
			OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
			writer.write(outStr);
			writer.flush();
			in.close();
			xx.close();
			br.close();
			writer.close();
			fout.close();

			System.out.println("");
			System.out.println("备份成功······");

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("备份失败······");
		}

	}
还原数据库的代码:
/**
	 * Mysql数据库的还原
	 * 
	 * @param fPath
	 * @param hostIP
	 * @param userName
	 * @param passWord
	 * @param databaseName
	 */
	public static void load1(String fPath, String hostIP, String userName,
			String passWord, String databaseName) {
		try {
			System.out.println("还原开始·······");
			// String fPath = "c:/test.sql";

			Runtime rt = Runtime.getRuntime();

			// 调用mysql的cmd命令
			Process child = rt.exec("mysql -u" + userName + " -p" + passWord
					+ "  " + databaseName);
			OutputStream out = child.getOutputStream();
			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(fPath), "utf8"));
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
			writer.write(outStr);

			writer.flush();

			out.close();
			br.close();
			writer.close();

			System.out.println("");
			System.out.println("还原成功······");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("还原失败······");
		}

	}
	
测试代码:

public static void main(String args[]) throws IOException {
		
		
		/**************** 测试Mysql数据库的备份begin **************************************/
		/*String hostIP = "localhost";
		String userName = "root";
		String passWord = "root";
		String databaseName = "db_test";
		String savePath = "F:/backup";
		String fileName = "db_test11";
		String fPath = "F:/backup/db_test.sql";

		backup(savePath, fileName, hostIP, userName, passWord, databaseName);
		load1(fPath, hostIP, userName, passWord, databaseName);*/
		/******************************* 测试Mysql数据库的备份end **************************************/

	}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值