package cn.wizool.mysqlbak.frame;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import cn.wizool.mysqlbak.connnection.Dbconnection;
import cn.wizool.mysqlbak.util.MysqlUtil;
public class MainFrame extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 1L;
private JButton bakupButton = null, restoreButton = null,
testButton = null;
private JLabel hostLable = null, portLable = null, nameLable = null,
passwordLable = null, dbLable = null;
private JTextField hostfield = null, portfield = null, namefield = null;
private JPasswordField passwordfield = null;
private JComboBox jcb = null;
private ArrayList<String> attachArrayList = new ArrayList<String>();// 用于存储附件路径的链表
MysqlUtil mysqlutil = new MysqlUtil();
Dbconnection dbcnn = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
private String date = sdf.format(new Date());
public MainFrame() {
super();
getContentPane().setLayout(null);
jFrameValidate();
setTitle("Mysql备份还原工具");
JLabel backgroundLabel = new JLabel();
backgroundLabel.setBounds(0, 0, 350, 300);
backgroundLabel.setText("<html><img width=350 height=300 src='"
+ this.getClass().getResource("/mysqlbak.jpg") + "'></html>");
backgroundLabel.setLayout(null);
hostLable = new JLabel();
hostLable.setText("服务器地址:");
hostLable.setFont(new java.awt.Font("宋体", 0, 12));
hostLable.setBounds(20, 75, 100, 25);
backgroundLabel.add(hostLable);
portLable = new JLabel();
portLable.setText("端口号:");
portLable.setFont(new java.awt.Font("宋体", 0, 12));
portLable.setBounds(20, 105, 100, 25);
backgroundLabel.add(portLable);
nameLable = new JLabel();
nameLable.setText("用户名:");
nameLable.setFont(new java.awt.Font("宋体", 0, 12));
nameLable.setBounds(20, 135, 100, 25);
backgroundLabel.add(nameLable);
passwordLable = new JLabel();
passwordLable.setText("密码:");
passwordLable.setFont(new java.awt.Font("宋体", 0, 12));
passwordLable.setBounds(20, 165, 100, 25);
backgroundLabel.add(passwordLable);
dbLable = new JLabel();
dbLable.setText("数据库:");
dbLable.setFont(new java.awt.Font("宋体", 0, 12));
dbLable.setBounds(20, 230, 100, 26);
backgroundLabel.add(dbLable);
hostfield = new JTextField("localhost");
hostfield.setBounds(190, 75, 140, 25);
backgroundLabel.add(hostfield);
portfield = new JTextField("3306");
portfield.setBounds(190, 105, 140, 25);
backgroundLabel.add(portfield);
namefield = new JTextField("root");
namefield.setBounds(190, 135, 140, 25);
backgroundLabel.add(namefield);
passwordfield = new JPasswordField();
passwordfield.setBounds(190, 165, 140, 25);
backgroundLabel.add(passwordfield);
new DefaultListModel();
jcb = new JComboBox();
jcb.setBounds(70, 230, 100, 25);
// jcb.setEditable(true);可编辑
backgroundLabel.add(jcb);
testButton = new JButton("连接测试");
bakupButton = new JButton("备份");
restoreButton = new JButton("还原");
backgroundLabel.add(testButton);
backgroundLabel.add(bakupButton);
backgroundLabel.add(restoreButton);
// testButton.setBounds(20, 230, 100, 26);
testButton.setBounds(190, 195, 100, 25);
bakupButton.setBounds(190, 230, 65, 26);
restoreButton.setBounds(260, 230, 65, 26);
testButton.addActionListener(this);
bakupButton.addActionListener(this);
restoreButton.addActionListener(this);
getContentPane().add(backgroundLabel);
}
/**
* 计算屏幕的宽和高
*/
public void jFrameValidate() {
Toolkit tk = getToolkit();// 获得屏幕的宽和高
Dimension dim = tk.getScreenSize();
this.setResizable(false);
this.setBounds(dim.width / 2 - 380, dim.height / 2 - 270, 350, 300);
validate();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bakupButton) {
bakupButton.setEnabled(false);
restoreButton.setEnabled(false);
bakup();// 备份
} else if (e.getSource() == restoreButton) {
bakupButton.setEnabled(false);
restoreButton.setEnabled(false);
recover();// 还原
} else if (e.getSource() == testButton) {
bakupButton.setEnabled(false);
restoreButton.setEnabled(false);
if (init()) {
testConnect();// 测试连接
}
}
}
// 初始化
private Boolean init() {
String host = hostfield.getText().trim();
String port = portfield.getText().trim();
String name = namefield.getText().trim();
// String database = dbfield.getText().trim();
String password = passwordfield.getText().trim();
String url = "jdbc:mysql://" + host + ":" + port + "/test";
if (password == null || password.equals("") || host == null
|| host.equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整信息!!!");
bakupButton.setEnabled(true);
restoreButton.setEnabled(true);
validate();
repaint();
return false;
} else {
dbcnn = new Dbconnection(host, name, password, "test", url);
dbcnn.init();
return true;
}
}
// 测试连接
private void testConnect() {
try {
if (dbcnn.test()) {
attachArrayList = dbcnn.SelectDB();
jcb.removeAllItems();
for (Iterator i = attachArrayList.iterator(); i.hasNext();) {
jcb.addItem(i.next());
}
JOptionPane.showMessageDialog(this, "连接成功");
} else {
JOptionPane.showMessageDialog(this, "连接失败");
}
bakupButton.setEnabled(true);
restoreButton.setEnabled(true);
validate();
repaint();
} catch (HeadlessException e) {
e.printStackTrace();
}
}
// 备份操作
private void bakup() {
try {
String host = hostfield.getText();// 服务器ip
String port = portfield.getText();// 端口
String name = namefield.getText();
String password = passwordfield.getText();
String dbname = (String) jcb.getSelectedItem();// 获取选择的数据库名称
if (dbname != null && !("").equals(dbname)) {
FileFilter filter_txt = new FileNameExtensionFilter(
"文本文档(*.txt)", "txt");
FileFilter filter_sql = new FileNameExtensionFilter(
"sql脚本(*.sql)", "sql");
File f = new File(".");// 得到当前目录
JFileChooser chooser = new JFileChooser(f);// 构造一个当前路径的文件选择器
chooser.addChoosableFileFilter(filter_sql);// 添加默认文件格式sql和txt
chooser.addChoosableFileFilter(filter_txt);
JTextField jtf = this.getTextField(chooser);
jtf.setText("wms_" + date + ".sql");
if (chooser.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION) {// 如果选择确定键
File file = chooser.getSelectedFile();
if (file.getName() != null) {
mysqlutil.bakup(file.getPath(), host, name, password,
port, dbname);
}
} else if (chooser.showSaveDialog(getContentPane()) == JFileChooser.CANCEL_OPTION) {
}
} else {
JOptionPane.showMessageDialog(thi