您的位置:首页 > 编程语言 > Java开发

java备份和恢复MySQL数据实例

2012-05-08 10:59 645 查看
import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

import java.util.logging.Level;

import java.util.logging.Logger;

/**

*

* 数据库操作类

* 用于备份和恢复数据

* 2012-05-08

* 吴辉

*/

public class MySqlDB {

/**

* 备份数据

* @param DBName:备份的数据库名

* @param accName:访问的用户名

* @param password:用户密码

* @param backupFilePath:备份的文件全路径

* @param sqlBinAddress:mysqlBin路径

* @throws IOException

*/

public static void backupDb(String DBName, String accName, String password, String backupFilePath, String sqlBinAddress) throws IOException {

String command = sqlBinAddress + "mysqldump -u" + accName + " -p" + password + " " + DBName;

InputStream in = null;

InputStreamReader isr = null;

BufferedReader br = null;

OutputStreamWriter writer = null;

FileOutputStream fout = null;

try {

Runtime rt = Runtime.getRuntime();

Process child = rt.exec(command);

in = child.getInputStream();// 控制台的输出信息作为输入流

isr = new InputStreamReader(in, "utf8");// 设置输出流编码为utf8。这里必须是utf8,否则从流中读入的是乱码

// 组合控制台输出信息字符串

br = new BufferedReader(isr);

String line;

StringBuilder sb = new StringBuilder("");

while ((line = br.readLine()) != null) {

sb.append(line).append("\r\n");

}

//写文件

fout = new FileOutputStream(backupFilePath);

writer = new OutputStreamWriter(fout, "utf8");

writer.write(sb.toString());

// 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免

writer.flush();

} catch (UnsupportedEncodingException ex) {

Logger.getLogger(MySqlDB.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(MySqlDB.class.getName()).log(Level.SEVERE, null, ex);

} finally {

MySqlDB.closeStream(in, isr, br, writer, fout);

}

}

/**

* 恢复数据

* @param DBName:数据库名

* @param accName:用户名

* @param passwor:密码

* @param revertFilePath:文件全路径

* @param sqlBinAddress :mysqlBin路径

*/

public static void revertDB(String DBName, String accName, String password, String revertFilePath, String sqlBinAddress) throws IOException {

String command = sqlBinAddress + "mysql -u" + accName + " -p" + password + " " + DBName;

InputStream in = null;

InputStreamReader isr = null;

BufferedReader br = null;

OutputStreamWriter writer = null;

FileOutputStream fout = null;

try {

Runtime rt = Runtime.getRuntime();

Process process = rt.exec(command);

OutputStream out = process.getOutputStream();

StringBuilder sb = new StringBuilder("");

br = new BufferedReader(new FileReader(new File(revertFilePath)));

String line = null;

while ((line = br.readLine()) != null) {

sb.append(line).append("\r\n");

}

writer = new OutputStreamWriter(out, "utf8");

writer.write(sb.toString());

writer.flush();

out.close();

br.close();

writer.close();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

MySqlDB.closeStream(in, isr, br, writer, fout);

}

}

/**

* 关闭流

* @param in

* @param isr

* @param br

* @param writer

* @param font

*/

public static void closeStream(InputStream in, InputStreamReader isr, BufferedReader br, OutputStreamWriter writer, FileOutputStream fout) throws IOException {

if (in != null) {

in.close();

}

if (isr != null) {

isr.close();

}

if (br != null) {

br.close();

}

if (writer != null) {

writer.close();

}

if (fout != null) {

fout.close();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: