public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException { File saveFile = new File(savePath); if (!saveFile.exists()) {// 若是目錄不存在 saveFile.mkdirs();// 建立文件夾 } if(!savePath.endsWith(File.separator)){ savePath = savePath + File.separator; } PrintWriter printWriter = null; BufferedReader bufferedReader = null; try { printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8")); Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName); InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8"); bufferedReader = new BufferedReader(inputStreamReader); String line; while((line = bufferedReader.readLine())!= null){ printWriter.println(line); } printWriter.flush(); if(process.waitFor() == 0){//0 表示線程正常終止。 return true; } }catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (printWriter != null) { printWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; }
public static void main(String[] args){ try { if (exportDatabaseTool("127.0.0.1", "root", "123456", "c:/", "blood_bank.sql", "blood_bank")) { System.out.println("數據庫成功備份!!!"); } else { System.out.println("數據庫備份失敗!!!"); } } catch (InterruptedException e) { e.printStackTrace(); } }