乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java調(diào)用MySQL命令備份與恢復(fù)數(shù)據(jù)庫(kù)

       閑來(lái)看看 2012-03-30

      代碼如下:

      /*
      * @(#)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數(shù)據(jù)庫(kù)的備份與恢復(fù)
      * 缺陷:可能會(huì)被殺毒軟件攔截
      *
      * @author <a href="http://www.hbhlny.cn/mailto:jadmin@163.com%22%3Ejadmin%3C/a>
      * @version $Revision: 1.0 Apr 23, 2009 11:44:00 PM $
      */
      public class DatabaseBackup {

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

      /** 訪問(wèn)MySQL數(shù)據(jù)庫(kù)的用戶名 */
      private String username;

      /** 訪問(wèn)MySQL數(shù)據(jù)庫(kù)的密碼 */
      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;
      }

      /**
      * 備份數(shù)據(jù)庫(kù)
      *
      * @param output 輸出流
      * @param dbname 要備份的數(shù)據(jù)庫(kù)名
      */
      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();
          }
         }
      }

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

      /**
      * 恢復(fù)數(shù)據(jù)庫(kù)
      *
      * @param input 輸入流
      * @param dbname 數(shù)據(jù)庫(kù)名
      */
      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();
         }

      }

      /**
      * 恢復(fù)數(shù)據(jù)庫(kù)
      *
      * @param dest 備份文件的路徑
      * @param dbname 數(shù)據(jù)庫(kù)名
      */
      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,數(shù)據(jù)庫(kù),備份,恢復(fù),mysql命令


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多