?? employee.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 + -