📄 consultation.java
字号:
package src;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import src.SwingResourceManager;
import src.Doctor;
public class Consultation extends JFrame {
private ResultSet rs = null;
Connection conn = null;
private JComboBox comboBox_1;
private JComboBox comboBox;
private JTable table;
private DefaultTableModel dtm;
public static void main(String args[]) {
try {
Consultation frame = new Consultation();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Consultation() {
super();
setIconImage(SwingResourceManager.getImage(Consultation.class, "images/pill.gif"));
//this.setVisible(true);
this.connDB();
setTitle("科室会诊");
final String[] colHeads = { "科室", "诊断医生", "病人姓名", "性别", "年龄", "床位" };
dtm = new DefaultTableModel(colHeads, 8);
getContentPane().setLayout(null);
setBounds(100, 100, 590, 378);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(40, 90, 517, 167);
getContentPane().add(scrollPane);
table = new JTable(dtm);
scrollPane.setViewportView(table);
final JLabel label = new JLabel();
label.setText("科室");
label.setBounds(51, 31, 75, 25);
getContentPane().add(label);
comboBox = new JComboBox();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String se1 = (String) cb.getSelectedItem();
if (se1.equals("科室一")) {
comboBox_1.removeAllItems();
String office51 = new String("office51");
try {
Statement stmt = conn.createStatement();
rs = stmt
.executeQuery("select docName from doctor where officeNo='"
+ office51 + "'");
while (rs.next()) {
comboBox_1.addItem(rs.getString(1));
}
} catch (SQLException office51EX) {
System.out.println(office51EX.getMessage());
}
}
if (se1.equals("科室二")) {
comboBox_1.removeAllItems();
String office52 = new String("office52");
try {
Statement stmt = conn.createStatement();
rs = stmt
.executeQuery("select docName from doctor where officeNo='"
+ office52 + "'");
while (rs.next()) {
comboBox_1.addItem(rs.getString(1));
}
} catch (SQLException office52EX) {
System.out.println(office52EX.getMessage());
}
}
if (se1.equals("科室三")) {
comboBox_1.removeAllItems();
String office53 = new String("office53");
try {
Statement stmt = conn.createStatement();
rs = stmt
.executeQuery("select docName from doctor where officeNo='"
+ office53 + "'");
while (rs.next()) {
comboBox_1.addItem(rs.getString(1));
}
} catch (SQLException office53EX) {
System.out.println(office53EX.getMessage());
}
}
// -----------------------------------------------------------------------
if (se1.equals("科室四")) {
comboBox_1.removeAllItems();
String office54 = new String("office54");
try {
Statement stmt = conn.createStatement();
rs = stmt
.executeQuery("select docName from doctor where officeNo='"
+ office54 + "'");
while (rs.next()) {
comboBox_1.addItem(rs.getString(1));
}
} catch (SQLException office54EX) {
System.out.println(office54EX.getMessage());
}
}
// ------------------------------------------------------------
if (se1.equals("科室五")) {
comboBox_1.removeAllItems();
String office55 = new String("office55");
try {
Statement stmt = conn.createStatement();
rs = stmt
.executeQuery("select docName from doctor where officeNo='"
+ office55 + "'");
while (rs.next()) {
comboBox_1.addItem(rs.getString(1));
}
} catch (SQLException office55EX) {
System.out.println(office55EX.getMessage());
}
}
}
});
comboBox.setModel(new DefaultComboBoxModel(new String[] { "科室一", "科室二",
"科室三", "科室四", "科室五" }));
comboBox.setBounds(102, 35, 104, 28);
getContentPane().add(comboBox);
final JLabel label_1 = new JLabel();
label_1.setText("医生");
label_1.setBounds(235, 33, 65, 25);
getContentPane().add(label_1);
comboBox_1 = new JComboBox();
comboBox_1.setBounds(288, 35, 104, 27);
getContentPane().add(comboBox_1);
final JButton button = new JButton();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
try {
conn.close();
} catch (SQLException sqlEX) {
System.out.println("关闭数据库");
System.out.println(sqlEX.getMessage());
}
System.exit(0);
}
});
button.setText("退出");
button.setBounds(364, 292, 80, 24);
getContentPane().add(button);
final JLabel label_2 = new JLabel();
label_2.setText("详细信息");
label_2.setBounds(40, 65, 99, 25);
getContentPane().add(label_2);
final JButton button_1 = new JButton();
button_1.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
// 取得下拉列表框的当前选项
Object sel2 = comboBox_1.getSelectedItem();
// 判断第二个下拉列表框是否被选中
if (sel2 == null) {
JOptionPane.showMessageDialog(Doctor.frame, " 请选择医生姓名!",
"注意", JOptionPane.ERROR_MESSAGE);
}
String docName = sel2.toString();
// 取得当前Jtable中的行数
int rowcount = dtm.getRowCount() - 1;
if (rowcount != 0) {
for (int i = rowcount; i >= 0; i--) {
dtm.removeRow(i); // 删除Jtable中的所有行
}
dtm.setRowCount(0); // 将Jtable中的行数设为零
}
// ----------------------------------------------------------------
try {
Statement stmt = conn.createStatement();
// 查询所需的信息,6个字段
rs = stmt
.executeQuery("select room.officeNo,docName,sickName,sex,age,bedNo from sick,doctor,room where doctor.docNo=sick.docNo and room.roomNo=sick.roomNo and docName='"
+ docName + "'");
// 定义数组为Jtable中的一行
String[] data = new String[6];
while (rs.next()) {
for (int j = 1; j <= 6; j++) {
data[j - 1] = rs.getString(j); // 取出数据库中的数组装载到数组中
}
dtm.addRow(data); // 在Jtable中显示取出的数据
}
// insertTab(6);
} catch (SQLException office51EX) {
System.out.println(office51EX.getMessage());
}
}
});
button_1.setText("打印信息");
button_1.setBounds(251, 291, 89, 25);
getContentPane().add(button_1);
this.setResizable(false);
}
// 初始化数据库连接
private void connDB() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
System.out.println("Not Found Driver!!!");
}
try {
conn = DriverManager.getConnection("jdbc:odbc:Hospital", "", "");
} catch (SQLException e) {
System.out.println("Link Error");
}
}
/*
* public void insertTab(int num){ int j=0; try{ while(rs.next()){
* rowCount++; data=new Vector<String>(); for(int i=1;i<=num;i++) {
* data.add(rs.getString(i)); } dtm.insertRow(j++,data); }
* }catch(SQLException e){ System.out.println("Link Error"); } }
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -