?? notnormalworktype.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;
import java.sql.SQLException;
public class NotNormalWorkType extends JPanel implements ActionListener {
private JLabel type = new JLabel("非正常出勤類型:");
private JTextField typeTF = new JTextField(10);
private JTable recTable = null;
private JScrollPane recScrollPane = null;
private JButton add = new JButton("增加記錄");
private JButton delete = new JButton("刪除記錄");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private String[] head = { "ID", "非正常出勤類型" };
private int headNum = 0;
private String[][] data = null;
private String[] typeIDs = null;
public NotNormalWorkType() {
headNum = head.length;
String valueGet = getRecord();
if (valueGet == null) {
return;
}
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
p1.add(type);
p1.add(typeTF);
add.setBackground(Color.LIGHT_GRAY);
add.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
p2.add(add);
p2.add(delete);
add.addActionListener(this);
delete.addActionListener(this);
}
private String getRecord() {
try {
DBConnection con = new DBConnection();
String query = "select* from NNormalWork";
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][headNum];
typeIDs = new String[count];
crs.beforeFirst();
int arrayIndex = 0;
while (crs.next()) {
String id = String.valueOf(crs.getInt(1));
typeIDs[arrayIndex] = id;
data[arrayIndex][0] = id;
data[arrayIndex][1] = crs.getString(2).trim();
arrayIndex++;
}
recTable = new JTable(data, head);
recScrollPane = new JScrollPane(recTable);
this.setLayout(new BorderLayout(0, 15));
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
this.validate();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
}
return "success";
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == add) {
String type = typeTF.getText().trim();
if (type.equals("")) {
JOptionPane.showMessageDialog(null, "類型不能為空!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String query = "select* from NNormalWork where tName='" + type
+ "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "該類型已存在!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
StringBuffer insertSB = new StringBuffer(
"insert into NNormalWork values('");
insertSB.append(type).append("')");
con.addSql(insertSB.toString());
con.doDML();
getRecord();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (ae.getSource() == delete) {
String typeStr = (String) JOptionPane.showInputDialog(null,
"請選擇要刪除的非正常出勤類型ID!", "", JOptionPane.INFORMATION_MESSAGE,
null, typeIDs, typeIDs[0]);
if (typeStr == null) {
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的確認刪除該記錄嗎?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
DBConnection con = new DBConnection();
StringBuffer deleteSB = new StringBuffer(
"delete from NNormalWork where ID=");
deleteSB.append(typeStr);
con.addSql(deleteSB.toString());
con.doDML();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
getRecord();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -