java 实现 mysql 数据库导出与导入(适合linux和windows)-阿里云开发者社区

开发者社区> 数据库> 正文
登录阅读全文

java 实现 mysql 数据库导出与导入(适合linux和windows)

简介: </pre>备份<p></p><p></p><pre name="code" class="html">import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStream; import java.io

</pre>备份<p></p><p></p><pre name="code" class="html">import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class DataBackup {
	public static void main(String[] args) {
		backup();
	}
	


	public static void backup() {
		try {
		Runtime rt = Runtime.getRuntime();

		Process child = rt.exec("D:/MySQL/MySQL Server 5.6/bin/mysqldump -uroot -p123456 -R -c --set-charset=utf8 senyuan");
		InputStream in = child.getInputStream();
		InputStreamReader xx = new InputStreamReader(in, "utf8");
		String inStr;
		StringBuffer sb = new StringBuffer("");
		String outStr;
		BufferedReader br = new BufferedReader(xx);
		while ((inStr = br.readLine()) != null) {
		sb.append(inStr + "\r\n");
		}
		outStr = sb.toString();

		FileOutputStream fout = new FileOutputStream("E:/sql/qq.sql");
		OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
		writer.write(outStr);
		writer.flush();

		in.close();
		xx.close();
		br.close();
		writer.close();
		fout.close();
		} catch (Exception e) {
		e.printStackTrace();
		}

		}

}


导入

public static void load() {
try {
String fPath = "备份的文件路径名";
Runtime rt = Runtime.getRuntime();

Process child = rt.exec("mysql -u用户名 -p密码 数据库名");
OutputStream out = child.getOutputStream();
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();

OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();

out.close();
br.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

其他文章
最新文章
相关文章