可以由文件对话框选择备份路径和还原文件所在路径
**需要配置MySQL环境变量
**
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
//-------------------------数据备份与还原----------------//
public class Backup extends JFrame implements ActionListener{
JPanel contentPane;
JPanel panBack = new JPanel();
JTextField txtPthBack = new JTextField("");
JTabbedPane tab = new JTabbedPane();
JPanel pnlReload = new JPanel();
JTextField txtPathReLoad = new JTextField();
JButton btnCopy = new JButton("备份");
JButton btnReload = new JButton("还原");
JButton btnCreate = new JButton("创建...");
JButton btnFind = new JButton("载入...");
private FileDialog fd;
private Connection con;
public Backup(){
setup();
//--------------注册监他听器-------------//
btnCopy.addActionListener(this);
btnReload.addActionListener(this);
btnCreate.addActionListener(this);
btnFind.addActionListener(this);
addWindowListener(new WindowCloser());
}
public void setup(){
contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
setTitle("数据备份与还原");
contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
panBack.setBorder(BorderFactory.createEtchedBorder());
panBack.setLayout(null);
txtPthBack.setBounds(new Rectangle(50, 97, 186, 30));
txtPthBack.setEditable(false);
btnCreate.setBounds(new Rectangle(253, 97, 98, 30));
pnlReload.setBorder(BorderFactory.createEtchedBorder());
pnlReload.setLayout(null);
txtPathReLoad.setBounds(new Rectangle(50, 97, 186, 30));
txtPathReLoad.setEditable(false);
btnFind.setBounds(new Rectangle(253, 97, 98, 30));
btnReload.setBounds(new Rectangle(264, 164, 80, 30));
btnCopy.setBounds(new Rectangle(264,164, 80, 30));
panBack.add(txtPthBack);
panBack.add(btnCopy);
panBack.add(btnCreate);
tab.add(panBack, "备份");
tab.add(pnlReload, "还原");
pnlReload.add(btnFind);
pnlReload.add(txtPathReLoad);
pnlReload.add(btnReload);
contentPane.add(tab, java.awt.BorderLayout.CENTER);
setResizable(false);
setSize(new Dimension(400, 300));
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-this.getWidth())/2,(screen.height-this.getHeight())/2);
setVisible(true);
}
/*行为处理**/
public void actionPerformed(ActionEvent e){
if(e.getSource()==btnCopy){
if(backup()){
JOptionPane.showMessageDialog(null,"备份成功!","提示",JOptionPane.PLAIN_MESSAGE);
}else JOptionPane.showMessageDialog(this,"备份失败!","提示",JOptionPane.ERROR_MESSAGE);
}else if(e.getSource()==btnReload){
restore();
if(restore()){
JOptionPane.showMessageDialog(null,"还原成功!","提示",JOptionPane.PLAIN_MESSAGE);
}else JOptionPane.showMessageDialog(this,"还原失败!","提示",JOptionPane.ERROR_MESSAGE);
}else if(e.getSource()==btnFind){
fd=new FileDialog(this,"还原数据库",FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile()!=null){
txtPathReLoad.setText(fd.getDirectory()+fd.getFile());
}
fd.dispose();
}else if(e.getSource()==btnCreate){
fd=new FileDialog(this,"备份数据库文件",FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile()!=null){
txtPthBack.setText(fd.getDirectory()+fd.getFile());
}
fd.dispose();
}
}
/**
* 备份数据库
* @return 成功:TRUE 失败:FALSE
*/
public boolean backup(){
String backFilePath = "";
String backDirString = txtPthBack.getText();//默认备份库
try {
backFilePath =txtPthBack.getText();//要备份的文件
File backDir = new File(backDirString);
if(!backDir.exists()){//存放库的文件夹不存在
backDir.mkdir();
}
//判断要备份的文件是否已存在
File backFile = new File(backFilePath);
if(backFile.exists()){
backFile.delete();
}
Runtime runt = Runtime.getRuntime();
Process proc = runt.exec(getBackupShellString());
int tag = proc.waitFor();//等待进程终止
if(tag==0){
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据路径生成备份数据库的Shell字符串
* @return 实际执行的shell命令
*/
public String getBackupShellString(){
String backFile = "";
SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMMddHHmmss");
Date now=new Date();
backFile = txtPthBack.getText()+"_"+myFmt.format(now)+".sql";//要备份的文件
String OSType = System.getProperty("os.name");
String shellStr = "";
if(OSType.indexOf("Windows")!=-1){
shellStr ="mysqldump -h localhost -P3306 -uroot -pscorpio --result-file="+backFile+" --default-character-set=gbk phonesell";
}else{
shellStr ="mysqldump -h localhost -P3306 -uroot -pscorpio --result-file="+backFile+" --default-character-set=gbk phonesell";
}
return shellStr;
}
/**
* 恢复数据库
* @return 成功:TRUE 失败:FALSE
*/
public boolean restore(){
String fPath="";
try {
fPath=txtPathReLoad.getText();
Runtime rt = Runtime.getRuntime();
// 调用 mysql 的 cmd:
Process child = rt.exec("mysql -uroot -pscorpio phonesell");
OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
//下面的InputStreamReader和OutputStreamWriter的第二个参数为数据的编码格式,
// 要跟备份的格式一样,否则会有异常:java.io.IOException: 管道已结束。
BufferedReader br=new BufferedReader(new InputStreamReader( new FileInputStream(fPath), "gbk"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "/r/n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "gbk");
writer.write(outStr);
writer.flush();
// 关闭输入输出流
out.close();
br.close();
writer.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/*关闭窗体*/
private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we){
Backup.this.dispose();
System.exit(0);
}
}
public static void main(String args[]){
new Backup();
}
}