java中使用mysqldump 備份數據庫


java中使用mysqldump 備份數據庫: 見代碼

String hostIP = "127.0.0.1";

String userName = "root";
String password = "123456";
String savePath = "C:/Users/Administrator/Desktop/";
String fileName = "SetofBook"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".sql";
String databaseName = "sss";
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();
//0 表示線程正常終止。
if(process.waitFor() == 0){
return true;
}
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (printWriter != null) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM