posts - 495,  comments - 11,  trackbacks - 0

          代碼如下:

          /*
          * @(#)DatabaseBackup.java Apr 23, 2009
          *
          * Copyright (c) 2009 by jadmin. All Rights Reserved.
          */

          package util.dbak;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.InputStreamReader;
          import java.io.OutputStream;
          import java.io.OutputStreamWriter;
          import java.io.PrintWriter;
          import java.io.UnsupportedEncodingException;

          /**
          * MySQL數據庫的備份與恢復
          * 缺陷:可能會被殺毒軟件攔截
          *
          * @author <a href="mailto:jadmin@163.com">jadmin</a>
          * @version $Revision: 1.0 Apr 23, 2009 11:44:00 PM $
          */
          public class DatabaseBackup {

          /** MySQL安裝目錄的Bin目錄的絕對路徑 */
          private String mysqlBinPath;

          /** 訪問MySQL數據庫的用戶名 */
          private String username;

          /** 訪問MySQL數據庫的密碼 */
          private String password;

          public String getMysqlBinPath() {
          ?? return mysqlBinPath;
          }

          public void setMysqlBinPath(String mysqlBinPath) {
          ?? this.mysqlBinPath = mysqlBinPath;
          }

          public String getUsername() {
          ?? return username;
          }

          public void setUsername(String username) {
          ?? this.username = username;
          }

          public String getPassword() {
          ?? return password;
          }

          public void setPassword(String password) {
          ?? this.password = password;
          }

          public DatabaseBackup() {
          ?? super();
          }

          public DatabaseBackup(String mysqlBinPath, String username, String password) {
          ?? super();
          ?? if (!mysqlBinPath.endsWith(File.separator)) {
          ??? mysqlBinPath = mysqlBinPath + File.separator;
          ?? }
          ?? this.mysqlBinPath = mysqlBinPath;
          ?? this.username = username;
          ?? this.password = password;
          }

          /**
          * 備份數據庫
          *
          * @param output 輸出流
          * @param dbname 要備份的數據庫名
          */
          public void backup(OutputStream output, String dbname) {
          ?? String command = "cmd /c " + mysqlBinPath + "mysqldump -u" + username + " -p" + password + " --set-charset=utf8 "
          ???? + dbname;
          ?? System.out.println(command);
          ?? PrintWriter p = null;
          ?? BufferedReader reader = null;
          ?? try {
          ??? p = new PrintWriter(new OutputStreamWriter(output, "utf8"));
          ??? Process process = Runtime.getRuntime().exec(command);
          ??? InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
          ??? reader = new BufferedReader(inputStreamReader);
          ??? String line = null;
          ??? while ((line = reader.readLine()) != null) {
          ???? p.println(line);
          ??? }
          ??? p.flush();
          ?? } catch (UnsupportedEncodingException e) {
          ??? e.printStackTrace();
          ?? } catch (IOException e) {
          ??? e.printStackTrace();
          ?? } finally {
          ??? try {
          ???? if (reader != null) {
          ????? reader.close();
          ???? }
          ???? if (p != null) {
          ????? p.close();
          ???? }
          ??? } catch (IOException e) {
          ???? e.printStackTrace();
          ??? }
          ?? }
          }

          /**
          * 備份數據庫,如果指定路徑的文件不存在會自動生成
          *
          * @param dest 備份文件的路徑
          * @param dbname 要備份的數據庫
          */
          public void backup(String dest, String dbname) {
          ?? try {
          ??? OutputStream out = new FileOutputStream(dest);
          ??? backup(out, dbname);
          ?? } catch (FileNotFoundException e) {
          ??? e.printStackTrace();
          ?? }
          }

          /**
          * 恢復數據庫
          *
          * @param input 輸入流
          * @param dbname 數據庫名
          */
          public void restore(InputStream input, String dbname) {
          ?? String command = "cmd /c " + mysqlBinPath + "mysql -u" + username + " -p" + password + " " + dbname;
          ?? try {
          ??? Process process = Runtime.getRuntime().exec(command);
          ??? OutputStream out = process.getOutputStream();
          ??? String line = null;
          ??? String outStr = null;
          ??? StringBuffer sb = new StringBuffer("");
          ??? BufferedReader br = new BufferedReader(new InputStreamReader(input, "utf8"));
          ??? while ((line = br.readLine()) != null) {
          ???? sb.append(line + "\r\n");
          ??? }
          ??? outStr = sb.toString();

          ??? OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
          ??? writer.write(outStr);
          ??? writer.flush();
          ??? out.close();
          ??? br.close();
          ??? writer.close();
          ?? } catch (UnsupportedEncodingException e) {
          ??? e.printStackTrace();
          ?? } catch (IOException e) {
          ??? e.printStackTrace();
          ?? }

          }

          /**
          * 恢復數據庫
          *
          * @param dest 備份文件的路徑
          * @param dbname 數據庫名
          */
          public void restore(String dest, String dbname) {
          ?? try {
          ??? InputStream input = new FileInputStream(dest);
          ??? restore(input, dbname);
          ?? } catch (FileNotFoundException e) {
          ??? e.printStackTrace();
          ?? }
          }

          public static void main(String[] args) {
          ?? DatabaseBackup bak = new DatabaseBackup("C:/MySQL/UTF8/bin", "root", "root");
          ?? bak.restore("c:/t.sql", "ttk");
          }
          }

          tags:java,mysql,database,數據庫,備份,恢復,mysql命令

          posted on 2009-04-24 22:55 jadmin 閱讀(87) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 区。| 大新县| 龙陵县| 屯门区| 漾濞| 七台河市| 长治县| 潞西市| 慈利县| 武定县| 九台市| 繁峙县| 安庆市| 元阳县| 西和县| 周至县| 天柱县| 松滋市| 木兰县| 郸城县| 靖江市| 专栏| 军事| 青海省| 卫辉市| 鸡东县| 昭苏县| 封丘县| 保亭| 安远县| 广河县| 龙岩市| 清苑县| 宁明县| 吴江市| 洛阳市| 楚雄市| 康保县| 天津市| 湘乡市| 鹿邑县|