gpt4 book ai didi

通过java备份恢复mysql数据库的实现代码

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章通过java备份恢复mysql数据库的实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

代码如下

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数据库的实现代码的文章就讲到这里了,如果你想了解更多关于通过java备份恢复mysql数据库的实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com