📄 nnormalworkrecord.java
字号:
package file1;
/*
* 功能描述:所有对员工的非正常出勤类型操作的入口
* Author:黄顺武
* Time:---
* Last Modified:2007-12-15
* Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
*/
import java.awt.*;
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.*;
public class NNormalWorkRecord extends JPanel implements ActionListener,
ItemListener {
private JLabel eName = new JLabel("员工名字:");
private JLabel indentityNo = new JLabel("身份证号:");
private JLabel head = new JLabel("职称:");
private JLabel type = new JLabel("非正常出勤类型:");
private JLabel occureTime = new JLabel("非正常出勤时间:");
private JComboBox nameBox = new JComboBox();
private JTextField identityNoTF = new JTextField(15);
private JComboBox headBox = new JComboBox();
private JComboBox typeBox = new JComboBox();
private JTextField timeTF = new JTextField(10);
private JButton add = new JButton("增加记录");
private JButton modify = new JButton("修改记录");
private JButton delete = new JButton("删除记录");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JTable recTable = null;
private JScrollPane recScrollPane = null;
private String[] title = { "记录ID", "员工名字", "身份证号", "非正常出勤类型", "非正常出勤时间" };
private String[][] data = null;
private int titleNum = 0;
private String[] typeIDs = null;// 存储非正常出勤类型的ID
private String[] ids = null;// 存储非正常出勤记录的ID
private String[] identityNoS = null;// 存储某一职称的所有员工的身份证号
private String[] eIDS = null;// 存储某一职称的所有员工的ID号
private String idToModify = null;// 存储要修改的记录的ID
private GetDate getD = new GetDate();
public NNormalWorkRecord() {
String value = getNameAndType();
if (value == null) {
return;
}
getD = new GetDate();
titleNum = title.length;
identityNoTF.setEditable(false);
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
p1.add(head);
p1.add(headBox);
p1.add(eName);
p1.add(nameBox);
p1.add(indentityNo);
p1.add(identityNoTF);
p1.add(type);
p1.add(typeBox);
p1.add(occureTime);
p1.add(timeTF);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
add.setBackground(Color.LIGHT_GRAY);
add.setBorder(null);
modify.setBackground(Color.LIGHT_GRAY);
modify.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
p2.add(add);
p2.add(modify);
p2.add(delete);
headBox.addItemListener(this);
nameBox.addItemListener(this);
value = getRecord();
if (value == null) {
return;
}
add.addActionListener(this);
modify.addActionListener(this);
delete.addActionListener(this);
}
private String getNameAndType() {
try {
DBConnection con = new DBConnection();
String query = "select count(*) from Head";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
if (crs.next()) {
count = crs.getInt(1);
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "请您先添加职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
crs = null;
query = "select hName from Head";
crs = con.getResultSet(query);
count = 0;
while (crs.next()) {
headBox.addItem(crs.getString(1).trim());
}
crs = null;
headBox.setSelectedIndex(-1);
query = "select* from NNormalWork";
crs = con.getResultSet(query);
count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "请您先添加非正常出勤类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
typeIDs = new String[count];
crs.beforeFirst();
count = 0;
String type = null;
while (crs.next()) {
typeIDs[count] = String.valueOf(crs.getInt(1));
type = crs.getString(2).trim();
typeBox.addItem(type);
count++;
}
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
private String getRecord() {
try {
DBConnection con = new DBConnection();
String query = "select nWorkRecord.ID,name,IdentityNo,tName,occureTime from "
+ "nWorkRecord,NNormalWork,Employee where eID=Employee.ID and nWorkRecord.nTypeID=NNormalWork.ID";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
modify.setEnabled(false);
delete.setEnabled(false);
} else {
modify.setEnabled(true);
delete.setEnabled(true);
}
ids = new String[count];
data = new String[count][titleNum];
crs.beforeFirst();
count = 0;
while (crs.next()) {
ids[count] = String.valueOf(crs.getInt(1));
data[count][0] = ids[count];
data[count][1] = crs.getString(2).trim();
data[count][2] = crs.getString(3).trim();
data[count][3] = crs.getString(4).trim();
data[count][4] = crs.getString(5).trim();
count++;
}
recTable = new JTable(data, title);
recScrollPane = new JScrollPane(recTable);
this.setLayout(new BorderLayout(0, 20));
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
this.validate();
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == add) {
if (modify.getText().equals("确认修改")) {
modify.setText("修改记录");
}
int index = nameBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请选择员工职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String identityNo = identityNoTF.getText().trim();
if (identityNo.equals("")) {
JOptionPane.showMessageDialog(null, "请选择员工名字!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
index = typeBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请选择非正常出勤类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String eID = eIDS[nameBox.getSelectedIndex()];
String occureTime = getD.getDate(timeTF.getText().trim());
if (occureTime == null) {
JOptionPane.showMessageDialog(null, "日期输入有误!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String query = "select* from nWorkRecord where eID=" + eID
+ " and nTypeID=" + typeIDs[index] + " and occureTime='"
+ occureTime + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "以存在该记录!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
String insert = "insert into nWorkRecord values(" + eID
+ "," + typeIDs[index] + ",'" + occureTime + "')";
con.addSql(insert);
con.doDML();
getRecord();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (ae.getSource() == modify) {
if (modify.getActionCommand().equals("修改记录")) {
try {
idToModify = (String) JOptionPane.showInputDialog(null,
"请选择要修改的非正常出勤记录的ID!", "",
JOptionPane.INFORMATION_MESSAGE, null, ids, ids[0]);
if (idToModify == null) {
return;
}
DBConnection con = new DBConnection();
String query = "select head,name,tName,occureTime from Employee,nWorkRecord,NNormalWork where nWorkRecord.ID="
+ idToModify
+ " and eID=Employee.ID and nWorkRecord.nTypeID=NNormalWork.ID";
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
String head = crs.getString(1).trim();
String name = crs.getString(2).trim();
String type = crs.getString(3).trim();
String time = crs.getString(4).trim();
headBox.setSelectedItem(head);
nameBox.setSelectedItem(name);
typeBox.setSelectedItem(type);
timeTF.setText(time);
modify.setText("确认修改");
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
} else if (modify.getActionCommand().equals("确认修改")) {
int typeID = typeBox.getSelectedIndex();
if (typeID == -1) {
JOptionPane.showMessageDialog(null, "请选择非正常出勤类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String occureTime = getD.getDate(timeTF.getText().trim());
if (occureTime == null) {
JOptionPane.showMessageDialog(null, "日期输入有误!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String update = "update nWorkRecord set nTypeID="
+ typeIDs[typeID] + ",occureTime='" + occureTime
+ "' where ID=" + idToModify;
try {
DBConnection con = new DBConnection();
con.addSql(update);
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
return;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return;
}
getRecord();
modify.setText("修改记录");
}
}
if (ae.getSource() == delete) {
if (modify.getText().equals("确认修改")) {
modify.setText("修改记录");
}
String choice = (String) JOptionPane.showInputDialog(null,
"请选择要删除的非正常出勤记录的ID!", "", JOptionPane.INFORMATION_MESSAGE,
null, ids, ids[0]);
if (choice == null) {
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除该记录吗?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
String delete = "delete from nWorkRecord where ID=" + choice;
try {
DBConnection con = new DBConnection();
con.addSql(delete);
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
getRecord();
}
}
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == headBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
int index = headBox.getSelectedIndex();
if (index == -1) {
return;
}
String head = (String) headBox.getSelectedItem();
if (nameBox.getItemCount() != 0) {
nameBox.removeAllItems();
}
String query = "select ID,name,IdentityNo from Employee where head='"
+ head + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
int count = 0;
crs = con.getResultSet(query);
while (crs.next()) {
count++;
}
eIDS = new String[count];
identityNoS = new String[count];
crs.beforeFirst();
count = 0;
while (crs.next()) {
eIDS[count] = String.valueOf(crs.getInt(1));
nameBox.addItem(crs.getString(2).trim());
identityNoS[count] = crs.getString(3).trim();
count++;
}
nameBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return;
}
}
}
if (ie.getSource() == nameBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
int index = nameBox.getSelectedIndex();
if (index == -1) {
return;
}
identityNoTF.setText(identityNoS[index]);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -