}
/**
* 备份数据库
*
* @param output
*输出流
* @param dbname
*要备份的数据库名
*/
public void backup(OutputStream output, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysqldump -u" + username
+ " -p" + password + " --set-charset=utf8 " + 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();
}
}
}
/**
* 备份数据库,如果指定路径的文件不存在会自动生成
*
* @param dest
*备份文件的路径
* @param dbname
*要备份的数据库
*/
public void backup(String dest, String dbname) {
try {
OutputStream out = new FileOutputStream(dest);
backup(out, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param input
*输入流
* @param dbname
*数据库名
*/
public void restore(InputStream input, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysql -u" + username
+ " -p" + password + " " + dbname;
try {
Process process = Runtime.getRuntime().exec(command);
OutputStream out = process.getOutputStream();
String line = null;
String outStr = null;
StringBuffer sb = new StringBuffer("");
BufferedReader br = new BufferedReader(new InputStreamReader(input,
"utf8"));
while ((line = br.readLine()) != null) {
sb.append(line + "/r/n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();
out.close();
br.close();
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param dest
*备份文件的路径
* @param dbname
*数据库名
*/
public void restore(String dest, String dbname) {
try {
InputStream input = new FileInputStream(dest);
restore(input, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Configuration config = HibernateSessionFactory.getConfiguration();
String binPath = config.getProperty("mysql.binpath");
String userName = config.getProperty("connection.username");
String pwd = config.getProperty("connection.password");
DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);
bak.backup("c:/ttt.sql", "ttt");
bak.restore("c:/ttt.sql", "ttt");
}
}
最后的main方法只是一个简单的使用方法的示例代码 。
本人所做的项目是使用了hibernate的,而这里需要提供MySQL的bin路径和用户名、密码,而hibernate.cfg.xml中本身就是需要配置数据库的用户名和密码 , 所以我把MySQL的bin路径也直接配置到了这个文件里面,也不需要创建专门的配置文件,不需要写读取配置文件的接口了 。
推荐阅读
- 网络直播要什么电脑配置,网络直播要什么电脑配置才能用
- oraclesql同步数据库的简单介绍
- 哈尔滨微信管家平台代理,微信管家在哪里
- 快手直播后如何调音效,快手直播音量怎么调比较好
- c语言宏函数的用法 c语言宏运算
- oracle数据字典索引,oracle索引字段
- mysql中位数怎么算 mysql 按位或
- 路由器信号怎么能强一点,路由器信号放大器有用吗
- 怎么将word表格居中,如何将word里的表格居中