⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database_dlg.java

📁 简单的实现了一个人事资源管理系统
💻 JAVA
字号:
package mypackage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataBase_Dlg extends JDialog {

	private JTextField textField;
	private JTextArea textArea;
	/**
	 * Launch the application
	 * @param args
	 */
	void bakDb()
	{
		File fBak=new File("E:\\MyJava\\Manager\\mypackage\\bak\\db.bak");//备份文件对象
		File fDb=new File("E:\\MyJava\\Manager\\mypackage\\Password.mdb");//数据库文件
		FileInputStream fin=null;//文件输入流
		FileOutputStream fout=null;//文件输出流
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try{//从数据库文件中将数据拷贝到备份文件中
			if(!fBak.exists())
				fBak.createNewFile();
			fin=new FileInputStream(fDb);
			fout=new FileOutputStream(fBak);
			
			bis=new BufferedInputStream(fin);
			bos=new BufferedOutputStream(fout);
			
			byte[] temp=new byte[128];
			int len=bis.read(temp);
			while(len!=-1){
				bos.write(temp,0,len);
				len=bis.read(temp);
			
			}
			JOptionPane.showMessageDialog(this, "备份成功", "提示", JOptionPane.INFORMATION_MESSAGE);

		}catch(Exception ex){
			JOptionPane.showMessageDialog(this, "备份失败", "提示", JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}finally{
			try{
				if(fin!=null) fin.close();
				if(fout!=null) fout.close();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
	void recoverDb()
	{
		File fBak=new File("E:\\MyJava\\Manager\\mypackage\\bak\\db.bak");//备份文件对象
		File fDb=new File("E:\\MyJava\\Manager\\mypackage\\Password.mdb");//数据库文件
		FileInputStream fin=null;//文件输入流
		FileOutputStream fout=null;//文件输出流
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try{
			if(!fBak.exists())//检查备份文件是否存在
			{
				JOptionPane.showMessageDialog(this, "目前还没有备份", "提示", JOptionPane.ERROR_MESSAGE);
				return;
			}
			//从备份文件中读取数据写到数据库文件中
			fin=new FileInputStream(fBak);
			fout=new FileOutputStream(fDb);
			
			bis=new BufferedInputStream(fin);
			bos=new BufferedOutputStream(fout);
			
			byte[] temp=new byte[128];
			int len=bis.read(temp);
			while(len!=-1){
				bos.write(temp,0,len);
				len=bis.read(temp);
			
			}
			JOptionPane.showMessageDialog(this, "数据库恢复成功", "提示", JOptionPane.INFORMATION_MESSAGE);

		}catch(Exception ex){
			JOptionPane.showMessageDialog(this, "数据库恢复失败", "提示", JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}finally{
			try{
				if(fin!=null) fin.close();
				if(fout!=null) fout.close();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
	public static void main(String args[]) {
		try {
			DataBase_Dlg dialog = new DataBase_Dlg();
			dialog.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
				//	System.out.println("退出");
				System.exit(0);
					
				}
			});
			
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}/**
	 * Create the dialog
	 */
	public DataBase_Dlg() {
		super();
		setTitle("数据库管理");
		setBounds(100, 100, 504, 398);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel, BorderLayout.CENTER);

		final JButton bakBt = new JButton();
		bakBt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				bakDb();
			}
		});
		bakBt.setBounds(50, 113, 146, 34);
		bakBt.setText("数据库备份");
		panel.add(bakBt);

		final JButton recBt = new JButton();
		recBt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				recoverDb();
			}
		});
		recBt.setBounds(50, 187, 146, 34);
		recBt.setText("数据库恢复");
		panel.add(recBt);

		textArea = new JTextArea();
		textArea.setBackground(new Color(160, 137, 252));
		textArea.setBounds(224, 194, 220, 76);
		textArea.setLineWrap(true);
		textArea.setRows(4);
		textArea.setText("从数据库备份中恢复所有数据,恢复时会覆盖已有数据,可能会造成数据丢失,小心使用!");
		textArea.setEditable(false);
		panel.add(textArea);

		textField = new JTextField();
		textField.setBackground(new Color(160, 137, 252));
		textField.setEditable(false);
		textField.setText("备份数据库,防止数据丢失,耐心等待");
		textField.setBounds(224, 113, 220, 34);
		panel.add(textField);
		//
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -