Java實現MySQLDatabase Backup(一)

來源:互聯網
上載者:User

標籤:儲存   write   return   總結   返回   blog   number   host   exists   

 下班了,利用閑暇時間總結一下如何使用Java語言實現MySQLDatabase Backup:

 

[java] view plain copy 
  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.OutputStreamWriter;  
  7. import java.io.PrintWriter;  
  8.   
  9. /** 
  10.  * MySQLDatabase Backup 
  11.  *  
  12.  * @author GaoHuanjie 
  13.  */  
  14. public class MySQLDatabaseBackup {  
  15.   
  16.     /** 
  17.      * Java代碼實現MySQL資料庫匯出 
  18.      *  
  19.      * @author GaoHuanjie 
  20.      * @param hostIP MySQL資料庫所在伺服器位址IP 
  21.      * @param userName 進入資料庫所需要的使用者名稱 
  22.      * @param password 進入資料庫所需要的密碼 
  23.      * @param savePath 資料庫匯出檔案儲存路徑 
  24.      * @param fileName 資料庫匯出檔案檔案名稱 
  25.      * @param databaseName 要匯出的資料庫名 
  26.      * @return 返回true表示匯出成功,否則返回false。 
  27.      */  
  28.     public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException {  
  29.         File saveFile = new File(savePath);  
  30.         if (!saveFile.exists()) {// 如果目錄不存在  
  31.             saveFile.mkdirs();// 建立檔案夾  
  32.         }  
  33.         if(!savePath.endsWith(File.separator)){  
  34.             savePath = savePath + File.separator;  
  35.         }  
  36.           
  37.         PrintWriter printWriter = null;  
  38.         BufferedReader bufferedReader = null;  
  39.         try {  
  40.             printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));  
  41.             Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);  
  42.             InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");  
  43.             bufferedReader = new BufferedReader(inputStreamReader);  
  44.             String line;  
  45.             while((line = bufferedReader.readLine())!= null){  
  46.                 printWriter.println(line);  
  47.             }  
  48.             printWriter.flush();  
  49.             if(process.waitFor() == 0){//0 表示線程正常終止。  
  50.                 return true;  
  51.             }  
  52.         }catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         } finally {  
  55.             try {  
  56.                 if (bufferedReader != null) {  
  57.                     bufferedReader.close();  
  58.                 }  
  59.                 if (printWriter != null) {  
  60.                     printWriter.close();  
  61.                 }  
  62.             } catch (IOException e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.         }  
  66.         return false;  
  67.     }  
  68.       
  69.     public static void main(String[] args){  
  70.         try {  
  71.             if (exportDatabaseTool("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test")) {  
  72.                 System.out.println("資料庫成功備份!!!");  
  73.             } else {  
  74.                 System.out.println("Database Backup失敗!!!");  
  75.             }  
  76.         } catch (InterruptedException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.     }  
  80. }  

Java實現MySQLDatabase Backup(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.