Java實現MySQLDatabase Backup(二)

來源:互聯網
上載者:User

標籤:wait   rgs   pat   資料庫   append   伺服器   存在   end   ora   

權聲明:本文為博主原創文章,未經博主允許不得轉載。

        部落格《Java實現MySQLDatabase Backup(一)》使用I/O流的方式實現了MySQL資料庫的備份,這種方法比較繁雜,下面介紹另一種備份MySQL資料庫的方法:

 

[java] view plain copy 
  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. /** 
  5.  * MySQLDatabase Backup 
  6.  *  
  7.  * @author GaoHuanjie 
  8.  */  
  9. public class MySQLDatabaseBackup {  
  10.   
  11.     /** 
  12.      * Java代碼實現MySQL資料庫匯出 
  13.      *  
  14.      * @author GaoHuanjie 
  15.      * @param hostIP MySQL資料庫所在伺服器位址IP 
  16.      * @param userName 進入資料庫所需要的使用者名稱 
  17.      * @param password 進入資料庫所需要的密碼 
  18.      * @param savePath 資料庫匯出檔案儲存路徑 
  19.      * @param fileName 資料庫匯出檔案檔案名稱 
  20.      * @param databaseName 要匯出的資料庫名 
  21.      * @return 返回true表示匯出成功,否則返回false。 
  22.      */  
  23.     public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {  
  24.         File saveFile = new File(savePath);  
  25.         if (!saveFile.exists()) {// 如果目錄不存在  
  26.             saveFile.mkdirs();// 建立檔案夾  
  27.         }  
  28.         if (!savePath.endsWith(File.separator)) {  
  29.             savePath = savePath + File.separator;  
  30.         }  
  31.   
  32.         StringBuilder stringBuilder = new StringBuilder();  
  33.         stringBuilder.append("mysqldump").append(" --opt").append(" -h").append(hostIP);  
  34.         stringBuilder.append(" --user=").append(userName) .append(" --password=").append(password).append(" --lock-all-tables=true");  
  35.         stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ").append(databaseName);  
  36.         try {  
  37.             Process process = Runtime.getRuntime().exec(stringBuilder.toString());  
  38.             if (process.waitFor() == 0) {// 0 表示線程正常終止。  
  39.                 return true;  
  40.             }  
  41.         } catch (IOException e) {  
  42.             e.printStackTrace();  
  43.         } catch (InterruptedException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.         return false;  
  47.     }  
  48.   
  49.     public static void main(String[] args) throws InterruptedException {  
  50.         if (exportDatabaseTool("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14.sql", "test")) {  
  51.             System.out.println("Database Backup成功!!!");  
  52.         } else {  
  53.             System.out.println("Database Backup失敗!!!");  
  54.         }  
  55.     }  
  56. }  

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.