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