java中使用mysqldump 备份数据库

Posted youyouxiaosheng-lh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();




以上是关于java中使用mysqldump 备份数据库的主要内容,如果未能解决你的问题,请参考以下文章

mysqldump 逻辑备份搭建备库

mysql备份与还原之mysqldump

mysqldump备份

mysqldump+binlog的备份与恢复

mysqldump直接将MySQL数据库压缩备份遇到报错

学习笔记-备份还原