📄 notinclientd.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 NotInClientD extends JPanel implements ActionListener {
private JLabel cName = new JLabel("客户名称:");
private JLabel cause = new JLabel("原因:");
private JLabel shouldIn = new JLabel("应入住时间:");
private JComboBox nameBox;
private JComboBox causeBox;
private JTextField shouldInTF = 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 GetDate getD = null;
private JButton add = new JButton("增加记录");
private JButton delete = new JButton("删除记录");
private String[] causeIDs = null;// 存储未入住原因ID
private String[] ids = null;// 存储所有用户的ID
public NotInClientD() {
headNum = head.length;
getD = new GetDate();
shouldInTF.setBorder(null);
nameBox = new JComboBox();
causeBox = new JComboBox();
p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
add.setBorder(null);
add.setBackground(Color.LIGHT_GRAY);
delete.setBorder(null);
delete.setBackground(Color.LIGHT_GRAY);
p2.add(add);
p2.add(delete);
String index = getAllCNames();
if (index == null) {
return;
}
doIt();
add.addActionListener(this);
delete.addActionListener(this);
}
private String getAllCNames() {
try {
DBConnection con = new DBConnection();
String query = "select ID,cName from Client";
CachedRowSet crs = con.getResultSet(query);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "数据库中没客户记录,请先添加客户!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
ids = new String[count];
count = 0;
crs.beforeFirst();
while (crs.next()) {
ids[count++] = String.valueOf(crs.getInt(1));
nameBox.addItem(crs.getString(2));
}
query = "select* from Cause";
crs = null;
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];
crs.beforeFirst();
count = 0;
while (crs.next()) {
causeIDs[count++] = String.valueOf(crs.getInt(1));
causeBox.addItem(crs.getString(2));
}
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
p1.add(cName);
p1.add(nameBox);
p1.add(cause);
p1.add(causeBox);
p1.add(shouldIn);
p1.add(shouldInTF);
} 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 Client.cName,cause,shouldInTime from Client,CNotIn,Cause where Client.ID=CNotIn.clientID and causeID=Cause.ID";
CachedRowSet crs = con.getResultSet(sql);
int row = 0;
while (crs.next()) {
row++;
}
if (row == 0) {
JOptionPane.showMessageDialog(null, "当前没有未入住客户!", "提示",
JOptionPane.INFORMATION_MESSAGE);
delete.setEnabled(false);
} else {
delete.setEnabled(true);
}
data = new String[row][headNum];
crs.beforeFirst();
row = 0;
while (crs.next()) {
data[row][0] = crs.getString(1);
data[row][1] = crs.getString(2);
data[row][2] = crs.getString(3);
row++;
}
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 name_index = nameBox.getSelectedIndex();
int causeID = causeBox.getSelectedIndex();
if (shouldInTF.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "日期不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String shouldinTime = getD.getDate(shouldInTF.getText().trim());
if (shouldinTime == null) {
JOptionPane.showMessageDialog(null,
"日期类型必须为如2007-09-07的格式,且不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (name_index == -1 || causeID == -1) {
JOptionPane.showMessageDialog(null, "名字或原因不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String query = "select* from CNotIn where clientID="
+ ids[name_index] + " and causeID=" + causeIDs[causeID]
+ " and shouldInTime='" + shouldinTime + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (crs.next()) {
JOptionPane.showMessageDialog(null, "该记录已经存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insert = "insert into CNotIn values(" + ids[name_index]
+ "," + causeIDs[causeID] + ",'" + shouldinTime + "')";
con.addSql(insert);
con.doDML();
doIt();
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (e.getSource() == delete) {
int name_index = nameBox.getSelectedIndex();
int causeID = causeBox.getSelectedIndex();
String shouldinTime = getD.getDate(shouldInTF.getText().trim());
if (shouldinTime == null) {
JOptionPane.showMessageDialog(null,
"日期类型必须为如2007-09-07的格式,且不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (name_index == -1 || causeID == -1) {
JOptionPane.showMessageDialog(null, "名字或原因不能为空!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String query = "select* from CNotIn where clientID="
+ ids[name_index] + " and causeID=" + causeIDs[causeID]
+ " and shouldInTime='" + shouldinTime + "'";
CachedRowSet crs = null;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(query);
if (!crs.next()) {
JOptionPane.showMessageDialog(null, "该记录不存在!", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String delete = "delete from CNotIn where clientID="
+ ids[name_index] + " and causeID=" + causeIDs[causeID]
+ " and shouldInTime='" + shouldinTime + "'";
int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
"", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
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 + -