?? notinpermitted.java
字號:
package file1;
/*
* 功能描述:所有對客戶級別的未入住處罰標準的操作的入口
* Author:黃順武
* Time:---
* Last Modified:2007-12-15
* Modify Reason:數據庫連接類DBConnection 的內部結構設計得到優化
*/
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import sun.jdbc.rowset.*;
public class NotInPermitted extends JPanel implements ActionListener {
private JLabel cGrade = new JLabel("客戶級別:");
private JLabel cause = new JLabel("未按時入住原因:");
private JLabel seed = new JLabel("比例系數:");
private JComboBox cGradeBox = new JComboBox();
private JComboBox causeBox = new JComboBox();
private JTextField seedTF = new JTextField(10);
private JPanel p1 = new JPanel();
private JTable recTable = null;
private String[] head = { "客戶級別", "未按時入住原因", "比例系數" };
private int headNum = 0;
private String[][] data = null;
private JScrollPane recScrollPane = null;
private JPanel p2 = new JPanel();
private JButton add = new JButton("增加記錄");
private JButton delete = new JButton("刪除記錄");
private String[] causeIDs;// 所有未入住原因的id
private String[] cGrade_ids = null;// 所有客戶級別的id
public NotInPermitted() {
headNum = head.length;
seedTF.setBorder(null);
String index = getGradesAndCauses();
if (index == null) {
return;
}
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 5));
p1.add(cGrade);
p1.add(cGradeBox);
p1.add(cause);
p1.add(causeBox);
p1.add(seed);
p1.add(seedTF);
add.setBorder(null);
add.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
p2.add(add);
p2.add(delete);
doIt();
add.addActionListener(this);
delete.addActionListener(this);
}
private String getGradesAndCauses() {
try {
DBConnection con = new DBConnection();
String query = "select id,grade from CGrade";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "沒有客戶級別,請您先添加客戶級別!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
cGrade_ids = new String[count];
crs.beforeFirst();
count = 0;
while (crs.next()) {
cGrade_ids[count++] = String.valueOf(crs.getInt(1));
cGradeBox.addItem(crs.getString(2));
}
cGradeBox.setSelectedIndex(-1);
crs = null;
query = "select* from Cause";
crs = con.getResultSet(query);
count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "沒有未入住原因,請您先添加未入住原因!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
causeIDs = new String[count];
count = 0;
crs.beforeFirst();
while (crs.next()) {
causeIDs[count++] = String.valueOf(crs.getInt(1));
causeBox.addItem(crs.getString(2));
}
causeBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
private void doIt() {
try {
DBConnection con = new DBConnection();
String sql = "select grade,cause,timesPermitted from NotInPermitted,Cause,CGrade where causeID=Cause.ID and cGradeID=CGrade.ID";
CachedRowSet crs = con.getResultSet(sql);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
delete.setEnabled(false);
} else {
delete.setEnabled(true);
}
data = new String[count][headNum];
crs.beforeFirst();
count = 0;
while (crs.next()) {
data[count][0] = crs.getString(1);
data[count][1] = crs.getString(2);
data[count][2] = String.valueOf(crs.getInt(3));
count++;
}
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 (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == add) {
int grade_id = cGradeBox.getSelectedIndex();
if (grade_id == -1) {
JOptionPane.showMessageDialog(null, "客戶級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int causeID = causeBox.getSelectedIndex();
if (causeID == -1) {
JOptionPane.showMessageDialog(null, "原因不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
String seed = seedTF.getText().trim();
int seedGet = Integer.valueOf(seed);
if (seedGet <= 0) {
JOptionPane.showMessageDialog(null, "比例系數必須為正整數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
DBConnection con = new DBConnection();
String query = "select* from NotInPermitted where cGradeID="
+ cGrade_ids[grade_id] + " and causeID="
+ causeIDs[causeID];
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "該記錄已經存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into NotInPermitted values("
+ cGrade_ids[grade_id] + "," + causeIDs[causeID] + ","
+ seedGet + ")";
con.addSql(insert);
con.doDML();
doIt();
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "比例系數必須為正整數!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == delete) {
int grade_id = cGradeBox.getSelectedIndex();
if (grade_id == -1) {
JOptionPane.showMessageDialog(null, "客戶級別不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int causeID = causeBox.getSelectedIndex();
if (causeID == -1) {
JOptionPane.showMessageDialog(null, "原因不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
DBConnection con = new DBConnection();
int confirm = JOptionPane.showConfirmDialog(null, "您真的確認刪除嗎?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
StringBuffer deleteSB = new StringBuffer(
"delete from NotInPermitted where cGradeID=");
deleteSB.append(grade_id).append(" and causeID=").append(
causeIDs[causeID]);
con.addSql(deleteSB.toString());
con.doDML();
doIt();
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -