Java实现MySQL数据库备份(一)

 下班了,利用闲暇时间总结一下如何使用Java语言实现MySQL数据库备份:

 

[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.  * MySQL数据库备份 
  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("数据库备份失败!!!");  
  75.             }  
  76.         } catch (InterruptedException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.     }  
  80. }  
posted @ 2017-01-06 09:47  疯子110  阅读(15084)  评论(4编辑  收藏  举报