会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
hibernate3例子
博客园
首页
新随笔
联系
订阅
管理
mysql数据库备份--java代码
import java.io.BufferedReader; import java.io.BufferedWriter; 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 MySQLDump { public static boolean sqlDump(String cmd,String filePath){ boolean falg = false; try { Runtime run = Runtime.getRuntime(); //cmd 命令:"C:/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump -uroot -proot email" Process p = run.exec(cmd); InputStream is = p.getInputStream();// 控制台的输出信息作为输入流 InputStreamReader isr = new InputStreamReader(is,"UTF-8");//设置输入流编码格式 BufferedReader br = new BufferedReader(isr); //将控制台输入信息写入到文件输出流中 FileOutputStream fos = new FileOutputStream(filePath); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8")); String temp = null; while( (temp = br.readLine()) !=null){ bw.write(temp); bw.newLine(); } bw.flush(); bw.close(); br.close(); falg = true; System.out.println("/* Dump SQL File "+filePath+" OK! */"); } catch (IOException e) { throw new RuntimeException("请将mysql命令添加到path中!",e); } return falg; } //恢复数据库 /** * 导入 * */ public static void sqlLoad(String cmd,String sqlPath) { try { Runtime rt = Runtime.getRuntime(); // 调用 mysql 的 cmd: C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email Process child = rt.exec(cmd); OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流 //输入流 BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(sqlPath), "utf8")); //输出流 OutputStreamWriter writer = new OutputStreamWriter(out, "utf8"); String inStr; while ((inStr = br.readLine()) != null) { writer.write(inStr); writer.write("\r\n"); } writer.flush(); // 别忘记关闭输入输出流 out.close(); br.close(); writer.close(); System.out.println("/* Load SQL File "+sqlPath+" OK! */"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { sqlDump("C:/Program Files/MySQL/MySQL Server 5.1/bin/mysqldump -uroot -proot email","c:/email.sql"); sqlLoad("C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot email","c:/email.sql"); } }
posted @
2011-11-10 20:40
hibernate3例子
阅读(
680
) 评论(
0
)
编辑
收藏
举报
刷新页面
返回顶部
公告