java中使用mysqldump 备份数据库: 见代码String hostIP = 127.0.0.1;String userName = root;String password = 123456;String savePath = C:/Users/Administrator/Desktop/;String fileName = SetofBook+...
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 备份数据库
基础教程推荐
猜你喜欢
- JSP中EL表达式的用法详解(必看篇) 2023-08-01
- Java创建型设计模式之建造者模式详解 2023-07-15
- SpringBoot整合Thymeleaf与FreeMarker视图层技术 2023-04-12
- Java获取登录用户的IP地址示例代码 2023-07-14
- Java中split根据"."分割字符串问题举例 2023-06-07
- Java编写实现窗体程序显示日历 2023-01-02
- JSP的Cookie在登录中的使用 2023-07-31
- 使用反射方式获取JPA Entity的属性和值 2022-10-30
- 拦截Druid数据源自动注入帐密解密实现详解 2023-07-01
- JSON 格式的弊端与解决方法(真实示例) 2023-05-09