📄 etitle.java
字号:
package file1;
/*
* 功能描述:所有对员工职称的操作的入口
* @Author:黄顺武
* Time:---
* 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 ETitle extends JPanel implements ActionListener {
private JLabel title = new JLabel("职称:");
private JLabel salary = new JLabel("月薪:");
private JLabel type = new JLabel("奖金派发方式:");
private JLabel money = new JLabel("每次派发奖金额:");
private JTextField titleTF = new JTextField(10);
private JTextField salaryTF = new JTextField(10);
private JTextField typeTF = new JTextField(10);
private JTextField moneyTF = new JTextField(10);
private JPanel p1 = new JPanel();
private JTable recordTable = new JTable();
private String[] head = { "职称", "月薪", "奖金派发方式", "每次派发奖金额" };
private int headNum = 0;
private String tableData[][] = null;
private JScrollPane recScrollPane;
private JPanel p3 = new JPanel();
private JButton add = new JButton("增加记录");
private JButton delete = new JButton("删除记录");
private String[] eTitles = null;
public ETitle() {
titleTF.setBorder(null);
moneyTF.setBorder(null);
salaryTF.setBorder(null);
typeTF.setBorder(null);
headNum = head.length;
this.setLayout(new BorderLayout(0, 5));
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
p1.add(title);
p1.add(titleTF);
p1.add(salary);
p1.add(salaryTF);
p1.add(type);
p1.add(typeTF);
p1.add(money);
p1.add(moneyTF);
p3.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
add.setBorder(null);
add.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
p3.add(add);
p3.add(delete);
doIt();
add.addActionListener(this);
delete.addActionListener(this);
}
private void doIt() {// 返回1表示没有记录或查询出错,返回0表示非前面两种情况
try {
DBConnection con = new DBConnection();
String sql = "select* from Head";
CachedRowSet crs = con.getResultSet(sql);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
delete.setEnabled(false);
} else {
delete.setEnabled(true);
}
tableData = new String[count][headNum];
eTitles = new String[count];
crs.beforeFirst();
count = 0;
int row = 0;
while (crs.next()) {
String title = crs.getString(1);
eTitles[count] = title;
tableData[row][0] = title;
tableData[row][1] = String.valueOf(crs.getFloat(2));
tableData[row][2] = crs.getString(3);
tableData[row][3] = String.valueOf(crs.getFloat(4));
count++;
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) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == add) {
String title = titleTF.getText().trim();
if (title.equals("")) {
JOptionPane.showMessageDialog(null, "职称不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String type = typeTF.getText().trim();
if (type.equals("")) {
JOptionPane.showMessageDialog(null, "奖金派发方式不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
float salary = Float.valueOf(salaryTF.getText().trim());
float m = Float.valueOf(moneyTF.getText().trim());
DBConnection con = new DBConnection();
String query = "select* from Head where hName='" + title + "'";
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "该记录已经存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into Head values('" + title + "',"
+ salary + ",'" + type + "'," + m + ")";
con.addSql(insert);
con.doDML();
doIt();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "月薪,奖金额都必须为数字!", "提示",
JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == delete) {
String titleGet = (String) JOptionPane.showInputDialog(null,
"请选择要删除的职称!", "", JOptionPane.INFORMATION_MESSAGE, null,
eTitles, eTitles[0]);
if (titleGet == null) {
return;
}
String query = "select* from Head where hName='" + titleGet + "'";
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 == -1 || confirm == JOptionPane.NO_OPTION) {
return;
}
String delete = "delete from Head where hName='" + titleGet
+ "'";
con.addSql(delete);
con.doDML();
doIt();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -