java 調用命令 備份mysql資料庫

來源:互聯網
上載者:User
  java 調用命令 備份mysql資料庫

Java代碼
  1. String command = "cmd /c C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqldump -h localhost -u root -p aijia > E:/aijia.dmp";  
  2.   try {  
  3.    Process process = Runtime.getRuntime().exec(command);  
  4.    InputStreamReader ir = new InputStreamReader(process  
  5.      .getInputStream());  
  6.    LineNumberReader input = new LineNumberReader(ir);  
  7.    String line;  
  8.    while ((line = input.readLine()) != null)  
  9.     System.out.println(line);  
  10.    input.close();  
  11.   } catch (IOException e) {  
  12.    e.printStackTrace();  
  13.   }  
  14.   
  15.   
  16.   
  17.   
  18. 另外  
  19.   
  20. 首先,設定mysql的環境變數(在path中添加%MYSQL_HOME%/bin),重啟電腦。  
  21. 完整代碼:  
  22.     /** 
  23.      * @param args 
  24.      */  
  25.     public static void main(String[] args) {  
  26.         /* 
  27.          * 備份和匯入是一個互逆的過程。 
  28.          * 備份:程式調用mysql的備份命令,讀出控制台輸入資料流資訊,寫入.sql檔案; 
  29.          * 匯入:程式調用mysql的匯入命令,把從.sql檔案中讀出的資訊寫入控制台的輸出資料流 
  30.          * 注意:此時定向符">"和"<"是不能用的 
  31.          */  
  32.         backup();  
  33.         load();  
  34.     }  
  35.   
  36.     /** 
  37.      * 備份檢驗一個sql檔案是否可以做匯入檔案用的一個判斷方法:把該sql檔案分別用記事本和ultra 
  38.      * edit開啟,如果看到的中文均正常沒有亂碼,則可以用來做匯入的源檔案(不管sql檔案的編碼格式如何,也不管db的編碼格式如何) 
  39.      */  
  40.     public static void backup() {  
  41.         try {  
  42.             Runtime rt = Runtime.getRuntime();  
  43.   
  44.             // 調用 mysql 的 cmd:  
  45.             Process child = rt  
  46.                     .exec("mysqldump -u root --set-charset=utf8 bjse act_obj");// 設定匯出編碼為utf8。這裡必須是utf8  
  47.              
  48.             // 把進程執行中的控制台輸出資訊寫入.sql檔案,即產生了備份檔案。註:如果不對控制台資訊進行讀出,則會導致進程堵塞無法運行  
  49.             InputStream in = child.getInputStream();// 控制台的輸出資訊作為輸入資料流  
  50.                          
  51.             InputStreamReader xx = new InputStreamReader(in, "utf8");// 設定輸出資料流編碼為utf8。這裡必須是utf8,否則從流中讀入的是亂碼  
  52.              
  53.             String inStr;  
  54.             StringBuffer sb = new StringBuffer("");  
  55.             String outStr;  
  56.             // 組合控制台輸出資訊字串  
  57.             BufferedReader br = new BufferedReader(xx);  
  58.             while ((inStr = br.readLine()) != null) {  
  59.                 sb.append(inStr + "/r/n");  
  60.             }  
  61.             outStr = sb.toString();  
  62.              
  63.             // 要用來做匯入用的sql目標檔案:  
  64.             FileOutputStream fout = new FileOutputStream(  
  65.                     "e:/mysql-5.0.27-win32/bin/bjse22.sql");  
  66.             OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");  
  67.             writer.write(outStr);  
  68.             // 註:這裡如果用緩衝方式寫入檔案的話,會導致中文亂碼,用flush()方法則可以避免  
  69.             writer.flush();  
  70.   
  71.             // 別忘記關閉輸入輸出資料流  
  72.             in.close();  
  73.             xx.close();  
  74.             br.close();  
  75.             writer.close();  
  76.             fout.close();  
  77.   
  78.             System.out.println("/* Output OK! */");  
  79.   
  80.         } catch (Exception e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.   
  84.     }  
  85.   
  86.     /** 
  87.      * 匯入 
  88.      * 
  89.      */  
  90.     public static void load() {  
  91.         try {  
  92.             String fPath = "e:/mysql-5.0.27-win32/bin/bjse22.sql";  
  93.             Runtime rt = Runtime.getRuntime();  
  94.   
  95.             // 調用 mysql 的 cmd:  
  96.             Process child = rt.exec("mysql -u root bjse ");  
  97.             OutputStream out = child.getOutputStream();//控制台的輸入資訊作為輸出資料流  
  98.             String inStr;  
  99.             StringBuffer sb = new StringBuffer("");  
  100.             String outStr;  
  101.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  102.                     new FileInputStream(fPath), "utf8"));  
  103.             while ((inStr = br.readLine()) != null) {  
  104.                 sb.append(inStr + "/r/n");  
  105.             }  
  106.             outStr = sb.toString();  
  107.   
  108.             OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");  
  109.             writer.write(outStr);  
  110.             // 註:這裡如果用緩衝方式寫入檔案的話,會導致中文亂碼,用flush()方法則可以避免  
  111.             writer.flush();  
  112.             // 別忘記關閉輸入輸出資料流  
  113.             out.close();  
  114.             br.close();  
  115.             writer.close();  
  116.   
  117.             System.out.println("/* Load OK! */");  
  118.   
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.         }  
  122.   
  123.     } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.