通过java备份恢复mysql数据库的实现代码
时间:2024-01-25 23:44:58
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Test {
public static void main(String[] args) throws IOException{
backup("d:\\\\d.sql");
recover("d:\\\\d.sql");
}
public static void backup(String path) throws IOException{
Runtime runtime = Runtime.getRuntime();
//-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字
Process process = runtime.exec("mysqldump -u root -p123456 family");
InputStream inputStream = process.getInputStream();//得到输入流,写成.sql文件
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String s = null;
StringBuffer sb = new StringBuffer();
while((s = br.readLine()) != null){
sb.append(s+"\\r\\n");
}
s = sb.toString();
System.out.println(s);
File file = new File(path);
file.getParentFile().mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
br.close();
reader.close();
inputStream.close();
}
public static void recover(String path) throws IOException{
Runtime runtime = Runtime.getRuntime();
//-u后面是用户名,-p是密码-p后面最好不要有空格,-family是数据库的名字,--default-character-set=utf8,这句话一定的加
//我就是因为这句话没加导致程序运行成功,但是数据库里面的内容还是以前的内容,最好写上完成的sql放到cmd中一运行才知道报错了
//错误信息:
//mysql: Character set 'utf-8' is not a compiled character set and is not specified in the '
//C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\Index.xml' file ERROR 2019 (HY000): Can't
// initialize character set utf-8 (path: C:\\Program Files\\MySQL\\MySQL Server 5.5\\share\\charsets\\),
//又是讨人厌的编码问题,在恢复的时候设置一下默认的编码就可以了。
Process process = runtime.exec("mysql -u root -p123456 --default-character-set=utf8 family");
OutputStream outputStream = process.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String str = null;
StringBuffer sb = new StringBuffer();
while((str = br.readLine()) != null){
sb.append(str+"\\r\\n");
}
str = sb.toString();
System.out.println(str);
OutputStreamWriter writer = new OutputStreamWriter(outputStream,"utf-8");
writer.write(str);
writer.flush();
outputStream.close();
br.close();
writer.close();
}
}
标签:java,备份恢复,mysql数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS中判断null、undefined与NaN的方法
2024-04-19 09:54:05
使用Python的PEAK来适配协议的教程
2021-08-01 04:12:04
对Python字符串中的换行符和制表符介绍
2021-11-11 15:45:29
python反编译教程之2048小游戏实例
2023-07-24 08:04:47
![](https://img.aspxhome.com/file/2023/5/71405_0s.png)
Mootools 1.2教程(17)——手风琴插件
2008-12-11 13:39:00
浅析php与数据库代码开发规范
2024-05-02 17:33:59
IE中选择符的4095限制
2009-10-09 13:25:00
从MySQL4.0向MySQL5迁移数据
2007-11-19 13:11:00
![](https://img.aspxhome.com/file/UploadPic/200711/19/20071119131517223s.png)
python实战小游戏之考验记忆力
2023-02-23 14:29:54
![](https://img.aspxhome.com/file/2023/9/133899_0s.png)
python如何实现反向迭代
2023-05-17 17:37:55
微信小程序中做用户登录与登录态维护的实现详解
2024-04-08 10:53:44
![](https://img.aspxhome.com/file/2023/3/136963_0s.png)
Python光学仿真学习衍射算法初步理解
2021-01-07 13:54:46
![](https://img.aspxhome.com/file/2023/3/110373_0s.png)
PHP数组交集的优化代码分析
2023-09-29 21:58:59
PHP扩展Swoole实现实时异步任务队列示例
2023-11-10 05:11:22
详解操作python容器的内置通用函数
2022-12-09 04:52:39
![](https://img.aspxhome.com/file/2023/5/105055_0s.png)
java实现数据库的数据写入到txt的方法
2024-01-28 06:22:16
PyCharm永久激活方式(推荐)
2023-06-12 00:53:33
![](https://img.aspxhome.com/file/2023/7/59467_0s.png)
ASP中如何判断字符串中是否包含字母和数字
2009-07-10 13:12:00
隐蔽的ASP后门 大家可以查看下
2010-08-05 21:26:00
关于python线程池的四种实现方式
2023-08-23 06:52:30
![](https://img.aspxhome.com/file/2023/0/62020_0s.png)