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