springMVC+hibernate框架 用命令行备份还原mysql数据库

1.要提前配置好数据库的环境变量
2.备份的命令:
mysqldump -u用户名 -p密码 --set-charset=utf8 数据库名 表名 >要备份到的地址
例:mysqldump -uroot -p123456 --set-charset=utf8 ssh_finance lsconf > e:\backup\ttt.sql
3.恢复的命令:
mysql -u用户名 -p密码 -f -D ssh_finance lsconf < e:\backup\ttt.sql

在程序中执行命令用Runtime方法:
恢复数据部分的代码:

public void restore(String path) {

try {
String command ="Mysql -u" + username + " -p" + password +" -f -D "+"ssh_finance"+"<"+path;
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c " +command);
} catch (IOException e) {
e.printStackTrace();
}
}

备份数据的代码:

public void backup(String dest, String dbname) {
try {
String path = dest+"\\"+dbname+".sql";
OutputStream out = new FileOutputStream(path);
backup(out, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public void backup(OutputStream output, String dbname) {
String command ="cmd /c "+ "mysqldump -u" + username
+ " -p" + password + " --set-charset=utf8 " +"ssh_finance "+dbname;
PrintWriter p = null;
BufferedReader reader = null;
try {
p = new PrintWriter(new OutputStreamWriter(output, "utf8"));
Process process = Runtime.getRuntime().exec(command);
InputStreamReader inputStreamReader = new InputStreamReader(process
.getInputStream(), "utf8");
reader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = reader.readLine()) != null) {
p.println(line);
}
p.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (p != null) {
p.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
阅读更多
文章标签: hibernate 数据库
上一篇EasyUI 扩展自定义validatebox校验规则
下一篇Java后台传入txt文件及form表单的ajax式提交
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭