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

📄 employee.java

📁 JAVA实现的酒店管理系统
💻 JAVA
字号:
package file1;

/*
 * 功能描述:所有对员工的操作的入口
 * @Author:黄顺武
 * Time:2007-12-1
 * Last Modified:2007-12-15
 * Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
 */
import java.sql.*;
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Employee extends JPanel implements ActionListener {

	private JLabel name = new JLabel("名字:");
	private JLabel gender = new JLabel("性别:");
	private JLabel age = new JLabel("年龄:");
	private JLabel cardNo = new JLabel("身份证号:");
	private JLabel title = new JLabel("职称:");
	private JLabel department = new JLabel("所属部门:");
	private JComboBox departmentBox = new JComboBox();
	private JTextField nameTF = new JTextField(10);
	private JComboBox genderBox = new JComboBox(new String[] { "男", "女" });
	private JTextField ageTF = new JTextField(10);
	private JTextField cardNoTF = new JTextField(10);
	private JComboBox titleBox = new JComboBox();
	private JPanel p1 = new JPanel();
	private JTable recordTable = new JTable();
	private JScrollPane recScrollPane;
	private JPanel p3 = new JPanel();
	private JButton add = new JButton("增加记录");
	private JButton modify = new JButton("修改记录");
	private JButton delete = new JButton("删除记录");
	private String[] IDS;
	private String[] dpids = null;// 存储部门的所有ID
	private String[] head = { "ID", "员工名字", "性别", "年龄", "身份证号", "职称", "所属部门",
			"月薪" };
	private int headNum = 0;
	private String tableData[][] = null;

	public Employee() {

		nameTF.setBorder(null);
		ageTF.setBorder(null);
		cardNoTF.setBorder(null);
		String index = getTitlesAndDPs();
		if (index.equals("failure")) {
			return;
		}
		headNum = head.length;
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
		p1.add(title);
		p1.add(titleBox);
		p1.add(department);
		p1.add(departmentBox);
		p1.add(name);
		p1.add(nameTF);
		p1.add(gender);
		p1.add(genderBox);
		p1.add(age);
		p1.add(ageTF);
		p1.add(cardNo);
		p1.add(cardNoTF);
		p3.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		add.setBorder(null);
		add.setBackground(Color.LIGHT_GRAY);
		modify.setBorder(null);
		modify.setBackground(Color.LIGHT_GRAY);
		delete.setBorder(null);
		delete.setBackground(Color.LIGHT_GRAY);
		p3.add(add);
		p3.add(modify);
		p3.add(delete);
		this.setLayout(new BorderLayout(0, 15));
		doIt();
		modify.addActionListener(this);
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	private String getTitlesAndDPs() {
		String sql = "select hName from Head";
		CachedRowSet crs = null;
		int count = 0;
		try {
			DBConnection con = new DBConnection();
			crs = con.getResultSet(sql);
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "数据库中没职称记录,请先添加职称!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return "failure";
			}
			crs.beforeFirst();
			while (crs.next()) {
				titleBox.addItem(crs.getString(1));
			}
			crs = null;
			count = 0;
			sql = "select* from Department";
			crs = con.getResultSet(sql);
			while (crs.next()) {
				count++;
			}
			dpids = new String[count];
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "数据库中没部门记录,请先添加部门!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return "failure";
			}
			crs.beforeFirst();
			count = 0;
			while (crs.next()) {
				dpids[count++] = String.valueOf(crs.getInt(1));
				departmentBox.addItem(crs.getString(2));
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
		return "success";
	}

	private String doIt() {
		try {
			DBConnection con = new DBConnection();
			String sql = "select Employee.ID,name,gender,age,IdentityNo,head,dpName,salary from Employee,Department,Head where dpID=Department.ID and  head=hName";
			CachedRowSet crs = con.getResultSet(sql);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				modify.setEnabled(false);
				delete.setEnabled(false);
			} else {
				modify.setEnabled(true);
				delete.setEnabled(true);
			}
			tableData = new String[count][headNum];
			IDS = new String[count];
			crs.beforeFirst();
			count = 0;
			int row = 0;
			while (crs.next()) {
				IDS[count++] = String.valueOf(crs.getInt(1));
				tableData[row][0] = String.valueOf(crs.getInt(1));
				tableData[row][1] = crs.getString(2);
				tableData[row][2] = crs.getString(3);
				tableData[row][3] = String.valueOf(crs.getInt(4));
				tableData[row][4] = crs.getString(5);
				tableData[row][5] = crs.getString(6);
				tableData[row][6] = crs.getString(7);
				tableData[row][7] = String.valueOf(crs.getFloat(8));
				row++;
			}
			recordTable = new JTable(tableData, head);
			recordTable.setRowHeight(20);
			recScrollPane = new JScrollPane(recordTable);
			this.add(p1, BorderLayout.NORTH);
			this.add(recScrollPane, BorderLayout.CENTER);
			this.add(p3, BorderLayout.SOUTH);
			this.validate();
		} catch (SQLException sqle) {
			return null;
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			return null;
		}
		return "success";
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == add) {
			if (modify.getText().equals("确认修改")) {
				modify.setText("修改记录");
			}
			String name = nameTF.getText().trim();
			if (name.equals("")) {
				JOptionPane.showMessageDialog(null, "名字不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int gIndex = genderBox.getSelectedIndex();
			String gender = (String) genderBox.getSelectedItem();
			if (gIndex == -1) {
				JOptionPane.showMessageDialog(null, "请您选择性别!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int age;
			try {
				age = Integer.valueOf(ageTF.getText().trim());
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(null, "年龄必须为正整数!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String cardNo = cardNoTF.getText().trim();
			if (cardNo.equals("")) {
				JOptionPane.showMessageDialog(null, "身份证号不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String card = check(cardNo);
			if (card == null) {
				return;
			}
			int index1 = titleBox.getSelectedIndex();
			int index2 = departmentBox.getSelectedIndex();
			if (index1 == -1 || index2 == -1) {
				JOptionPane.showMessageDialog(null, "请您选择职称和部门!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String title = (String) titleBox.getSelectedItem();
			String query = "select* from Employee where IdentityNo='" + card
					+ "'";
			CachedRowSet crs = null;
			try {
				DBConnection con = new DBConnection();
				crs = con.getResultSet(query);
				if (crs.next()) {
					JOptionPane.showMessageDialog(null, "该员工已经存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String insert = "insert into Employee values('" + name + "','"
						+ gender + "'," + age + ",'" + card + "','" + title
						+ "'," + dpids[index2] + ")";
				con.addSql(insert);
				con.doDML();
				doIt();
				modify.setEnabled(true);
				delete.setEnabled(true);
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
		if (e.getSource() == modify) {
			if (e.getActionCommand().equals("修改记录")) {
				String idGet = (String) JOptionPane.showInputDialog(null,
						"请选择要修改的员工记录的ID!", "", JOptionPane.INFORMATION_MESSAGE,
						null, IDS, IDS[0]);
				if (idGet == null) {
					return;
				}
				String query = "select* from Employee where ID=" + idGet;
				CachedRowSet crs = null;
				try {
					DBConnection con = new DBConnection();
					crs = con.getResultSet(query);
					while (crs.next()) {
						crs.getInt(1);
						nameTF.setText(crs.getString(2));
						nameTF.setEditable(false);
						genderBox.setSelectedItem(crs.getString(3));
						ageTF.setText(String.valueOf(crs.getInt(4)));
						cardNoTF.setText(crs.getString(5));
						cardNoTF.setEditable(false);
						titleBox.setSelectedItem(crs.getString(6));
						for (int i = 0; i < dpids.length; i++) {
							if (dpids[i].equals(String.valueOf(crs.getInt(7)))) {
								departmentBox.setSelectedIndex(i);
							}
						}
						modify.setText("确认修改");
					}
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				} catch (ClassNotFoundException cnfe) {
					cnfe.printStackTrace();
				}

			} else if (e.getActionCommand().equals("确认修改")) {
				int age;
				try {
					age = Integer.valueOf(ageTF.getText().trim());
				} catch (NumberFormatException nfe) {
					JOptionPane.showMessageDialog(null, "年龄必须为正整数!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String cardNo = cardNoTF.getText().trim();
				String card = check(cardNo);
				if (card == null) {
					return;
				}
				int index = titleBox.getSelectedIndex();
				if (index == -1) {
					JOptionPane.showMessageDialog(null, "请您选择职称!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String title = (String) titleBox.getSelectedItem();
				int genderIndex = genderBox.getSelectedIndex();
				if (genderIndex == -1) {
					JOptionPane.showMessageDialog(null, "请您选择性别!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if (departmentBox.getSelectedIndex() == -1) {
					JOptionPane.showMessageDialog(null, "请您选择所属部门!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				DBConnection con = new DBConnection();
				String gender = (String) genderBox.getSelectedItem();
				String update = "update Employee set age=" + age + ",gender='"
						+ gender + "',head='" + title + "',dpID="
						+ dpids[departmentBox.getSelectedIndex()]
						+ " where IdentityNo='" + card + "'";
				con.addSql(update);
				try {
					con.doDML();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				} catch (ClassNotFoundException cnfe) {
					cnfe.printStackTrace();
				}
				doIt();
				modify.setText("修改记录");
			}
		}
		if (e.getSource() == delete) {
			if (modify.getText().equals("确认修改")) {
				modify.setText("修改记录");
			}
			String idGet = (String) JOptionPane.showInputDialog(null,
					"请选择要删除的员工记录的ID!", "", JOptionPane.INFORMATION_MESSAGE,
					null, IDS, IDS[0]);
			if (idGet == null) {
				return;
			}
			String query = "select* from Employee where ID=" + idGet;
			CachedRowSet crs = null;
			try {
				DBConnection con = new DBConnection();
				crs = con.getResultSet(query);
				if (!crs.next()) {
					JOptionPane.showMessageDialog(null, "该记录不存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
						"", JOptionPane.YES_NO_OPTION);
				if (confirm == JOptionPane.YES_OPTION) {
					String delete = "delete from Employee where ID=" + idGet;
					con.addSql(delete);
					con.doDML();
					doIt();
				}
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
	}

	private String check(String value) {
		int len = value.length();
		for (int i = 0; i < len; i++) {
			if (!Character.isDigit(value.charAt(i))) {
				JOptionPane.showMessageDialog(null, "身份证号必须为数字!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return null;
			}
		}
		if (len != 18) {
			JOptionPane.showMessageDialog(null, "身份证号必须为18位!", "提示",
					JOptionPane.INFORMATION_MESSAGE);
			return null;
		}
		return value;
	}
}

⌨️ 快捷键说明

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