📄 nnwpunishment.java
字号:
package file1;
/*
* 功能描述:所有对员工的非正常出勤类型操作的入口
* Author:黄顺武
* Time:---
* Last Modified:2007-12-15
* Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
*/
import java.awt.*;
import sun.jdbc.rowset.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NNWPunishment extends JPanel implements ActionListener {
private JLabel head = new JLabel("员工职称:");
private JLabel type = new JLabel("非正常出勤类型:");
private JLabel punishment = new JLabel("罚款:");
private JComboBox headBox = new JComboBox();
private JComboBox typeBox = new JComboBox();
private JTextField punishmentTF = new JTextField(10);
private JButton add = new JButton("添加记录");
private JButton delete = new JButton("删除记录");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JTable recTable = null;
private JScrollPane recPane = null;
private String[] title = { "员工职称", "非正常出勤类型", "罚款" };
private int titleNum = 0;
private String[][] data = null;
private String[] typeIDs = null;
public NNWPunishment() {
String valueReturned = getHeadAndType();
if (valueReturned == null) {
return;
}
titleNum = title.length;
punishmentTF.setBorder(null);
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
p1.add(head);
p1.add(headBox);
p1.add(type);
p1.add(typeBox);
p1.add(punishment);
p1.add(punishmentTF);
add.setBackground(Color.LIGHT_GRAY);
add.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 0));
p2.add(add);
p2.add(delete);
valueReturned = getRecord();
if (valueReturned == null) {
return;
}
add.addActionListener(this);
delete.addActionListener(this);
}
private String getHeadAndType() {
try {
DBConnection con = new DBConnection();
String query = "select hName from Head";
CachedRowSet crs = con.getResultSet(query);
while (crs.next()) {
headBox.addItem(crs.getString(1).trim());
}
headBox.setSelectedIndex(-1);
crs = null;
query = "select* from NNormalWork";
crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count++;
}
typeIDs = new String[count];
crs.beforeFirst();
String type = null;
int id = 0;
count = 0;
while (crs.next()) {
id = crs.getInt(1);
typeIDs[count++] = String.valueOf(id);
type = crs.getString(2).trim();
typeBox.addItem(type);
}
typeBox.setSelectedIndex(-1);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
}
return "success";
}
private String getRecord() {
try {
DBConnection con = new DBConnection();
String query = "select eTitle,tName,nnPunishment from NNPunishment,NNormalWork "
+ "where nnTypeID=NNormalWork.ID";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
delete.setEnabled(false);
} else {
delete.setEnabled(true);
}
data = new String[count][titleNum];
crs.beforeFirst();
count = 0;
while (crs.next()) {
data[count][0] = crs.getString(1).trim();
data[count][1] = crs.getString(2).trim();
data[count][2] = String.valueOf(crs.getFloat(3));
count++;
}
recTable = new JTable(data, title);
recPane = new JScrollPane(recTable);
this.setLayout(new BorderLayout(0, 15));
this.add(p1, BorderLayout.NORTH);
this.add(recPane, 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 (!punishmentTF.isEditable()) {
punishmentTF.setEditable(true);
}
String head = (String) headBox.getSelectedItem();
if (head == null) {
JOptionPane.showMessageDialog(null, "请选择职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int typeIndex = typeBox.getSelectedIndex();
if (typeIndex == -1) {
JOptionPane.showMessageDialog(null, "请选择非正常出勤类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
String punishment = punishmentTF.getText().trim();
float temp = Float.valueOf(punishment);
if (temp < 0) {
JOptionPane.showMessageDialog(null, "罚款金额必须为大于等于零的数字!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
DBConnection con = new DBConnection();
String query = "select* from NNPunishment where eTitle='"
+ head + "' and nnTypeID=" + typeIDs[typeIndex];
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "该记录已存在!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into NNPunishment values('" + head
+ "'," + typeIDs[typeIndex] + "," + temp + ")";
con.addSql(insert);
con.doDML();
getRecord();
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "罚款金额必须为大于等于零的数字!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (ae.getSource() == delete) {
String head = (String) headBox.getSelectedItem();
if (head == null) {
JOptionPane.showMessageDialog(null, "请选择职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int index = typeBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请选择非正常出勤类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String type = (String) typeBox.getSelectedItem();
String query = "select nnPunishment from NNPunishment where eTitle='"
+ head + "' and nnTypeID=" + typeIDs[index];
DBConnection con = new DBConnection();
CachedRowSet crs = null;
try {
crs = con.getResultSet(query);
if (crs.next()) {
String temp = String.valueOf(crs.getFloat(1));
punishmentTF.setText(temp);
}
punishmentTF.setEditable(false);
} catch (SQLException sqle) {
sqle.printStackTrace();
return;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除 ("
+ head + "," + type + "," + punishmentTF.getText().trim()
+ ") 这条记录" + "吗?", "", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
StringBuffer sqlSB = new StringBuffer(
"delete from NNPunishment where eTitle='");
sqlSB.append(head).append("' and nnTypeID=").append(
typeIDs[index]);
con.addSql(sqlSB.toString());
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
getRecord();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -