Java实现MySQL的备份和恢复

来源:互联网 发布:黄轩演技知乎 编辑:程序博客网 时间:2024/06/07 04:09

实现方法主要是通过执行命令行来实现的。 自己将其封装成了类,方便调用。

 

package klock.util;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;import notifier.NotificationType;import notifier.NotifierDialog;//数据库备份public class MySQLBackup {/** * 导出 * @param cmd * @param filePath * @return */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) {NotifierDialog.openCenter("MySQL安装路径配置不正确,\n请到配置项中重新配置!",NotificationType.ERROR);throw new RuntimeException("请将mysql命令添加到path中!",e);}return falg;}//恢复数据库/**       * 导入       *       */      public static boolean sqlLoad(String cmd,String sqlPath) {      boolean flag = false;        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();               flag = true;            //System.out.println("/* Load  SQL File "+sqlPath+" OK! */");           } catch (Exception e) {         NotifierDialog.openCenter("MySQL安装路径配置不正确,\n请到配置项中重新配置!",NotificationType.ERROR);            e.printStackTrace();           }           return flag;      }  public static void main(String[] args) {sqlDump("E:/Progranmming/MySQL/MySQL Server 5.1/bin/mysqldump -uroot -proot proto1","F:/proto1.sql");//sqlLoad("E:/Progranmming/MySQL/MySQL Server 5.1/bin/mysql.exe -uroot -proot proto1","F:/proto1.sql");}}


 

原创粉丝点击