?? notincause.java
字號:
package file1;
/*
* 功能描述:所有對客戶未按時入住原因的操作的入口
* Author:黃順武
* Time:---
* Last Modified:2007-12-15
* Modify Reason:數據庫連接類DBConnection 的內部結構設計得到優化
*/
import java.sql.*;
import sun.jdbc.rowset.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NotInCause extends JPanel implements ActionListener {
private JLabel cause = new JLabel("未按時入住原因:");
private JTextField causeTF = new JTextField(10);
private JPanel p1 = new JPanel();
private JTable recTable = null;
private String[] head = { "ID", "未按時入住原因" };
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;
public NotInCause() {
headNum = head.length;
causeTF.setBorder(null);
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 5));
p1.add(cause);
p1.add(causeTF);
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 void doIt() {// 返回1表示沒有記錄或查詢出錯,返回0表示非前面兩種情況
try {
DBConnection con = new DBConnection();
String sql = "select* from Cause";
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();
causeIDs = new String[count];
count = 0;
while (crs.next()) {
String id = String.valueOf(crs.getInt(1));
causeIDs[count] = id;
data[count][0] = id;
data[count][1] = crs.getString(2).trim();
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) {
String cause = causeTF.getText().trim();
if (cause.equals("")) {
JOptionPane.showMessageDialog(null, "原因不能為空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
try {
DBConnection con = new DBConnection();
String query = "select* from Cause where cause='" + cause + "'";
CachedRowSet crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "該記錄已經存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into Cause values('" + cause + "')";
con.addSql(insert);
con.doDML();
doIt();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == delete) {
String causeID = (String) JOptionPane.showInputDialog(null,
"請您選擇要刪除的原因ID!", "", JOptionPane.INFORMATION_MESSAGE, null,
causeIDs, causeIDs[0]);
if (cause == null) {
return;
}
String query = "select* from Cause where ID=" + causeID;
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 == JOptionPane.YES_OPTION) {
String delete = "delete from Cause where ID=" + causeID;
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 + -