欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java+Swing實現(xiàn)醫(yī)院管理系統(tǒng)的完整代碼

 更新時間:2021年05月06日 10:14:26   作者:水堅石青  
這篇文章主要介紹了Java+Swing實現(xiàn)醫(yī)院管理系統(tǒng)的完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、系統(tǒng)介紹

本系統(tǒng)實現(xiàn)的以下功能
管理員功能:登錄系統(tǒng)、病人信息的增刪改查、就醫(yī)檔案的錄入、醫(yī)生信息的增刪改查、科室信息的增刪改查、收費統(tǒng)計功能、修改密碼。
醫(yī)生功能:登錄系統(tǒng)、病人信息的增刪改查、醫(yī)生信息的增刪改查、科室信息的增刪改查、收費統(tǒng)計功能、修改密碼。
收費員功能:價格管理、收費管理、修改密碼。
JDK版本:1.8
數(shù)據(jù)庫:Mysql8.0.13

數(shù)據(jù)庫用到的表
cashier
charge
department
doctor
drugtable
manager
medical_records
patient
price

工程截圖

在這里插入圖片描述

二、系統(tǒng)展示

1.登錄頁

在這里插入圖片描述

2.主頁面

在這里插入圖片描述

3.病人信息錄入

在這里插入圖片描述

4.病人信息操作

在這里插入圖片描述

5.就醫(yī)檔案錄入

在這里插入圖片描述

6.處方單錄入

在這里插入圖片描述

7.就醫(yī)檔案操作

在這里插入圖片描述

8.醫(yī)生信息錄入

在這里插入圖片描述

9.醫(yī)生信息操作

在這里插入圖片描述

10.科室信息錄入

在這里插入圖片描述

11.科室信息操作

在這里插入圖片描述

12.收費操作

在這里插入圖片描述

13.收費統(tǒng)計

在這里插入圖片描述

14.修改密碼

在這里插入圖片描述

15.醫(yī)生主頁面

在這里插入圖片描述

16.收費員主頁面

在這里插入圖片描述

三、系統(tǒng)實現(xiàn)

Login.java

package com.sjsq;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Login extends JFrame {

	public static String namew;
	// 輸入的用戶Id
	public static String userId;
	// 輸入的用戶名
	public static String username;
	// 輸入的密碼
	public static String password;
	// 驗證標識
	int loginFlag = 0;
	private static final long serialVersionUID = 1L;
	DBUtil dbUtil = new DBUtil();
	Connection con = dbUtil.getConnection();
	
	// 賬號
	JLabel accountJLabel = new JLabel("賬號:");
	// 錯誤提示標簽
	JLabel errorJLabel = new JLabel("用戶名或者密碼不對,請重新輸入");
	// 密碼
	JLabel passwordJLabel = new JLabel("密碼:");

	// r1:管理員 r2:收費員 r3:醫(yī)生
	public JRadioButton r1, r2, r3;
	ImageIcon bg = new ImageIcon("picture/login_bg.jpg");
	JLabel bgJLabel = new JLabel(bg);

	JButton loginJButton = new JButton("登錄");
	JButton cancelJButton = new JButton("取消");

	private boolean flag;
	static JTextField usernameJTextField = new JTextField();
	static JPasswordField passwordJPasswordField = new JPasswordField();

	Login(String sTitle) {
		super(sTitle);
		this.setLayout(null);
		this.add(errorJLabel); // 添加控件

		this.add(accountJLabel);
		this.add(passwordJLabel);
		this.add(loginJButton);
		this.add(cancelJButton);

		this.add(usernameJTextField);
		this.add(passwordJPasswordField);

		final JRadioButton r1 = new JRadioButton("管理員");
		final JRadioButton r2 = new JRadioButton("收費員");
		final JRadioButton r3 = new JRadioButton("醫(yī)生");
		
		ButtonGroup rg = new ButtonGroup();
		this.add(r2);
		rg.add(r1);
		this.add(r3);
		rg.add(r3);
		this.add(r1);
		rg.add(r2);
		r1.setBounds(150, 180, 80, 30);
		r2.setBounds(230, 180, 80, 30);
		r3.setBounds(310, 180, 80, 30);
		r1.setFocusPainted(false);
		r2.setFocusPainted(false);
		r3.setFocusPainted(false);
		r3.setContentAreaFilled(false);
		r1.setContentAreaFilled(false);
		r2.setContentAreaFilled(false);

		errorJLabel.setBounds(100, 130, 200, 50);
		errorJLabel.setForeground(Color.black);
		errorJLabel.setVisible(false);

		bgJLabel.setBounds(0, 0, 592, 350);
		
		// 登錄監(jiān)聽
		loginJButton.addActionListener(new ActionListener() {
			public boolean flag = false;

			public void actionPerformed(ActionEvent e) {

				// 醫(yī)生
				if (r3.isSelected()) {
					try {
						String usernameText = usernameJTextField.getText().toString(); // 獲取帳號文本框內(nèi)容
						String passwordText = passwordJPasswordField.getText().toString(); // 獲取密碼文本框內(nèi)容
						Statement stmt = con.createStatement();
						ResultSet rs = stmt.executeQuery("select * from doctor"); // 執(zhí)行SQL語句,返回結(jié)果集
						while (rs.next()) {

							userId = rs.getString("DrId"); // 獲取登錄的用戶編號,
							username = rs.getString("DrName");// 獲取登錄的用戶姓名
							password = rs.getString("Password"); // 獲取數(shù)據(jù)庫中的數(shù)據(jù)項的密碼
							if (userId.equals(usernameText) && password.equals(passwordText)) {// 判斷數(shù)據(jù)庫的用戶編號以及密碼是否與文本框的值相同
								loginFlag = 1;
								break;
							}

						}
						if (loginFlag == 1) {
							JOptionPane.showMessageDialog(null, "登錄成功");
							// 顯示系統(tǒng)主界面
							MainPanelDoctor a = new MainPanelDoctor("醫(yī)生界面"); 
							a.setVisible(true);
							Login.this.setVisible(false);// 關(guān)閉登錄按鈕
						} else {
							usernameJTextField.setText(""); // 錯誤的話則文本框內(nèi)容設(shè)置為空,顯示錯誤標簽
							passwordJPasswordField.setText("");

							JOptionPane.showMessageDialog(null, "登陸錯誤");
						}
					} catch (SQLException e2) {
						System.out.println(e2);
					}
				}
				
				// 管理員
				else if (r1.isSelected()) {

					try {
						
						String usernameText = usernameJTextField.getText().toString(); // 獲取帳號文本框內(nèi)容
						String passwordText = passwordJPasswordField.getText().toString(); // 獲取密碼文本框內(nèi)容

						Statement stmt = con.createStatement();
						ResultSet rs = stmt.executeQuery("select * from manager"); // 執(zhí)行SQL語句,返回結(jié)果集
						while (rs.next()) {

							userId = rs.getString("ManagerID"); // 獲取登錄的用戶編號,
							username = rs.getString("ManagerName");// 獲取登錄的用戶姓名
							password = rs.getString("MaPassWord"); // 獲取數(shù)據(jù)庫中的數(shù)據(jù)項的密碼
							if (userId.equals(usernameText) && password.equals(passwordText)) {// 判斷數(shù)據(jù)庫的用戶編號以及密碼是否與文本框的值相同

								loginFlag = 1;

								break;
							}

						}
						// 登錄成功
						if (loginFlag == 1) {
							JOptionPane.showMessageDialog(null, "登錄成功");
							new MainPanelManager("管理員界面"); // 顯示系統(tǒng)主界面
							Login.this.setVisible(false);// 關(guān)閉登錄按鈕
						// 登錄失敗
						} else {
							usernameJTextField.setText(""); // 錯誤的話則文本框內(nèi)容設(shè)置為空,顯示錯誤標簽
							passwordJPasswordField.setText("");
							JOptionPane.showMessageDialog(null, "登陸錯誤");
						}
					} catch (SQLException e3) {
						System.out.println(e3);
					}

				}
				// 收費員
				else if (r2.isSelected()) {

					try {
						String usernameText = usernameJTextField.getText().toString(); // 獲取帳號文本框內(nèi)容
						String passwordText = passwordJPasswordField.getText().toString(); // 獲取密碼文本框內(nèi)容
						
						Statement stmt = con.createStatement();
						ResultSet rs = stmt.executeQuery("select * from cashier"); // 執(zhí)行SQL語句,返回結(jié)果集
						while (rs.next()) {

							userId = rs.getString("cashierId"); // 獲取登錄的用戶編號,
							username = rs.getString("cashierName");// 獲取登錄的用戶姓名
							password = rs.getString("cashierPassWord"); // 獲取數(shù)據(jù)庫中的數(shù)據(jù)項的密碼
							if (userId.equals(usernameText) && password.equals(passwordText)) {// 判斷數(shù)據(jù)庫的用戶編號以及密碼是否與文本框的值相同

								loginFlag = 1;

								break;
							}

						}
						if (loginFlag == 1) {
							JOptionPane.showMessageDialog(null, "登錄成功");
							new MainPanelCashier("收費員頁面"); // 顯示系統(tǒng)主界面
							Login.this.setVisible(false);// 關(guān)閉登錄按鈕
						} else {
							usernameJTextField.setText(""); // 錯誤的話則文本框內(nèi)容設(shè)置為空,顯示錯誤標簽
							passwordJPasswordField.setText("");

							JOptionPane.showMessageDialog(null, "登陸錯誤");
						}
					} catch (SQLException e3) {
						System.out.println(e3);
					}

				} else if (r1.isSelected() == false && r2.isSelected() == false && r3.isSelected() == false) {
					JOptionPane.showMessageDialog(null, "請選擇用戶類型");

				}

			}
		});

		// 登錄按鈕添加功能事件

		// 賬號
		accountJLabel.setBounds(150, 50, 100, 50);
		accountJLabel.setFont(new Font("", 1, 20));

		// 密碼
		passwordJLabel.setBounds(150, 120, 100, 50);
		passwordJLabel.setFont(new Font("", 1, 20));

		// 登錄
		loginJButton.setBounds(150, 220, 100, 40);
		loginJButton.setBackground(Color.CYAN);
		
		// 取消
		cancelJButton.setBounds(280, 220, 100, 40);
		cancelJButton.setBackground(Color.CYAN);
		
		// 賬號輸入框
		usernameJTextField.setBounds(250, 60, 150, 30);
		// 密碼輸入框
		passwordJPasswordField.setBounds(250, 120, 150, 30);

		this.add(bgJLabel);
		this.setVisible(true);
		this.setSize(600, 350); // 設(shè)置窗口大小
		this.setResizable(false); // 設(shè)置不可調(diào)整窗口大小
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public static void main(String args[]) {
		Login login = new Login("醫(yī)院管理系統(tǒng)");
	}

}

HomePage.java

HomePage.java
package com.sjsq;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class HomePage extends JFrame {
	JPanel homePage = new JPanel();
	private JLabel la1, la2;
	private Font laFont = new Font("隸書", Font.BOLD, 100);

	public HomePage() {
		homePage.setLayout(null);
		// 獲取項目路徑
		ImageIcon background = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(background);

		la1 = new JLabel("歡迎使用");
		la2 = new JLabel("醫(yī)院信息管理系統(tǒng)");

		la1.setBounds(330, 0, 800, 300);
		la1.setFont(laFont);
		la2.setBounds(120, 150, 1000, 300);
		la2.setFont(laFont);

		homePage.add(la1);
		homePage.add(la2);

		homePage.add(label);
		label.setBounds(0, 0, 1100, 700);
	}
}

Charge.java

package com.sjsq;

import java.awt.Color;
import java.awt.Font;
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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

public class Charge extends JFrame implements ActionListener {
	private JComboBox box1;
	private JScrollPane JScrollPane5 = new JScrollPane();
	JPanel panel2 = new JPanel();
	Font f2 = new Font("隸書", Font.BOLD, 30);
	private JLabel la0, la1, la2, la3, la4, la5, la6, la7, la8, la9, la10, la11, la12, la13, l14, l15, la14, la15;
	private JTextField tx13, tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8, tx9, tx10, tx11, tx12, tx14;
	public static JTable table3;
	public static DefaultTableModel dtm3;
	private JButton btn1, btn2;
	private double suma = 0;
	private double sumd = 0;
	private double sumb = 0;
	private double sume = 0;
	private double sumc = 0;
	private double sumf = 0;
	String b;
	private String columnNames[] = { "就醫(yī)檔案編號", "病人編號", "病人姓名", "就醫(yī)科室" };

	Charge() {
		// 獲取時間
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
		String dateString = sdf.format(now);
		ImageIcon background = new ImageIcon("picture/right_bg.jpg"); // 背景圖片
		JLabel label = new JLabel(background);

		panel2.setLayout(null);

		// 控件的定義
		btn1 = new JButton("確定結(jié)算");
		btn2 = new JButton("撤銷結(jié)算");
		la0 = new JLabel("費用總計 :");
		la1 = new JLabel("就醫(yī)檔案編號 :");
		la2 = new JLabel("姓名: ");
		la3 = new JLabel("編碼: ");
		la4 = new JLabel("姓名: ");
		la5 = new JLabel("科室: ");
		la6 = new JLabel("用藥費用: ");
		la7 = new JLabel("治療費用: ");
		la8 = new JLabel("檢查費用: ");
		la9 = new JLabel("掛號費: ");
		la10 = new JLabel("處置費: ");
		la11 = new JLabel("化驗費: ");
		la12 = new JLabel("押金累計: ");
		la13 = new JLabel("押金余額: ");
		la14 = new JLabel("結(jié)賬日期: ");
		la15 = new JLabel("收費操作 ");
		la13.setForeground(Color.red);
		la12.setForeground(Color.red);
		tx0 = new JTextField();
		tx1 = new JTextField();
		tx2 = new JTextField();
		tx3 = new JTextField();
		tx4 = new JTextField();
		tx5 = new JTextField();
		tx6 = new JTextField();
		tx7 = new JTextField();
		tx8 = new JTextField();
		tx9 = new JTextField();
		tx10 = new JTextField();
		tx11 = new JTextField();
		tx12 = new JTextField();
		tx13 = new JTextField();
		tx14 = new JTextField(dateString);
		la15.setFont(f2);

		// 設(shè)置文本框的邊緣不顯示
		tx0.setBorder(null);
		tx2.setBorder(null);
		tx3.setBorder(null);
		tx4.setBorder(null);
		tx14.setBorder(null);
		tx14.setEditable(false);

		// 設(shè)置一個新的面板
		final JPanel panel1 = new JPanel();
		JPanel panel12 = new JPanel();
		JPanel panel13 = new JPanel();
		panel13.setBackground(Color.pink);
		panel12.setBackground(Color.pink);
		panel1.setLayout(null);// 設(shè)置空布局
		panel1.setBorder(new TitledBorder(null, "收費結(jié)算", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		panel1.setBounds(10, 100, 800, 500);
		panel1.setBackground(Color.WHITE);
		panel1.add(panel12);
		panel12.setBounds(600, 0, 10, 500);
		panel1.add(panel13);
		panel13.setBounds(0, 330, 600, 10);

		btn1.addActionListener(this); // 設(shè)置按鈕事件
		btn2.addActionListener(this);

		// 默認表格模版的設(shè)置,添加表頭和設(shè)置表格不可編輯
		dtm3 = new DefaultTableModel(columnNames, 0);
		table3 = new JTable(dtm3) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};
		String sql = "select MrId,PaId,PaName,DeptName from Medical_records";
		databaseSearch1(sql, 4);

		JScrollPane5.setViewportView(table3);// 給表格添加滾動條
		panel1.add(JScrollPane5);
		JScrollPane5.setBounds(10, 0, 400, 100);
		JScrollPane5.setVisible(false);

		// 面板添加控件,設(shè)置位置
		panel2.add(tx0);
		tx0.setBounds(290, 60, 100, 30);
		panel1.add(btn1);
		btn1.setBounds(650, 100, 100, 50);
		panel1.add(btn2);
		btn2.setBounds(650, 200, 100, 50);
		panel1.add(tx2);
		tx2.setBounds(150, 70, 70, 30);
		panel1.add(tx3);
		tx3.setBounds(150, 120, 70, 30);
		panel1.add(tx4);
		tx4.setBounds(150, 170, 70, 30);
		panel1.add(tx5);
		tx5.setBounds(180, 220, 70, 30);
		panel1.add(tx6);
		tx6.setBounds(180, 270, 70, 30);
		panel1.add(tx7);
		tx7.setBounds(475, 70, 70, 30);
		panel1.add(tx8);
		tx8.setBounds(460, 120, 70, 30);
		panel1.add(tx9);
		tx9.setBounds(460, 170, 70, 30);
		panel1.add(tx10);
		tx10.setBounds(460, 220, 70, 30);
		panel1.add(tx11);
		tx11.setBounds(270, 350, 70, 30);
		panel1.add(tx12);
		tx12.setBounds(470, 350, 70, 30);
		panel1.add(tx13);
		tx13.setBounds(80, 350, 70, 30);

		panel2.add(la1);
		la1.setBounds(20, 50, 100, 50);
		panel2.add(la2);
		la2.setBounds(250, 50, 100, 50);
		panel2.add(tx1);
		tx1.setBounds(110, 58, 120, 30);
		panel1.add(la3);
		la3.setBounds(100, 70, 100, 30);
		panel1.add(la4);
		la4.setBounds(100, 120, 100, 30);
		panel1.add(la5);
		la5.setBounds(100, 170, 100, 30);
		panel1.add(la6);
		la6.setBounds(100, 220, 100, 30);
		panel1.add(la7);
		la7.setBounds(100, 270, 100, 30);

		panel1.add(la8);
		la8.setBounds(400, 70, 100, 30);
		panel1.add(la9);
		la9.setBounds(400, 120, 100, 30);
		panel1.add(la10);
		la10.setBounds(400, 170, 100, 30);
		panel1.add(la11);
		la11.setBounds(400, 220, 100, 30);
		panel1.add(la0);
		la0.setBounds(10, 350, 100, 30);

		la15.setBounds(30, 0, 200, 50);
		panel2.add(la15);
		la12.setBounds(200, 350, 100, 30);
		panel1.add(la12);
		la13.setBounds(400, 350, 100, 30);
		panel1.add(la13);
		panel2.add(panel1);

		panel2.add(la14);
		la14.setBounds(400, 60, 100, 30);
		panel2.add(tx14);
		tx14.setBounds(480, 60, 100, 30);

		// 設(shè)置文本框不可編輯
		tx0.setEditable(false);
		tx2.setEditable(false);
		tx3.setEditable(false);
		tx4.setEditable(false);
		tx11.setEditable(false);
		tx12.setEditable(false);
		tx13.setEditable(false);

		panel2.add(label);// 面板添加背景圖片,設(shè)置位置
		label.setBounds(-30, 0, 1100, 700);

		tx1.addMouseListener(new MouseAdapter() {// 給tx1的文本框添加按鈕事件,顯示一個表格
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					JScrollPane5.setVisible(true);
			}
		});
		// tx1文本框添加事件,根據(jù)文本框內(nèi)容的改變模糊查詢到數(shù)據(jù)庫內(nèi)容,顯示到表格中
		tx1.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {

				updata_combobox();
			}

			private void updata_combobox() {
				String s1 = null;
				s1 = tx1.getText(); // 根據(jù)S1的內(nèi)容模糊查詢數(shù)據(jù)庫對應(yīng)的數(shù)據(jù)
				JScrollPane5.setVisible(true);
				String sql = "select MrId,PaId,PaName,DeptName from Medical_records where MrId like  '%" + s1 + "%'";
				databaseSearch1(sql, 5);
			}
		});
		// 根據(jù)你選擇表格的某一行內(nèi)容,輸入到對應(yīng)的文本框內(nèi)。
		table3.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 1) {
						String sum1 = null;
						String sum3 = null;
						String sum6 = null;
						String sum2 = null;
						String sum4 = null;
						String sum5 = null;
						int row = table3.getSelectedRow();
						String ao = (String) table3.getValueAt(row, 2);
						String bo = (String) table3.getValueAt(row, 0);
						String do1 = (String) table3.getValueAt(row, 1);
						String co = (String) table3.getValueAt(row, 3);
						tx2.setText(do1);
						tx3.setText(ao);
						tx1.setText(bo);
						tx0.setText(ao);
						tx4.setText(co);
						JScrollPane5.setVisible(false);

						// 連接數(shù)據(jù)庫,查詢對應(yīng)的價格
						DBUtil dbUtil = new DBUtil();
						Connection con = dbUtil.getConnection();
						ResultSet rs;
						try {
							// 查詢數(shù)據(jù)庫中用藥的費用
							String sql1 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeClass='診斷類'or PeClass='藥品類'and MrId='" + tx1.getText() + "'";
							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);
							while (rs.next()) {
								String a = rs.getString(1);
								String b = rs.getString(2);

								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								suma = suma + d;
								sum1 = String.valueOf(suma);

							}
							suma = 0;// 設(shè)置為0,否則會應(yīng)為再次輸入而無法清楚原來的數(shù)值
							if (sum1 != null) {
								tx5.setText(sum1);
							} else {
								tx5.setText("0.0");
							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}
						try {

							String sql2 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeClass='其他類'and PeName not in('檢查費','掛號費','處置費','化驗費')";

							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql2);

							while (rs.next()) {
								String a = rs.getString(1);
								String b = rs.getString(2);
								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								sumb = sumb + d;
								sum3 = String.valueOf(sumb);
								System.out.println(a);
								System.out.println(b);
							}
							sumb = 0;
							if (sum3 != null) {
								tx6.setText(sum3);
							} else {
								tx6.setText("0.0");

							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}

						try {

							String sql1 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeName='檢查費'";

							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);

							while (rs.next()) {
								String a = rs.getString(1);
								String b = rs.getString(2);
								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								sumc = sumc + d;
								sum2 = String.valueOf(sumc);

							}
							sumc = 0;
							if (sum2 != null) {
								tx7.setText(sum2);
							} else {
								tx7.setText("0.0");
							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}

						try {

							String sql1 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeName='掛號費'";

							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);

							while (rs.next()) {

								String a = rs.getString(1);
								String b = rs.getString(2);
								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								sumd = sumd + d;
								sum4 = String.valueOf(sumd);

							}
							sumd = 0;
							if (sum4 != null) {
								tx8.setText(sum4);
							} else {
								tx8.setText("0.0");
							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}
						try {

							String sql1 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeName='處置費'";

							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);

							while (rs.next()) {

								String a = rs.getString(1);
								String b = rs.getString(2);
								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								sume = sume + d;
								sum5 = String.valueOf(sume);

							}
							sume = 0;
							if (sum5 != null) {
								tx9.setText(sum5);
							} else {
								tx9.setText("0.0");
							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}
						try {
							String sql1 = "select PePrice,PeNumber from DrugTable where MrId='" + tx1.getText()
									+ "' and PeName='化驗費'";

							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);

							while (rs.next()) {
								String a = rs.getString(1);
								String b = rs.getString(2);
								double a1 = Double.valueOf(a).doubleValue();
								int b1 = Integer.valueOf(b).intValue();

								double d = a1 * b1;

								sumf = sumf + d;
								sum6 = String.valueOf(sumf);

							}
							sumc = 0;
							if (sum6 != null) {
								tx10.setText(sum6);
							} else {
								tx10.setText("0.0");
							}

						} catch (Exception ex) {
							ex.printStackTrace();
						}
						try {
							String sql1 = "select 	PaPay from Patient  where PaId='" + tx2.getText() + "'";
							Statement stmt = con.createStatement();
							rs = stmt.executeQuery(sql1);
							while (rs.next()) {
								String a = rs.getString(1);
								if (a != null) {
									tx11.setText(a);
								} else {
									tx11.setText("0.0");
								}
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}
						String t;
						String y;
						String u;
						String u1;
						String u2;
						String u3;
						String u4;
						t = tx5.getText();
						u1 = tx6.getText();
						u2 = tx8.getText();
						u3 = tx9.getText();
						u4 = tx10.getText();
						u = tx7.getText();
						y = tx11.getText();
						float c = Float.parseFloat(u);
						float c1 = Float.parseFloat(t);
						float c2 = Float.parseFloat(y);
						float c4 = Float.parseFloat(u1);
						float c5 = Float.parseFloat(u2);
						float c6 = Float.parseFloat(u3);
						float c7 = Float.parseFloat(u4);
						float q = (float) (c2 - (c7 + c6 + c5 + c4 + c1 + c));// 減除押金后需要交的錢
						float q1 = (float) ((c7 + c6 + c5 + c4 + c1 + c));// 費用總計
						String s = String.valueOf(q);
						String s1 = String.valueOf(q1);
						tx12.setText(s);
						tx13.setText(s1);
						tx12.setForeground(Color.BLUE);
						tx11.setForeground(Color.BLUE);
					}
			}
		});

	}

	private void databaseSearch1(String sql, int i) {
		// TODO Auto-generated method stub
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm3.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm3.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm3.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			String[] data = new String[4];
			while (rs.next()) {
				for (int j = 1; j <= 4; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm3.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == btn1) {
			// 把費用表錄入到收費表中
			try {
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();

				Statement stmt = con.createStatement();

				String sql = "INSERT INTO Charge(MrId,PaId,PaName,DeptName,Drugfee,treatmentfee,checkfee,registrationfee,disposalfee,assayfee,sum,time)VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
				PreparedStatement parepare = con.prepareStatement(sql);
				parepare.setString(1, tx1.getText());
				parepare.setString(2, tx2.getText());
				parepare.setString(3, tx3.getText());
				parepare.setString(4, tx4.getText());
				parepare.setString(5, tx5.getText());
				parepare.setString(6, tx6.getText());
				parepare.setString(7, tx7.getText());
				parepare.setString(8, tx8.getText());
				parepare.setString(9, tx9.getText());
				parepare.setString(10, tx10.getText());
				parepare.setString(11, tx13.getText());
				parepare.setString(12, tx14.getText());
				// 判斷是否有輸入錯誤的,做提示操作
				if (tx1.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "請輸入結(jié)算的檔案號", "錯誤", JOptionPane.INFORMATION_MESSAGE);
				} else {
					parepare.executeUpdate();
					JOptionPane.showMessageDialog(null, "結(jié)賬成功,需要交(退)" + tx12.getText() + "", "結(jié)賬成功",
							JOptionPane.INFORMATION_MESSAGE);

					String sql1 = "delete from Patient where PaId='" + tx2.getText() + "'";
					try {

						stmt.executeUpdate(sql1);
						tx0.setText("");

						tx1.setText("");
						tx2.setText("");
						tx3.setText("");
						tx4.setText("");
						tx5.setText("");
						tx6.setText("");
						tx7.setText("");
						tx8.setText("");
						tx9.setText("");
						tx10.setText("");
						tx11.setText("");
						tx12.setText("");
						tx13.setText("");
						tx14.setText("");
						JScrollPane5.setVisible(false);
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}

			} catch (Exception e2) {
				e2.printStackTrace();
			}

		} else if (e.getSource() == btn2) {
			tx0.setText("");

			tx1.setText("");
			tx2.setText("");
			tx3.setText("");
			tx4.setText("");
			tx5.setText("");
			tx6.setText("");
			tx7.setText("");
			tx8.setText("");
			tx9.setText("");
			tx10.setText("");
			tx11.setText("");
			tx12.setText("");
			tx13.setText("");
			tx14.setText("");
			JScrollPane5.setVisible(false);

		}
	}
}

ChargeQuery.java

package com.sjsq;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class ChargeQuery {
	Font f1 = new Font("隸書", Font.BOLD, 30);
	public static JTable table;
	
	public static DefaultTableModel dtm;
	private JScrollPane JScrollPane = new JScrollPane();
	JPanel panel2 = new JPanel();

	private JLabel la1, la2, la3, la4, la5;
	private JTextField tx1, tx2;
	private String columnNames[] = { "就醫(yī)檔案編號", "病人編號", "病人姓名", "就醫(yī)科室", "用藥費用", "治療費", "檢查費", "掛號費", "處置費", "化驗費",
			"費用總額", "結(jié)賬時間" };

	ChargeQuery() {
		// 添加背景
		ImageIcon background = new ImageIcon("picture/right_bg.jpg"); // 背景圖片
		JLabel label = new JLabel(background);

		panel2.setLayout(null);
		// 設(shè)置默認表格面板
		dtm = new DefaultTableModel(columnNames, 0);
		table = new JTable(dtm) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};

		String sql = "select * from Charge";
		databaseSearch(sql, 12);

		JScrollPane.setViewportView(table);// 給表格添加滾動條
		panel2.add(JScrollPane);
		JScrollPane.setBounds(30, 200, 950, 300);

		setbgcolor();

		JLabel label1 = new JLabel("收費統(tǒng)計");
		panel2.add(label1);
		label1.setBounds(30, 10, 400, 50);
		label1.setFont(f1);

		la1 = new JLabel("總費用統(tǒng)計:");
		la2 = new JLabel("人數(shù)統(tǒng)計:");
		tx1 = new JTextField();
		tx2 = new JTextField();

		la1.setBounds(30, 80, 100, 50);
		la2.setBounds(30, 120, 100, 50);
		tx1.setBounds(100, 90, 100, 30);
		tx2.setBounds(100, 130, 100, 30);

		panel2.add(la1);
		panel2.add(la2);
		panel2.add(tx1);
		panel2.add(tx2);
		tx1.setEditable(false);
		tx2.setEditable(false);
		panel2.add(label);// 面板添加背景圖片,設(shè)置位置
		label.setBounds(-30, 0, 1100, 700);
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs, rs1;

		try {
			String sql2 = "select Sum(sum) from Charge ";
			String sql1 = "select count(*) from Charge ";
			Statement stmt = con.createStatement();
			Statement stmt1 = con.createStatement();
			rs = stmt.executeQuery(sql1);
			rs1 = stmt1.executeQuery(sql2);
			while (rs.next()) {
				String a = rs.getString(1);
				tx2.setText(a);
			}

			while (rs1.next()) {
				String a = rs1.getString(1);
				tx1.setText(a);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setbgcolor() {
		// TODO Auto-generated method stub
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
						boolean hasFocus, int row, int column) {
					if (row % 2 == 0)
						setBackground(new Color(223, 220, 239)); // 設(shè)置奇數(shù)行底色
					else if (row % 2 == 1)
						setBackground(Color.white); // 設(shè)置偶數(shù)行底色
					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < table.getColumnCount(); i++) {
				table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void databaseSearch(String sql, int i) {
		// TODO Auto-generated method stub
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			String[] data = new String[12];
			while (rs.next()) {
				for (int j = 1; j <= 12; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm.addRow(data); // 在Jtable中添加數(shù)據(jù)行
			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {
		}
	}
}

ChufangModify.java

package com.sjsq;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class ChufangModify extends JFrame implements ActionListener, ItemListener {
	JButton button6 = new JButton("增加");
	JButton button7 = new JButton("確定");
	JButton button8 = new JButton("刪除");
	private String columnNames[] = { "編碼", "名稱", "單價", "數(shù)量", "計數(shù)單位", "類別", "檔案編號" };
	private String columnNames1[] = { "編碼", "名稱", "單價", "計數(shù)單位", "類別" };
	private JLabel la0;
	private JComboBox box1, box2;
	JPanel panel2 = new JPanel();
	public static JTable table2, table3;
	public static DefaultTableModel dtm2, dtm3;
	private JScrollPane JScrollPane3 = new JScrollPane();
	private JScrollPane JScrollPane5 = new JScrollPane();
	String y;

	ChufangModify(String Stitle) {
		super(Stitle);
		panel2.setLayout(null);
		ImageIcon ic; // 按鈕圖片
		ic = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(ic);// 把背景圖片顯示在一個標簽里面

		dtm2 = new DefaultTableModel(columnNames, 0) {// dtm2是項目收費表格模版
			public boolean isCellEditable(int row, int column) {
				if (column == 1 || column == 3)
					return true;// 這個是可以編輯的列
				// if(rowIndex!=0) return false;
				return false;
			}// 表格不允許被編輯 }
		};

		String fontSize1[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "12" };
		table2 = new JTable(dtm2);// JScrollPane4 項目表
		JScrollPane JScrollPane4 = new JScrollPane(table2);
		TableColumn a1 = table2.getColumn("名稱");
		TableColumn a2 = table2.getColumn("數(shù)量");
		JTextField box3 = new JTextField();
		box2 = new JComboBox(fontSize1);
		box2.addActionListener(this);
		box2.addItemListener(this);

		box3.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				System.out.println("removeUpdate");
				updata_combobox();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				System.out.println("insertUpdate");
				updata_combobox();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {

				updata_combobox();
			}

			private void updata_combobox() {
				String s1 = null;
				s1 = box3.getText();

				String sql = "select * from Price where PeName like  '%" + s1 + "%'and PeClass='其他類'";
				databaseSearch1(sql, 5);
			}
		});

		box3.setEditable(true);
		DefaultCellEditor dce2 = new DefaultCellEditor(box3);
		a1.setCellEditor(dce2);

		box2.setEditable(true);
		box2.setMaximumRowCount(5);
		DefaultCellEditor dce3 = new DefaultCellEditor(box2);
		a2.setCellEditor(dce3);
		box2.addActionListener(this);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "診療項目單", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		panel.setBounds(20, 150, 530, 180);
		panel.setBackground(Color.WHITE);
		panel.add(JScrollPane4);
		JScrollPane4.setBounds(10, 20, 400, 150);
		panel2.add(panel);
		button6.setBounds(420, 20, 100, 40);
		panel.add(button6);
		button7.setBounds(420, 70, 100, 40);
		panel.add(button7);
		button8.setBounds(420, 120, 100, 40);
		panel.add(button8);
		button6.addActionListener(this);
		button7.addActionListener(this);
		button8.addActionListener(this);
		dtm3 = new DefaultTableModel(columnNames1, 0);// 項目明細表
		table3 = new JTable(dtm3) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};

		JScrollPane5.setViewportView(table3);
		panel2.add(JScrollPane5);
		JScrollPane5.setBounds(30, 50, 400, 100);
		JScrollPane5.setVisible(false);

		String SQL1 = "select * from Price where PeClass='其他類'";
		databaseSearch1(SQL1, 5);

		JScrollPane4.setViewportView(table2);
		box3.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					JScrollPane5.setVisible(true);
			}
		});

		button8.addMouseListener(new MouseAdapter() { // 刪除按鈕實現(xiàn)刪除記錄的功能
			public void mouseClicked(MouseEvent e) {
				int row = table2.getSelectedRow();// 這句選擇要刪除的行
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				Statement stmt;
				String val = (String) table2.getValueAt(row, 6);
				String val1 = (String) table2.getValueAt(row, 0);
				String sql = "delete from DrugTable where MrId='" + val + "'and PeNo='" + val1 + "'";
				try {
					stmt = con.createStatement();
					stmt.executeUpdate(sql);
					button6.setEnabled(true);
					JOptionPane.showMessageDialog(null, " 刪除成功!", "注意", JOptionPane.INFORMATION_MESSAGE);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (row != -1) { // 這句判斷是否有選中的行
					dtm2.removeRow(row);
				} // 這句刪除指定行

			}
		});

		table3.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 2) {
						int o = table3.getSelectedRow();
						int row = table2.getSelectedRow();
						String ao = (String) table3.getValueAt(o, 1);
						String bo = (String) table3.getValueAt(o, 0);
						String co = (String) table3.getValueAt(o, 2);
						String eo = (String) table3.getValueAt(o, 4);
						String qo = (String) table3.getValueAt(o, 3);
						System.out.println(ao);
						box3.setText(ao);
						table2.setValueAt(bo, row, 0);
						table2.setValueAt(co, row, 2);

						table2.setValueAt(eo, row, 5);
						table2.setValueAt(qo, row, 4);
						y = co;
						JScrollPane5.setVisible(false);
					}
			}
		});

		panel2.add(label);
		label.setBounds(0, 0, 600, 400);
		this.add(panel2);
		this.setSize(600, 400); // 設(shè)置窗口大小
		this.setResizable(false); // 設(shè)置不可調(diào)整窗口大小
		this.setLocationRelativeTo(null);
		this.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == button7) {
			try {
				String s = (String) box2.getSelectedItem();
				int i = Integer.valueOf(s).intValue();
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				int row = table2.getSelectedRow();
				String b = (String) table2.getValueAt(row, 3);

				String sql = "INSERT INTO DrugTable(PeNo,PeName,PePrice,PeNumber,PeUnit,PeClass,MrId)VALUES(?,?,?,?,?,?,?)";
				PreparedStatement parepare = con.prepareStatement(sql);
				parepare.setString(1, (String) table2.getValueAt(row, 0));
				parepare.setString(2, (String) table2.getValueAt(row, 1));
				parepare.setString(3, (String) table2.getValueAt(row, 2));
				parepare.setString(4, (String) table2.getValueAt(row, 3));
				parepare.setString(5, (String) table2.getValueAt(row, 4));
				parepare.setString(6, (String) table2.getValueAt(row, 5));
				parepare.setString(7, (String) table2.getValueAt(row, 6));

				if (i <= 0 || b == "") {
					JOptionPane.showMessageDialog(null, "數(shù)量不能小于0或為空", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				}

				else {
					parepare.executeUpdate();
					JOptionPane.showMessageDialog(null, "錄入成功", "錄入成功", JOptionPane.INFORMATION_MESSAGE);

					button6.setEnabled(true);
				}
			} catch (Exception et) {
				et.printStackTrace();
			}
		}
	}

	public void databaseSearch1(String SQL1, int i) {
		// TODO Auto-generated method stub
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm3.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm3.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm3.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(SQL1);
			String[] data = new String[5];
			while (rs.next()) {
				for (int j = 1; j <= 5; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm3.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {

		}

	}

	public void addrow(JTable table) {
		// TODO Auto-generated method stub
		int row = table.getSelectedRow();

		String b = (String) table.getValueAt(row, 0);

		button6.addActionListener(new ActionListener() {// 添加事件
			public void actionPerformed(ActionEvent e) {
				String[] da1 = { "", "" };
				String[] rowValues = da1;

				dtm2.addRow(rowValues); // 添加一行
				int row1 = table2.getRowCount() - 1;
				table2.setRowSelectionInterval(row1, row1);
				table2.setValueAt(b, row1, 6);

				button6.setEnabled(false);

			}
		});
	}

	public void databaseSearch2(String SQL, int i) {
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm2.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm2.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm2.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);
			String[] data = new String[7];
			while (rs.next()) {
				for (int j = 1; j <= 7; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm2.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {

		}

	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub

	}

}

DBUtil.java

package com.sjsq;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {
	
	
	// 連接
	private Connection con = null;
	public String url = "jdbc:mysql://localhost:3306/swing_hospital_management?serverTimezone=UTC";
	public String username = "root";
	public String password = "admin";

	// 獲取連接
	public Connection getConnection() {
		try {
			con = DriverManager.getConnection(url, username, password);			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("獲取連接失敗:" + e.getMessage());
		}
		return con;
	}

	// 關(guān)閉連接
	public void close() {
		try {
			if (con != null) {
				con.close();
			}
			con = null;
			System.out.println("數(shù)據(jù)庫連接關(guān)閉");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 測試
	public static void main(String[] args) {
		DBUtil dbUtil = new DBUtil();
		dbUtil.getConnection();
		
	}
}

DepartmentManage.java

package com.sjsq;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class DepartmentManage extends JFrame implements ActionListener {

	JPanel departmentManage = new JPanel();
	private JLabel manageTitle = new JLabel("科室信息綜合操作");
	private JLabel la1, la2, la3, la4, la5, la6;
	private JTextField tx1, tx2, tx3, tx4, tx5, tx6;
	public JButton save, query, modify, delete;
	private Font laFont = new Font("宋體", Font.BOLD, 15);
	private JComboBox jcbb1;
	private String str1[] = { "查詢?nèi)?, "按科室編號查詢", "按科室名稱查詢" };
	private final String[] columnNames = { "科室編號", "科室名稱", "科室主任", "科室電話" };
	private JScrollPane JScrollPane1 = new JScrollPane();
	private java.sql.Connection con = null;
	private static JTable table;
	private static DefaultTableModel dtm;
	private Pattern pattern = Pattern.compile("[0-9]*");

	public DepartmentManage() {
		// 背景設(shè)置
		departmentManage.setLayout(null);
		ImageIcon background = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(background);

		// 標題設(shè)置
		manageTitle.setFont(new Font("宋體", Font.BOLD, 50));
		manageTitle.setBounds(60, 10, 1000, 50);
		departmentManage.add(manageTitle);

		// 錄入操作面板設(shè)置
		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "錄入操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, Color.red));
		panel.setBounds(45, 65, 550, 150);
		panel.setOpaque(false);

		la1 = new JLabel("科室編號:");
		la2 = new JLabel("科室名稱:");
		la3 = new JLabel("科室主任:");
		la4 = new JLabel("科室電話:");
		tx1 = new JTextField();
		tx2 = new JTextField();
		tx3 = new JTextField();
		tx4 = new JTextField();
		save = new JButton("保存");

		la1.setBounds(20, 20, 100, 50);
		la1.setFont(laFont);
		la2.setBounds(240, 20, 100, 50);
		la2.setFont(laFont);
		la3.setBounds(20, 80, 100, 50);
		la3.setFont(laFont);
		la4.setBounds(240, 80, 100, 50);
		la4.setFont(laFont);
		tx1.setBounds(100, 30, 120, 30);
		tx1.setFont(laFont);
		tx2.setBounds(320, 30, 120, 30);
		tx2.setFont(laFont);
		tx3.setBounds(100, 90, 120, 30);
		tx3.setFont(laFont);
		tx4.setBounds(320, 90, 120, 30);
		tx4.setFont(laFont);
		save.setBounds(460, 100, 80, 40);

		panel.add(la1);
		panel.add(la2);
		panel.add(la3);
		panel.add(la4);
		panel.add(tx1);
		panel.add(tx2);
		panel.add(tx3);
		panel.add(tx4);
		panel.add(save);
		departmentManage.add(panel);

		// 查詢操縱面板設(shè)置
		final JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBorder(new TitledBorder(null, "查詢操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, Color.red));
		panel1.setBounds(620, 65, 380, 150);
		panel1.setOpaque(false);

		query = new JButton("查詢");
		la5 = new JLabel("科室編號:");
		la6 = new JLabel("科室名稱:");
		tx5 = new JTextField();
		tx6 = new JTextField();
		jcbb1 = new JComboBox(str1);
		jcbb1.setBounds(20, 28, 150, 25);
		jcbb1.setFont(laFont);
		la5.setBounds(20, 73, 80, 50);
		la5.setFont(laFont);
		la6.setBounds(20, 73, 80, 50);
		la6.setFont(laFont);
		tx5.setBounds(100, 80, 120, 30);
		tx5.setFont(laFont);
		tx6.setBounds(100, 80, 120, 30);
		tx6.setFont(laFont);
		query.setBounds(290, 100, 80, 40);

		la5.setVisible(false);
		la6.setVisible(false);
		tx5.setVisible(false);
		tx6.setVisible(false);

		panel1.add(la5);
		panel1.add(la6);
		panel1.add(tx5);
		panel1.add(tx6);
		panel1.add(jcbb1);
		panel1.add(query);
		departmentManage.add(panel1);

		// 表格設(shè)置
		defaultTableModel(); // 設(shè)置表格不可編輯
		setTableColumnCenter(); // 設(shè)置表格內(nèi)容居中顯示
		setbgcolor(); // 設(shè)置表格隔行不同顏色
		JScrollPane1.setBounds(new Rectangle(45, 230, 850, 270));
		JScrollPane1.setViewportView(table); // 創(chuàng)建一個滾動條(如果有必要)并設(shè)置其視圖
		// table.getColumnModel().getColumn(0).setMinWidth(40);
		// table.getColumnModel().getColumn(0).setMaxWidth(40);
		table.getTableHeader().setReorderingAllowed(false); // 列不可拖動
		table.getTableHeader().setResizingAllowed(false); // 列寬不能改變
		departmentManage.add(JScrollPane1);

		// 按鈕設(shè)置
		modify = new JButton("修改");
		delete = new JButton("刪除");
		// reflash=new JButton("刷新");
		modify.setBounds(910, 230, 80, 40);
		delete.setBounds(910, 300, 80, 40);
		// reflash.setBounds(910, 370, 80, 40);
		departmentManage.add(modify);
		departmentManage.add(delete);
		// departmentManage.add(reflash);

		// 添加監(jiān)聽器
		save.addActionListener(this);
		delete.addActionListener(this);
		query.addActionListener(this);
		modify.addActionListener(this);
		// reflash.addActionListener(this);
		jcbb1.addActionListener(this);

		// 添加背景
		departmentManage.add(label);
		label.setBounds(0, 0, 1100, 700);
	}

	// 設(shè)置表格不可編輯
	private void defaultTableModel() {
		dtm = new DefaultTableModel(columnNames, 0);
		table = new JTable(dtm) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
	}

	// 設(shè)置表格內(nèi)容居中顯示
	private void setTableColumnCenter() {
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, r);
	}

	// 設(shè)置表格隔行背景顏色不同
	private static void setbgcolor() {
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
						boolean hasFocus, int row, int column) {
					if (row % 2 == 0)
						setBackground(new Color(223, 220, 239)); // 設(shè)置奇數(shù)行底色
					else if (row % 2 == 1)
						setBackground(Color.white); // 設(shè)置偶數(shù)行底色
					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < table.getColumnCount(); i++) {
				table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jcbb1) {
			if (jcbb1.getSelectedIndex() == 0) {
				la5.setVisible(false);
				la6.setVisible(false);
				tx5.setVisible(false);
				tx6.setVisible(false);
				return;
			} else if (jcbb1.getSelectedIndex() == 1) {
				la5.setVisible(true);
				tx5.setVisible(true);
				la6.setVisible(false);
				tx6.setVisible(false);
				return;
			} else if (jcbb1.getSelectedIndex() == 2) {
				tx5.setVisible(false);
				la5.setVisible(false);
				tx6.setVisible(true);
				la6.setVisible(true);
				return;
			}
		}

		if (e.getSource() == save) { // 錄入操作
			// 輸入信息不能為空
			if (tx1.getText().equals("") || tx2.getText().equals("") || tx4.getText().equals("")) {
				JOptionPane.showMessageDialog(null, "請輸入完整的科室信息!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 編號為3位數(shù)字
			else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {
				JOptionPane.showMessageDialog(null, "請輸入正確的3位數(shù)科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 名字長度不能超過20個字符
			else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {
				JOptionPane.showMessageDialog(null, "名字長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 電話號碼為8位數(shù)字
			else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {
				JOptionPane.showMessageDialog(null, "請輸入正確的8位數(shù)電話號碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}

			else {
				String deptNo = tx1.getText().trim();
				String deptName = tx2.getText();
				String deptPhone = tx4.getText().trim();
				try {
					DBUtil dbUtil = new DBUtil();
					Connection con = dbUtil.getConnection();
					Statement st1 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					Statement st2 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					Statement st3 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					ResultSet rs1 = st1.executeQuery("select * from Department where DeptNo='" + deptNo + "'"); // SQL語句
					ResultSet rs2 = st2.executeQuery("select * from Department where DeptName='" + deptName + "'");
					ResultSet rs3 = st3.executeQuery("select * from Department where DeptPhone='" + deptPhone + "'");

					if (rs1.next()) { // 判斷結(jié)果集rs是否有記錄,并且將指針后移一位
						JOptionPane.showMessageDialog(null, "該科室號已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st1.close();
					} else if (rs2.next()) {
						JOptionPane.showMessageDialog(null, "該科室名已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st2.close();
					} else if (rs3.next()) {
						JOptionPane.showMessageDialog(null, "該科室電話號碼已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st3.close();
					}

					else {
						int ok = JOptionPane.showConfirmDialog(null, "是否保存該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
								JOptionPane.INFORMATION_MESSAGE);
						if (ok == JOptionPane.YES_OPTION) {
							try {
								// 信息添加到數(shù)據(jù)庫
								String sql = "INSERT INTO Department(DeptNo,DeptName,DrName,DeptPhone)VALUES(?,?,?,?)";
								PreparedStatement parepare = con.prepareStatement(sql);
								parepare.setString(1, tx1.getText());
								parepare.setString(2, tx2.getText());
								parepare.setString(3, tx3.getText());
								parepare.setString(4, tx4.getText());
								parepare.executeUpdate();

								String[] data = new String[] { tx1.getText(), tx2.getText(), tx3.getText(),
										tx4.getText() };
								dtm.addRow(data); // 在表格添加一行剛添加的數(shù)據(jù)
								JOptionPane.showMessageDialog(null, "錄入成功");
								tx1.setText("");
								tx2.setText("");
								tx3.setText("");
								tx4.setText("");

							} catch (Exception e1) {
								e1.printStackTrace();
								System.out.println("SQL Exception occur.Message is:");
								System.out.println(e1.getMessage());
							}
						}
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}

		if (e.getSource() == query) { // 查詢操作
			try {
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				if (jcbb1.getSelectedIndex() == 0) { // 全部查詢
					String sql = "select * from Department";
					databaseSearch(sql);
					if (table.getRowCount() != 0) {
						JOptionPane.showMessageDialog(null, "查詢成功!");
					} else {
						JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
					}
				}
				if (jcbb1.getSelectedIndex() == 1) { // 編號查詢
					if (tx5.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(null, "請輸入正確的科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
					} else {
						String deptNo = tx5.getText().trim();
						String sql = "select * from Department where DeptNo like'%" + deptNo + "%'";
						databaseSearch(sql);
						if (table.getRowCount() != 0) {
							JOptionPane.showMessageDialog(null, "查詢成功!");
							tx5.setText("");
						} else {
							JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
						}
					}
				}
				if (jcbb1.getSelectedIndex() == 2) { // 名稱查詢
					if (tx6.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(null, "請輸入正確的科室名稱!", "錯誤", JOptionPane.ERROR_MESSAGE);
					} else {
						String deptName = tx6.getText();
						String sql = "select * from Department where DeptName like '%" + deptName + "%'";
						databaseSearch(sql);
						if (table.getRowCount() != 0) {
							JOptionPane.showMessageDialog(null, "查詢成功!");
							tx6.setText("");
						} else {
							JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
						}
					}
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if (e.getSource() == delete) { // 刪除操作
			try {
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
				int selectCount = table.getSelectedRowCount();
				if (selectCount == 0) {
					JOptionPane.showMessageDialog(null, "請選擇您要刪除的信息!");
				} else if (selectCount == 1) {
					int ok = JOptionPane.showConfirmDialog(null, "是否刪除該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
							JOptionPane.INFORMATION_MESSAGE);
					if (ok == JOptionPane.YES_OPTION) {
						int row = table.getSelectedRow();
						String deptNo = (String) table.getValueAt(row, 0);
						String sql = "delete from Department where DeptNo='" + deptNo + "'";
						stmt.executeUpdate(sql);
						dtm.removeRow(row);
						JOptionPane.showMessageDialog(null, "刪除成功!");
					}
				} else {
					int ok = JOptionPane.showConfirmDialog(null, "是否刪除所選" + selectCount + "個科室信息?", "確定",
							JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
					if (ok == JOptionPane.YES_OPTION) {
						for (int i = 1; i <= selectCount; i++) {
							int row1 = table.getSelectedRow();
							String deptNo = (String) table.getValueAt(row1, 0);
							String sql = "delete from Department where DeptNo='" + deptNo + "'";
							stmt.executeUpdate(sql);
							dtm.removeRow(row1);
						}
						JOptionPane.showMessageDialog(null, "刪除成功!");
					}
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		// 修改操作設(shè)置
		if (e.getSource() == modify) {
			if (table.getSelectedRowCount() != 1) {
				JOptionPane.showMessageDialog(null, "請選擇一項科室信息進行修改!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else {
				try {
					DBUtil dbUtil = new DBUtil();
					Connection con = dbUtil.getConnection();
					Statement stmt1 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象

					int row = table.getSelectedRow();

					String value0 = (String) table.getValueAt(row, 0);
					String value1 = (String) table.getValueAt(row, 1);
					String value2 = (String) table.getValueAt(row, 2);
					String value3 = (String) table.getValueAt(row, 3);

					DepartmentModify dig = new DepartmentModify();
					dig.tx1.setText(value0);
					dig.tx2.setText(value1);
					dig.tx3.setText(value2);
					dig.tx4.setText(value3);

					dig.s.setVisible(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	// 把數(shù)據(jù)庫數(shù)據(jù)傳入表格
	public void databaseSearch(String SQL) {
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm.removeRow(i1);
				}
				dtm.setRowCount(0);
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);
			String[] data = new String[4];
			while (rs.next()) {
				for (int j = 1; j <= 4; j++) {
					data[j - 1] = rs.getString(j);
				}
				dtm.addRow(data);
			}
			con.close();
		} catch (Exception err) {
			String error = err.getMessage();
			JOptionPane.showMessageDialog(null, error);
			err.printStackTrace();
		} finally {
			dbUtil.close();
		}
	}

	// 科室信息修改操作對話框設(shè)置++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	class DepartmentModify implements ActionListener {

		public JDialog s = new JDialog();
		public JLabel la1, la2, la3, la4;
		public JTextField tx1, tx2, tx3, tx4, txWait;
		public JButton confirm, cancel;
		public Font laFont = new Font("宋體", Font.BOLD, 15);
		public int row = table.getSelectedRow();

		public DepartmentModify() {
			ImageIcon background = new ImageIcon("picture/right_bg.jpg");
			JLabel label = new JLabel(background);
			s.setModal(true);
			s.setSize(500, 250);
			s.setResizable(false);
			s.setLocationRelativeTo(null);
			s.setLayout(null);

			la1 = new JLabel("科室編號:");
			la2 = new JLabel("科室名稱:");
			la3 = new JLabel("科室主任:");
			la4 = new JLabel("科室電話:");
			tx1 = new JTextField();
			tx2 = new JTextField();
			tx3 = new JTextField();
			tx4 = new JTextField();
			txWait = new JTextField();
			confirm = new JButton("確定");
			cancel = new JButton("取消");

			tx1.setEditable(false);

			la1.setBounds(30, 30, 100, 40);
			la1.setFont(laFont);
			la2.setBounds(250, 30, 100, 40);
			la2.setFont(laFont);
			la3.setBounds(30, 90, 100, 40);
			la3.setFont(laFont);
			la4.setBounds(250, 90, 100, 40);
			la4.setFont(laFont);
			tx1.setBounds(110, 35, 120, 30);
			tx1.setFont(laFont);
			tx2.setBounds(330, 35, 120, 30);
			tx2.setFont(laFont);
			tx3.setBounds(110, 95, 120, 30);
			tx3.setFont(laFont);
			tx4.setBounds(330, 95, 120, 30);
			tx4.setFont(laFont);
			confirm.setBounds(110, 150, 100, 40);
			cancel.setBounds(330, 150, 100, 40);

			s.add(la1);
			s.add(la2);
			s.add(la3);
			s.add(la4);
			s.add(tx1);
			s.add(tx2);
			s.add(tx3);
			s.add(tx4);
			s.add(confirm);
			s.add(cancel);

			confirm.addActionListener(this);
			cancel.addActionListener(this);

			s.add(label);
			label.setBounds(0, 0, 500, 250);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == cancel) {
				s.dispose();
			}
			if (e.getSource() == confirm) {

				int ok = JOptionPane.showConfirmDialog(null, "是否修改該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
						JOptionPane.INFORMATION_MESSAGE);
				if (ok == JOptionPane.YES_OPTION) {
					try {
						DBUtil dbUtil = new DBUtil();
						Connection con = dbUtil.getConnection();
						Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
						// 輸入信息不能為空
						if (tx2.getText().equals("") || tx4.getText().equals("")) {
							JOptionPane.showMessageDialog(null, "請輸入完整的科室信息!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 編號為3位數(shù)字
						else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {
							JOptionPane.showMessageDialog(null, "請輸入正確的3位數(shù)科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 名字長度不能超過20個字符
						else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {
							JOptionPane.showMessageDialog(null, "名字長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 電話號碼為8位數(shù)字
						else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {
							JOptionPane.showMessageDialog(null, "請輸入正確的8位數(shù)電話號碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}

						else {

							int row2 = table.getSelectedRow();
							String sql = "update Department   set DeptName='" + tx2.getText().trim() + "',DrName='"
									+ tx3.getText().trim() + "',DeptPhone='" + tx4.getText().trim() + "'where DeptNo='"
									+ tx1.getText().trim() + "'  ";
							stmt.executeUpdate(sql);
							String[] data = new String[] { tx1.getText(), tx2.getText(), tx3.getText(), tx4.getText() };
							dtm.removeRow(row2);
							dtm.insertRow(row2, data);

							JOptionPane.showMessageDialog(null, "修改成功");
							s.dispose();

						}

					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

			}
		}

	}
}

DepartmentManage.java

package com.sjsq;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class DepartmentManage extends JFrame implements ActionListener {

	JPanel departmentManage = new JPanel();
	private JLabel manageTitle = new JLabel("科室信息綜合操作");
	private JLabel la1, la2, la3, la4, la5, la6;
	private JTextField tx1, tx2, tx3, tx4, tx5, tx6;
	public JButton save, query, modify, delete;
	private Font laFont = new Font("宋體", Font.BOLD, 15);
	private JComboBox jcbb1;
	private String str1[] = { "查詢?nèi)?, "按科室編號查詢", "按科室名稱查詢" };
	private final String[] columnNames = { "科室編號", "科室名稱", "科室主任", "科室電話" };
	private JScrollPane JScrollPane1 = new JScrollPane();
	private java.sql.Connection con = null;
	private static JTable table;
	private static DefaultTableModel dtm;
	private Pattern pattern = Pattern.compile("[0-9]*");

	public DepartmentManage() {
		// 背景設(shè)置
		departmentManage.setLayout(null);
		ImageIcon background = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(background);

		// 標題設(shè)置
		manageTitle.setFont(new Font("宋體", Font.BOLD, 50));
		manageTitle.setBounds(60, 10, 1000, 50);
		departmentManage.add(manageTitle);

		// 錄入操作面板設(shè)置
		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "錄入操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, Color.red));
		panel.setBounds(45, 65, 550, 150);
		panel.setOpaque(false);

		la1 = new JLabel("科室編號:");
		la2 = new JLabel("科室名稱:");
		la3 = new JLabel("科室主任:");
		la4 = new JLabel("科室電話:");
		tx1 = new JTextField();
		tx2 = new JTextField();
		tx3 = new JTextField();
		tx4 = new JTextField();
		save = new JButton("保存");

		la1.setBounds(20, 20, 100, 50);
		la1.setFont(laFont);
		la2.setBounds(240, 20, 100, 50);
		la2.setFont(laFont);
		la3.setBounds(20, 80, 100, 50);
		la3.setFont(laFont);
		la4.setBounds(240, 80, 100, 50);
		la4.setFont(laFont);
		tx1.setBounds(100, 30, 120, 30);
		tx1.setFont(laFont);
		tx2.setBounds(320, 30, 120, 30);
		tx2.setFont(laFont);
		tx3.setBounds(100, 90, 120, 30);
		tx3.setFont(laFont);
		tx4.setBounds(320, 90, 120, 30);
		tx4.setFont(laFont);
		save.setBounds(460, 100, 80, 40);

		panel.add(la1);
		panel.add(la2);
		panel.add(la3);
		panel.add(la4);
		panel.add(tx1);
		panel.add(tx2);
		panel.add(tx3);
		panel.add(tx4);
		panel.add(save);
		departmentManage.add(panel);

		// 查詢操縱面板設(shè)置
		final JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBorder(new TitledBorder(null, "查詢操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, Color.red));
		panel1.setBounds(620, 65, 380, 150);
		panel1.setOpaque(false);

		query = new JButton("查詢");
		la5 = new JLabel("科室編號:");
		la6 = new JLabel("科室名稱:");
		tx5 = new JTextField();
		tx6 = new JTextField();
		jcbb1 = new JComboBox(str1);
		jcbb1.setBounds(20, 28, 150, 25);
		jcbb1.setFont(laFont);
		la5.setBounds(20, 73, 80, 50);
		la5.setFont(laFont);
		la6.setBounds(20, 73, 80, 50);
		la6.setFont(laFont);
		tx5.setBounds(100, 80, 120, 30);
		tx5.setFont(laFont);
		tx6.setBounds(100, 80, 120, 30);
		tx6.setFont(laFont);
		query.setBounds(290, 100, 80, 40);

		la5.setVisible(false);
		la6.setVisible(false);
		tx5.setVisible(false);
		tx6.setVisible(false);

		panel1.add(la5);
		panel1.add(la6);
		panel1.add(tx5);
		panel1.add(tx6);
		panel1.add(jcbb1);
		panel1.add(query);
		departmentManage.add(panel1);

		// 表格設(shè)置
		defaultTableModel(); // 設(shè)置表格不可編輯
		setTableColumnCenter(); // 設(shè)置表格內(nèi)容居中顯示
		setbgcolor(); // 設(shè)置表格隔行不同顏色
		JScrollPane1.setBounds(new Rectangle(45, 230, 850, 270));
		JScrollPane1.setViewportView(table); // 創(chuàng)建一個滾動條(如果有必要)并設(shè)置其視圖
		// table.getColumnModel().getColumn(0).setMinWidth(40);
		// table.getColumnModel().getColumn(0).setMaxWidth(40);
		table.getTableHeader().setReorderingAllowed(false); // 列不可拖動
		table.getTableHeader().setResizingAllowed(false); // 列寬不能改變
		departmentManage.add(JScrollPane1);

		// 按鈕設(shè)置
		modify = new JButton("修改");
		delete = new JButton("刪除");
		// reflash=new JButton("刷新");
		modify.setBounds(910, 230, 80, 40);
		delete.setBounds(910, 300, 80, 40);
		// reflash.setBounds(910, 370, 80, 40);
		departmentManage.add(modify);
		departmentManage.add(delete);
		// departmentManage.add(reflash);

		// 添加監(jiān)聽器
		save.addActionListener(this);
		delete.addActionListener(this);
		query.addActionListener(this);
		modify.addActionListener(this);
		// reflash.addActionListener(this);
		jcbb1.addActionListener(this);

		// 添加背景
		departmentManage.add(label);
		label.setBounds(0, 0, 1100, 700);
	}

	// 設(shè)置表格不可編輯
	private void defaultTableModel() {
		dtm = new DefaultTableModel(columnNames, 0);
		table = new JTable(dtm) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
	}

	// 設(shè)置表格內(nèi)容居中顯示
	private void setTableColumnCenter() {
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, r);
	}

	// 設(shè)置表格隔行背景顏色不同
	private static void setbgcolor() {
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
						boolean hasFocus, int row, int column) {
					if (row % 2 == 0)
						setBackground(new Color(223, 220, 239)); // 設(shè)置奇數(shù)行底色
					else if (row % 2 == 1)
						setBackground(Color.white); // 設(shè)置偶數(shù)行底色
					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < table.getColumnCount(); i++) {
				table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jcbb1) {
			if (jcbb1.getSelectedIndex() == 0) {
				la5.setVisible(false);
				la6.setVisible(false);
				tx5.setVisible(false);
				tx6.setVisible(false);
				return;
			} else if (jcbb1.getSelectedIndex() == 1) {
				la5.setVisible(true);
				tx5.setVisible(true);
				la6.setVisible(false);
				tx6.setVisible(false);
				return;
			} else if (jcbb1.getSelectedIndex() == 2) {
				tx5.setVisible(false);
				la5.setVisible(false);
				tx6.setVisible(true);
				la6.setVisible(true);
				return;
			}
		}

		if (e.getSource() == save) { // 錄入操作
			// 輸入信息不能為空
			if (tx1.getText().equals("") || tx2.getText().equals("") || tx4.getText().equals("")) {
				JOptionPane.showMessageDialog(null, "請輸入完整的科室信息!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 編號為3位數(shù)字
			else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {
				JOptionPane.showMessageDialog(null, "請輸入正確的3位數(shù)科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 名字長度不能超過20個字符
			else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {
				JOptionPane.showMessageDialog(null, "名字長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}
			// 電話號碼為8位數(shù)字
			else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {
				JOptionPane.showMessageDialog(null, "請輸入正確的8位數(shù)電話號碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
			}

			else {
				String deptNo = tx1.getText().trim();
				String deptName = tx2.getText();
				String deptPhone = tx4.getText().trim();
				try {
					DBUtil dbUtil = new DBUtil();
					Connection con = dbUtil.getConnection();
					Statement st1 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					Statement st2 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					Statement st3 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					ResultSet rs1 = st1.executeQuery("select * from Department where DeptNo='" + deptNo + "'"); // SQL語句
					ResultSet rs2 = st2.executeQuery("select * from Department where DeptName='" + deptName + "'");
					ResultSet rs3 = st3.executeQuery("select * from Department where DeptPhone='" + deptPhone + "'");

					if (rs1.next()) { // 判斷結(jié)果集rs是否有記錄,并且將指針后移一位
						JOptionPane.showMessageDialog(null, "該科室號已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st1.close();
					} else if (rs2.next()) {
						JOptionPane.showMessageDialog(null, "該科室名已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st2.close();
					} else if (rs3.next()) {
						JOptionPane.showMessageDialog(null, "該科室電話號碼已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						st3.close();
					}

					else {
						int ok = JOptionPane.showConfirmDialog(null, "是否保存該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
								JOptionPane.INFORMATION_MESSAGE);
						if (ok == JOptionPane.YES_OPTION) {
							try {
								// 信息添加到數(shù)據(jù)庫
								String sql = "INSERT INTO Department(DeptNo,DeptName,DrName,DeptPhone)VALUES(?,?,?,?)";
								PreparedStatement parepare = con.prepareStatement(sql);
								parepare.setString(1, tx1.getText());
								parepare.setString(2, tx2.getText());
								parepare.setString(3, tx3.getText());
								parepare.setString(4, tx4.getText());
								parepare.executeUpdate();

								String[] data = new String[] { tx1.getText(), tx2.getText(), tx3.getText(),
										tx4.getText() };
								dtm.addRow(data); // 在表格添加一行剛添加的數(shù)據(jù)
								JOptionPane.showMessageDialog(null, "錄入成功");
								tx1.setText("");
								tx2.setText("");
								tx3.setText("");
								tx4.setText("");

							} catch (Exception e1) {
								e1.printStackTrace();
								System.out.println("SQL Exception occur.Message is:");
								System.out.println(e1.getMessage());
							}
						}
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}

		if (e.getSource() == query) { // 查詢操作
			try {
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				if (jcbb1.getSelectedIndex() == 0) { // 全部查詢
					String sql = "select * from Department";
					databaseSearch(sql);
					if (table.getRowCount() != 0) {
						JOptionPane.showMessageDialog(null, "查詢成功!");
					} else {
						JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
					}
				}
				if (jcbb1.getSelectedIndex() == 1) { // 編號查詢
					if (tx5.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(null, "請輸入正確的科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
					} else {
						String deptNo = tx5.getText().trim();
						String sql = "select * from Department where DeptNo like'%" + deptNo + "%'";
						databaseSearch(sql);
						if (table.getRowCount() != 0) {
							JOptionPane.showMessageDialog(null, "查詢成功!");
							tx5.setText("");
						} else {
							JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
						}
					}
				}
				if (jcbb1.getSelectedIndex() == 2) { // 名稱查詢
					if (tx6.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(null, "請輸入正確的科室名稱!", "錯誤", JOptionPane.ERROR_MESSAGE);
					} else {
						String deptName = tx6.getText();
						String sql = "select * from Department where DeptName like '%" + deptName + "%'";
						databaseSearch(sql);
						if (table.getRowCount() != 0) {
							JOptionPane.showMessageDialog(null, "查詢成功!");
							tx6.setText("");
						} else {
							JOptionPane.showMessageDialog(null, "沒有找到您要的信息!");
						}
					}
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if (e.getSource() == delete) { // 刪除操作
			try {
				DBUtil dbUtil = new DBUtil();
				Connection con = dbUtil.getConnection();
				Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
				int selectCount = table.getSelectedRowCount();
				if (selectCount == 0) {
					JOptionPane.showMessageDialog(null, "請選擇您要刪除的信息!");
				} else if (selectCount == 1) {
					int ok = JOptionPane.showConfirmDialog(null, "是否刪除該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
							JOptionPane.INFORMATION_MESSAGE);
					if (ok == JOptionPane.YES_OPTION) {
						int row = table.getSelectedRow();
						String deptNo = (String) table.getValueAt(row, 0);
						String sql = "delete from Department where DeptNo='" + deptNo + "'";
						stmt.executeUpdate(sql);
						dtm.removeRow(row);
						JOptionPane.showMessageDialog(null, "刪除成功!");
					}
				} else {
					int ok = JOptionPane.showConfirmDialog(null, "是否刪除所選" + selectCount + "個科室信息?", "確定",
							JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
					if (ok == JOptionPane.YES_OPTION) {
						for (int i = 1; i <= selectCount; i++) {
							int row1 = table.getSelectedRow();
							String deptNo = (String) table.getValueAt(row1, 0);
							String sql = "delete from Department where DeptNo='" + deptNo + "'";
							stmt.executeUpdate(sql);
							dtm.removeRow(row1);
						}
						JOptionPane.showMessageDialog(null, "刪除成功!");
					}
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

		// 修改操作設(shè)置
		if (e.getSource() == modify) {
			if (table.getSelectedRowCount() != 1) {
				JOptionPane.showMessageDialog(null, "請選擇一項科室信息進行修改!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else {
				try {
					DBUtil dbUtil = new DBUtil();
					Connection con = dbUtil.getConnection();
					Statement stmt1 = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象

					int row = table.getSelectedRow();

					String value0 = (String) table.getValueAt(row, 0);
					String value1 = (String) table.getValueAt(row, 1);
					String value2 = (String) table.getValueAt(row, 2);
					String value3 = (String) table.getValueAt(row, 3);

					DepartmentModify dig = new DepartmentModify();
					dig.tx1.setText(value0);
					dig.tx2.setText(value1);
					dig.tx3.setText(value2);
					dig.tx4.setText(value3);

					dig.s.setVisible(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	// 把數(shù)據(jù)庫數(shù)據(jù)傳入表格
	public void databaseSearch(String SQL) {
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm.removeRow(i1);
				}
				dtm.setRowCount(0);
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);
			String[] data = new String[4];
			while (rs.next()) {
				for (int j = 1; j <= 4; j++) {
					data[j - 1] = rs.getString(j);
				}
				dtm.addRow(data);
			}
			con.close();
		} catch (Exception err) {
			String error = err.getMessage();
			JOptionPane.showMessageDialog(null, error);
			err.printStackTrace();
		} finally {
			dbUtil.close();
		}
	}

	// 科室信息修改操作對話框設(shè)置++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	class DepartmentModify implements ActionListener {

		public JDialog s = new JDialog();
		public JLabel la1, la2, la3, la4;
		public JTextField tx1, tx2, tx3, tx4, txWait;
		public JButton confirm, cancel;
		public Font laFont = new Font("宋體", Font.BOLD, 15);
		public int row = table.getSelectedRow();

		public DepartmentModify() {
			ImageIcon background = new ImageIcon("picture/right_bg.jpg");
			JLabel label = new JLabel(background);
			s.setModal(true);
			s.setSize(500, 250);
			s.setResizable(false);
			s.setLocationRelativeTo(null);
			s.setLayout(null);

			la1 = new JLabel("科室編號:");
			la2 = new JLabel("科室名稱:");
			la3 = new JLabel("科室主任:");
			la4 = new JLabel("科室電話:");
			tx1 = new JTextField();
			tx2 = new JTextField();
			tx3 = new JTextField();
			tx4 = new JTextField();
			txWait = new JTextField();
			confirm = new JButton("確定");
			cancel = new JButton("取消");

			tx1.setEditable(false);

			la1.setBounds(30, 30, 100, 40);
			la1.setFont(laFont);
			la2.setBounds(250, 30, 100, 40);
			la2.setFont(laFont);
			la3.setBounds(30, 90, 100, 40);
			la3.setFont(laFont);
			la4.setBounds(250, 90, 100, 40);
			la4.setFont(laFont);
			tx1.setBounds(110, 35, 120, 30);
			tx1.setFont(laFont);
			tx2.setBounds(330, 35, 120, 30);
			tx2.setFont(laFont);
			tx3.setBounds(110, 95, 120, 30);
			tx3.setFont(laFont);
			tx4.setBounds(330, 95, 120, 30);
			tx4.setFont(laFont);
			confirm.setBounds(110, 150, 100, 40);
			cancel.setBounds(330, 150, 100, 40);

			s.add(la1);
			s.add(la2);
			s.add(la3);
			s.add(la4);
			s.add(tx1);
			s.add(tx2);
			s.add(tx3);
			s.add(tx4);
			s.add(confirm);
			s.add(cancel);

			confirm.addActionListener(this);
			cancel.addActionListener(this);

			s.add(label);
			label.setBounds(0, 0, 500, 250);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == cancel) {
				s.dispose();
			}
			if (e.getSource() == confirm) {

				int ok = JOptionPane.showConfirmDialog(null, "是否修改該科室信息?", "確定", JOptionPane.YES_NO_OPTION,
						JOptionPane.INFORMATION_MESSAGE);
				if (ok == JOptionPane.YES_OPTION) {
					try {
						DBUtil dbUtil = new DBUtil();
						Connection con = dbUtil.getConnection();
						Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
						// 輸入信息不能為空
						if (tx2.getText().equals("") || tx4.getText().equals("")) {
							JOptionPane.showMessageDialog(null, "請輸入完整的科室信息!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 編號為3位數(shù)字
						else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {
							JOptionPane.showMessageDialog(null, "請輸入正確的3位數(shù)科室編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 名字長度不能超過20個字符
						else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {
							JOptionPane.showMessageDialog(null, "名字長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}
						// 電話號碼為8位數(shù)字
						else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {
							JOptionPane.showMessageDialog(null, "請輸入正確的8位數(shù)電話號碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
						}

						else {

							int row2 = table.getSelectedRow();
							String sql = "update Department   set DeptName='" + tx2.getText().trim() + "',DrName='"
									+ tx3.getText().trim() + "',DeptPhone='" + tx4.getText().trim() + "'where DeptNo='"
									+ tx1.getText().trim() + "'  ";
							stmt.executeUpdate(sql);
							String[] data = new String[] { tx1.getText(), tx2.getText(), tx3.getText(), tx4.getText() };
							dtm.removeRow(row2);
							dtm.insertRow(row2, data);

							JOptionPane.showMessageDialog(null, "修改成功");
							s.dispose();

						}

					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

			}
		}

	}
}

DoctorInput.java

package com.sjsq;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class DoctorInput extends JFrame implements ActionListener {

	JPanel doctorInput = new JPanel();
	private JLabel inputTitle = new JLabel("醫(yī)生檔案信息錄入");
	private JLabel la1, la2, la3, la4, la5, la6, la7, la8, la9, la10, la11;
	private JTextField tx1, tx2, tx8, tx10;
	private JPasswordField tx11;
	public JButton save, clean;
	private JComboBox sex, age, position, educated, department;
	private String str3[], str4[], str5[], str6[], str7[];
	private final String columnNames[];
	private JTextField showDate1 = new JTextField("單擊選擇日期");
	private Timedate dateChooser1 = Timedate.getInstance("yyyy-MM-dd");
	private Font laFont = new Font("宋體", Font.BOLD, 15);
	private JScrollPane JScrollPane1 = new JScrollPane();
	private static JTable table;
	private static DefaultTableModel dtm;
	private java.sql.Connection con = null;
	private Pattern pattern = Pattern.compile("[0-9]*");

	public DoctorInput() {
		// 醫(yī)生信息錄入界面設(shè)置
		doctorInput.setLayout(null);
		ImageIcon background = new ImageIcon("picture/right_bg.jpg"); // 背景圖片
		JLabel label = new JLabel(background);
		inputTitle.setFont(new Font("宋體", Font.BOLD, 50));
		inputTitle.setBounds(60, 10, 1000, 50);
		doctorInput.add(inputTitle);

		// 錄入操作面板設(shè)置
		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(null, "錄入操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, Color.red));
		panel.setBounds(60, 310, 950, 200);
		panel.setOpaque(false);

		la1 = new JLabel("醫(yī)生編號:"); // 標簽設(shè)置
		la2 = new JLabel("醫(yī)生姓名:");
		la3 = new JLabel("性別:");
		la4 = new JLabel("年齡:");
		la5 = new JLabel("職稱:");
		la6 = new JLabel("學(xué)歷:");
		la7 = new JLabel("所屬科室:");
		la8 = new JLabel("籍貫:");
		la9 = new JLabel("入職日期:");
		la10 = new JLabel("電話號碼:");
		la11 = new JLabel("系統(tǒng)密碼:");

		la1.setBounds(30, 20, 100, 40);
		la1.setFont(laFont);
		la2.setBounds(300, 20, 100, 40);
		la2.setFont(laFont);
		la3.setBounds(570, 20, 100, 40);
		la3.setFont(laFont);
		la4.setBounds(750, 20, 100, 40);
		la4.setFont(laFont);
		la5.setBounds(30, 70, 100, 40);
		la5.setFont(laFont);
		la6.setBounds(210, 70, 100, 40);
		la6.setFont(laFont);
		la7.setBounds(390, 70, 100, 40);
		la7.setFont(laFont);
		la8.setBounds(640, 70, 100, 40);
		la8.setFont(laFont);
		la9.setBounds(30, 120, 100, 40);
		la9.setFont(laFont);
		la10.setBounds(300, 120, 100, 40);
		la10.setFont(laFont);
		la11.setBounds(570, 120, 100, 40);
		la11.setFont(laFont);

		tx1 = new JTextField();
		tx1.setBounds(110, 25, 150, 30);
		tx1.setFont(laFont);
		tx2 = new JTextField();
		tx2.setBounds(380, 25, 150, 30);
		tx2.setFont(laFont);
		tx8 = new JTextField();
		tx8.setBounds(690, 75, 150, 30);
		tx8.setFont(laFont);
		tx10 = new JTextField();
		tx10.setBounds(380, 125, 150, 30);
		tx10.setFont(laFont);
		tx11 = new JPasswordField();
		tx11.setBounds(650, 125, 150, 30);
		tx11.setFont(laFont);

		save = new JButton("保存");
		clean = new JButton("清空");
		save.setBounds(630, 163, 150, 30);
		clean.setBounds(780, 163, 150, 30);

		// 性別下拉框設(shè)置
		str3 = new String[] { "", "男", "女" };
		sex = new JComboBox(str3);
		sex.setBounds(620, 25, 100, 25);
		sex.setFont(laFont);

		// 年齡下拉框設(shè)置
		str4 = new String[54];
		str4[0] = "";
		for (int i = 1; i <= 53; i++) {
			str4[i] = String.valueOf(i + 17);
		}
		age = new JComboBox(str4);
		age.setBounds(800, 25, 100, 25);
		age.setFont(laFont);

		// 職稱下拉框設(shè)置
		str5 = new String[] { "", "醫(yī)師", "主治醫(yī)師", "副主任醫(yī)師", "主任醫(yī)師" };
		position = new JComboBox(str5);
		position.setBounds(80, 75, 100, 25);
		position.setFont(laFont);

		// 學(xué)歷下拉框設(shè)置
		str6 = new String[] { "", "大專", "本科", "碩士", "博士", "博士后" };
		educated = new JComboBox(str6);
		educated.setBounds(260, 75, 100, 25);
		educated.setFont(laFont);

		// 所屬科室下拉框設(shè)置
		str7 = new String[] { "" };
		department = new JComboBox(str7);
		DBUtil dbUtil = new DBUtil();
		try {
			
			Connection con = dbUtil.getConnection();
			Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
			String sql = "select * from Department";
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				department.addItem(rs.getString("DeptName"));
			}
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		department.setBounds(470, 75, 125, 25);
		department.setFont(laFont);

		// 入職時間設(shè)置
		showDate1.setFont(new Font("", 1, 20));
		dateChooser1.register(showDate1);
		panel.add(showDate1);
		showDate1.setBounds(110, 125, 150, 30);

		// 表格設(shè)置
		columnNames = new String[] { "醫(yī)生編號", "醫(yī)生姓名", "性別", "年齡", "職稱", "學(xué)歷", "所屬科室", "籍貫", "入職日期", "電話號碼" };
		defaultTableModel();
		setTableColumnCenter();
		setbgcolor();
		JScrollPane1.setBounds(new Rectangle(60, 70, 935, 230));
		doctorInput.add(JScrollPane1);
		JScrollPane1.setViewportView(table); // 創(chuàng)建一個視口(如果有必要)并設(shè)置其視圖
		table.getTableHeader().setReorderingAllowed(false); // 列不可拖動
		table.getTableHeader().setResizingAllowed(false); // 列寬不能改變

		String sql = "select * from Doctor";
		databaseSearch(sql);

		// 組件添加
		panel.add(la1);
		panel.add(la2);
		panel.add(la3);
		panel.add(la4);
		panel.add(la5);
		panel.add(la6);
		panel.add(la7);
		panel.add(la8);
		panel.add(la9);
		panel.add(la10);
		panel.add(la11);
		panel.add(tx1);
		panel.add(tx2);
		panel.add(sex);
		panel.add(age);
		panel.add(position);
		panel.add(educated);
		panel.add(department);
		panel.add(tx8);
		panel.add(tx10);
		panel.add(tx11);
		panel.add(save);
		panel.add(clean);

		// 添加監(jiān)聽器
		save.addActionListener(this);
		clean.addActionListener(this);

		doctorInput.add(panel);

		doctorInput.add(label); // 添加背景
		label.setBounds(0, 0, 1100, 700);
	}

	// 數(shù)據(jù)庫數(shù)據(jù)錄入到表格中
	private void databaseSearch(String sql) {
		// TODO Auto-generated method stub
		DBUtil dbUtil = new DBUtil();
		Connection con = dbUtil.getConnection();
		ResultSet rs;
		try {
			int rowcount = dtm.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm.removeRow(i1);
				}
				dtm.setRowCount(0);
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			String[] data = new String[11];
			while (rs.next()) {
				for (int j = 1; j <= 11; j++) {
					data[j - 1] = rs.getString(j);
				}
				dtm.addRow(data);
			}
			con.close();
		} catch (Exception err) {
			String error = err.getMessage();
			JOptionPane.showMessageDialog(null, error);
			err.printStackTrace();
		} finally {
			dbUtil.close();
		}

	}

	// 設(shè)置表格不可編輯
	public void defaultTableModel() {
		dtm = new DefaultTableModel(columnNames, 0);
		table = new JTable(dtm) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
	}

	// 設(shè)置表格內(nèi)容居中顯示
	public void setTableColumnCenter() {
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, r);
	}

	// 設(shè)置表格隔行背景顏色不同
	public static void setbgcolor() {
		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
						boolean hasFocus, int row, int column) {
					if (row % 2 == 0)
						setBackground(new Color(223, 220, 239)); // 設(shè)置奇數(shù)行底色
					else if (row % 2 == 1)
						setBackground(Color.white); // 設(shè)置偶數(shù)行底色
					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < table.getColumnCount(); i++) {
				table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == clean) {
			tx1.setText("");
			tx2.setText("");
			tx8.setText("");
			tx10.setText("");
			tx11.setText("");
			sex.setSelectedIndex(0);
			age.setSelectedIndex(0);
			position.setSelectedIndex(0);
			educated.setSelectedIndex(0);
			department.setSelectedIndex(0);
			showDate1.setText("單擊選擇日期");
		}
		if (e.getSource() == save) {
			if (tx1.getText().equals("") || tx2.getText().equals("") || tx8.getText().equals("")
					|| tx10.getText().equals("") || tx11.getText().equals("") || sex.getSelectedIndex() == 0
					|| age.getSelectedIndex() == 0 || position.getSelectedIndex() == 0
					|| educated.getSelectedIndex() == 0 || department.getSelectedIndex() == 0
					|| showDate1.getText().equals("單擊選擇日期")) {
				JOptionPane.showMessageDialog(null, "請輸完整的醫(yī)生信息!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else if (tx1.getText().length() != 4 || pattern.matcher(tx1.getText()).matches() == false) {
				JOptionPane.showMessageDialog(null, "請輸入4位數(shù)醫(yī)生編號!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else if (tx2.getText().length() > 10) {
				JOptionPane.showMessageDialog(null, "名字長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else if (tx8.getText().length() > 10) {
				JOptionPane.showMessageDialog(null, "籍貫長度不能超過10個漢字!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else if (tx10.getText().length() != 11 || pattern.matcher(tx10.getText()).matches() == false) {
				JOptionPane.showMessageDialog(null, "請輸入11位數(shù)手機號碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else if (tx11.getText().length() < 6 || tx11.getText().length() > 8
					|| pattern.matcher(tx11.getText()).matches() == false) {
				JOptionPane.showMessageDialog(null, "請輸入6-8位數(shù)字密碼!", "錯誤", JOptionPane.ERROR_MESSAGE);
			} else {
				String DrId = tx1.getText();
				try {
					DBUtil dbUtil = new DBUtil();
					Connection con = dbUtil.getConnection();
					Statement stmt = con.createStatement(); // 創(chuàng)建一個數(shù)據(jù)庫會話對象
					ResultSet rs = stmt.executeQuery("select * from Doctor where DrId='" + DrId + "'"); // SQL語句
					if (rs.next()) {
						JOptionPane.showMessageDialog(null, "該醫(yī)生編號已存在,請重新輸入!", "錯誤", JOptionPane.ERROR_MESSAGE);
						rs.close();
					} else {
						int ok = JOptionPane.showConfirmDialog(null, "是否保存該醫(yī)生信息?", "確定", JOptionPane.YES_NO_OPTION,
								JOptionPane.INFORMATION_MESSAGE);
						if (ok == JOptionPane.YES_OPTION) {
							// 添加信息到數(shù)據(jù)庫
							String sql = "INSERT INTO Doctor(DrId,DrName,DrSex,DrAge,DrPos,DrEdu,DeptName,DrNative,DrDate,DrPhone,Password)VALUES(?,?,?,?,?,?,?,?,?,?,?)";
							PreparedStatement parepare = con.prepareStatement(sql);
							parepare.setString(1, tx1.getText());
							parepare.setString(2, tx2.getText());
							parepare.setString(3, sex.getSelectedItem().toString());
							parepare.setString(4, age.getSelectedItem().toString());
							parepare.setString(5, position.getSelectedItem().toString());
							parepare.setString(6, educated.getSelectedItem().toString());
							parepare.setString(7, department.getSelectedItem().toString());
							parepare.setString(8, tx8.getText());
							parepare.setString(9, showDate1.getText());
							parepare.setString(10, tx10.getText());
							parepare.setString(11, tx11.getText());
							parepare.executeUpdate();

							String data[] = new String[] { tx1.getText(), tx2.getText(),
									sex.getSelectedItem().toString(), age.getSelectedItem().toString(),
									position.getSelectedItem().toString(), educated.getSelectedItem().toString(),
									department.getSelectedItem().toString(), tx8.getText(), showDate1.getText(),
									tx10.getText(), tx11.getText() };
							dtm.addRow(data); // 在表格添加一行剛添加的數(shù)據(jù)
							JOptionPane.showMessageDialog(null, "錄入成功");
							tx1.setText("");
							tx2.setText("");
							tx8.setText("");
							tx10.setText("");
							tx11.setText("");
							sex.setSelectedIndex(0);
							age.setSelectedIndex(0);
							position.setSelectedIndex(0);
							educated.setSelectedIndex(0);
							department.setSelectedIndex(0);
							showDate1.setText("單擊選擇日期");
						}
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}finally {
					
				}
			}
		}
	}

}

Drug.java

package com.sjsq;
import java.awt.Font;
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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Pattern;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Drug extends JFrame implements ActionListener {
	JPanel chufangInput = new JPanel();
	private JButton button1, button2, button3, button4, button5, button6, button7, button8;
	private JLabel inputTitle = new JLabel("處方單錄入");
	private JLabel la0, la1, la2, la3, la4, la5, la6, la7, la8;
	private Font laFont = new Font("宋體", Font.BOLD, 15);
	private Pattern pattern = Pattern.compile("[0-9]*");
	private Connection con = null;
	private JLabel t1, t2;
	private static JTable table1, table2, table3, table4, table5;
	private static DefaultTableModel dtm1, dtm2, dtm3, dtm4, dtm5;
	private JScrollPane JScrollPane = new JScrollPane();
	private JScrollPane JScrollPane1 = new JScrollPane();
	private JScrollPane JScrollPane4 = new JScrollPane();
	private JScrollPane JScrollPane5 = new JScrollPane();
	private String columnNames1[] = { "編碼", "名稱", "單價", "數(shù)量", "計數(shù)單位", "類別", "病例編碼" };
	private String columnNames2[] = { "編碼", "名稱", "單價", "計數(shù)單位", "類別" };
	private String columnNames3[] = { "病例編碼", "病人編號", "病人姓名" };
	private JTextField tx1, tx2, tx3, tx4, tx5;
	private JComboBox box1, box2, box5;

	
	
	public Drug() {
		DBUtil dbUtil = new DBUtil();
		
		
		chufangInput.setLayout(null);

		dtm1 = new DefaultTableModel(columnNames1, 0) {// dtm2是項目收費表格模版
			public boolean isCellEditable(int row, int column) {
				if (column == 1 || column == 3)
					return true;// 這個是可以編輯的列
				// if(rowIndex!=0) return false;
				return false;
			}// 表格不允許被編輯 }
		};

		String fontSize1[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "12" };
		table1 = new JTable(dtm1);// JScrollPane4 項目表
		JScrollPane JScrollPane = new JScrollPane(table1);
		TableColumn a1 = table1.getColumn("名稱");
		TableColumn a2 = table1.getColumn("數(shù)量");
		JTextField box3 = new JTextField();
		box2 = new JComboBox(fontSize1);
		box3.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {

				updata_combobox();
			}

			private void updata_combobox() {
				String s1 = null;
				s1 = box3.getText();
				System.out.println(s1);
				JScrollPane1.setVisible(true);
				String sql = "select * from Price where PeName like  '%" + s1 + "%'and PeClass='其他類'";
				databaseSearch1(sql, 5);

			}
		});

		box3.setEditable(true);
		DefaultCellEditor dce2 = new DefaultCellEditor(box3);
		a1.setCellEditor(dce2);

		box2.setEditable(true);
		box2.setMaximumRowCount(5);
		DefaultCellEditor dce3 = new DefaultCellEditor(box2);
		a2.setCellEditor(dce3);
		box2.addActionListener(this);

		dtm2 = new DefaultTableModel(columnNames2, 0);// 項目明細表
		table2 = new JTable(dtm2) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};
		String sql = "select * from Price where PeClass='其他類'";
		databaseSearch1(sql, 5);

		JScrollPane1.setViewportView(table2);
		chufangInput.add(JScrollPane1);
		JScrollPane1.setBounds(70, 150, 400, 100);
		JScrollPane1.setVisible(false);

		// 設(shè)置用藥表
		dtm3 = new DefaultTableModel(columnNames1, 0) {// dtm3是藥物收費表格模版
			public boolean isCellEditable(int row, int column) {
				if (column == 1 || column == 3)
					return true;// 這個是可以編輯的列
				// if(rowIndex!=0) return false;
				return false;
			}// 表格不允許被編輯 }
		};

		table3 = new JTable(dtm3);//
		JScrollPane JScrollPane3 = new JScrollPane(table3);
		TableColumn b1 = table3.getColumn("名稱");
		TableColumn b2 = table3.getColumn("數(shù)量");
		JTextField box4 = new JTextField();
		box5 = new JComboBox(fontSize1);

		box4.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {

				updata_combobox();
			}

			private void updata_combobox() {
				String s1 = null;
				s1 = box4.getText();

				JScrollPane4.setVisible(true);
				String sql1 = "select * from Price where PeName like '%" + s1 + "%' and PeClass='診斷類'or PeName like '%"
						+ s1 + "%' and PeClass='藥品類'";
				databaseSearch2(sql1, 5);
			}
		});

		dtm4 = new DefaultTableModel(columnNames2, 0);// 藥物明細表
		table4 = new JTable(dtm4) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};
		String sql1 = "select * from Price where PeClass='診斷類'or PeClass='藥品類'";
		databaseSearch2(sql1, 5);

		JScrollPane4.setViewportView(table4);
		chufangInput.add(JScrollPane4);
		JScrollPane4.setBounds(550, 150, 400, 100);
		JScrollPane4.setVisible(false);

		box4.setEditable(true);
		DefaultCellEditor dce1 = new DefaultCellEditor(box4);
		b1.setCellEditor(dce1);

		box2.setEditable(true);
		box2.setMaximumRowCount(5);
		DefaultCellEditor dce0 = new DefaultCellEditor(box5);
		b2.setCellEditor(dce0);

		dtm5 = new DefaultTableModel(columnNames3, 0);// 項目明細表
		table5 = new JTable(dtm5) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}// 表格不允許被編輯 }
		};
		String sql2 = "select * from Medical_records where MrId not in(select MrId from DrugTable)";
		databaseSearch3(sql2, 3);

		JScrollPane5.setViewportView(table5);
		chufangInput.add(JScrollPane5);
		JScrollPane5.setBounds(120, 100, 300, 100);
		JScrollPane5.setVisible(false);

		// 設(shè)置背景
		ImageIcon background = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(background);
		inputTitle.setFont(new Font("宋體", Font.BOLD, 30));
		inputTitle.setBounds(60, 10, 1000, 50);
		chufangInput.add(inputTitle);
		;
		// 設(shè)置控件
		la1 = new JLabel("病例編號:");
		la2 = new JLabel("病人編號:");
		la3 = new JLabel("病人姓名:");
		t1 = new JLabel("項目單");
		t2 = new JLabel("藥物單");
		tx1 = new JTextField();
		tx2 = new JTextField();
		tx3 = new JTextField();
		button1 = new JButton("增加");
		button2 = new JButton("確定");
		button3 = new JButton("修改");
		button4 = new JButton("刪除");
		button5 = new JButton("增加");
		button6 = new JButton("確定");
		button7 = new JButton("修改");
		button8 = new JButton("刪除");
		button2.addActionListener(this);
		button6.addActionListener(this);
		// 給按鈕添加監(jiān)聽事件
		button1.addActionListener(new ActionListener() {// 添加事件
			public void actionPerformed(ActionEvent e) {
				String[] da1 = { "", "" };
				String[] rowValues = da1;

				dtm1.addRow(rowValues); // 添加一行

				button1.setEnabled(false);

			}
		});
		button5.addActionListener(new ActionListener() {// 藥物表添加事件
			public void actionPerformed(ActionEvent e) {

				String[] da = { "", "" };
				String[] rowValues = da;
				dtm3.addRow(rowValues); // 添加一行

				button5.setEnabled(false);

			}
		});
		button4.addMouseListener(new MouseAdapter() { // 刪除按鈕實現(xiàn)刪除記錄的功能
			public void mouseClicked(MouseEvent e) {
				int row = table1.getSelectedRow();// 這句選擇要刪除的行
				Connection con;

				con = dbUtil.getConnection();
				Statement stmt;
				String val = (String) table1.getValueAt(row, 6);
				String val1 = (String) table1.getValueAt(row, 0);
				String sql = "delete from DrugTable where MrId='" + val + "'and PeNo='" + val1 + "'";
				try {
					stmt = con.createStatement();
					stmt.executeUpdate(sql);
					button1.setEnabled(true);

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (row != -1) { // 這句判斷是否有選中的行
					dtm1.removeRow(row);
				} // 這句刪除指定行

			}
		});
		button8.addMouseListener(new MouseAdapter() { // 刪除按鈕實現(xiàn)刪除記錄的功能
			public void mouseClicked(MouseEvent e) {
				int row = table3.getSelectedRow();// 這句選擇要刪除的行
				Connection con;

				con = dbUtil.getConnection();
				Statement stmt;
				String val = (String) table3.getValueAt(row, 6);
				String val1 = (String) table3.getValueAt(row, 0);
				String sql = "delete from DrugTable where MrId='" + val1 + "'and PeNo='" + val + "'";
				try {
					stmt = con.createStatement();
					stmt.executeUpdate(sql);
					button5.setEnabled(true);

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (row != -1) { // 這句判斷是否有選中的行
					dtm3.removeRow(row);
				} // 這句刪除指定行

			}
		});
		box3.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					JScrollPane1.setVisible(true);
			}
		});
		box4.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					JScrollPane4.setVisible(true);
			}
		});

		table2.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 2) {
						String xingming = tx1.getText();
						int o = table2.getSelectedRow();
						int row = table1.getSelectedRow();
						String ao = (String) table2.getValueAt(o, 1);
						String bo = (String) table2.getValueAt(o, 0);
						String co = (String) table2.getValueAt(o, 2);
						String do1 = (String) table2.getValueAt(o, 3);
						String eo = (String) table2.getValueAt(o, 4);

						box3.setText(ao);
						table1.setValueAt(bo, row, 0);
						table1.setValueAt(do1, row, 4);
						table1.setValueAt(co, row, 2);
						table1.setValueAt(eo, row, 5);
						table1.setValueAt(xingming, row, 6);

						JScrollPane1.setVisible(false);

					}
			}
		});
		tx1.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					JScrollPane5.setVisible(true);
			}
		});

		tx1.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {

				updata_combobox();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {

				updata_combobox();
			}

			private void updata_combobox() {
				String s1 = null;
				s1 = tx1.getText();

				JScrollPane5.setVisible(true);
				String sql2 = "select * from Medical_records where MrId like '%" + s1
						+ "%'and MrId not in(select MrId from DrugTable)";
				databaseSearch3(sql2, 3);
			}
		});

		table4.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 2) {
						String xingming = tx1.getText();
						int o = table4.getSelectedRow();
						int row = table3.getSelectedRow();
						String ao = (String) table4.getValueAt(o, 1);
						String bo = (String) table4.getValueAt(o, 0);
						String co = (String) table4.getValueAt(o, 2);
						String do1 = (String) table4.getValueAt(o, 3);
						String eo = (String) table4.getValueAt(o, 4);

						box4.setText(ao);
						table3.setValueAt(bo, row, 0);
						table3.setValueAt(do1, row, 4);
						table3.setValueAt(co, row, 2);
						table3.setValueAt(eo, row, 5);
						table3.setValueAt(xingming, row, 6);

						JScrollPane4.setVisible(false);

					}
			}
		});
		table5.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 2) {

						int o = table5.getSelectedRow();

						String ao = (String) table5.getValueAt(o, 1);
						String bo = (String) table5.getValueAt(o, 0);
						String co = (String) table5.getValueAt(o, 2);

						tx1.setText(bo);
						tx2.setText(ao);
						tx3.setText(co);

						JScrollPane5.setVisible(false);

					}
			}
		});

		// 添加控件到面板以及設(shè)置控件位置
		chufangInput.add(la1);
		chufangInput.add(la2);
		chufangInput.add(la3);

		chufangInput.add(tx1);
		chufangInput.add(tx2);
		chufangInput.add(tx3);
		chufangInput.add(button1);
		chufangInput.add(button2);
		chufangInput.add(button3);
		chufangInput.add(button4);
		chufangInput.add(button5);
		chufangInput.add(button6);
		chufangInput.add(button7);
		chufangInput.add(button8);
		chufangInput.add(t1);
		chufangInput.add(t2);
		button1.setBounds(460, 250, 70, 50);
		button2.setBounds(460, 300, 70, 50);
		button3.setBounds(460, 350, 70, 50);
		button4.setBounds(460, 400, 70, 50);
		button5.setBounds(950, 250, 70, 50);
		button6.setBounds(950, 300, 70, 50);
		button7.setBounds(950, 350, 70, 50);
		button8.setBounds(950, 400, 70, 50);
		t1.setBounds(50, 200, 100, 50);
		t2.setBounds(520, 200, 100, 50);
		la1.setBounds(50, 70, 100, 40);
		la2.setBounds(300, 70, 100, 40);
		la3.setBounds(500, 70, 100, 40);
		tx1.setBounds(120, 75, 100, 30);
		tx2.setBounds(370, 75, 100, 30);
		tx3.setBounds(570, 75, 100, 30);
		chufangInput.add(JScrollPane);
		JScrollPane.setBounds(60, 250, 400, 200);
		chufangInput.add(JScrollPane3);
		JScrollPane3.setBounds(550, 250, 400, 200);
		chufangInput.add(label);
		label.setBounds(0, 0, 1100, 700);
	}

	private void databaseSearch3(String sql2, int i) {
		// TODO Auto-generated method stub
		Connection con;
		DBUtil dbUtil = new DBUtil();
		con = dbUtil.getConnection();
		ResultSet rs;

		try {
			int rowcount = dtm5.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm5.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm5.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql2);
			String[] data = new String[3];
			while (rs.next()) {
				for (int j = 1; j <= 3; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm5.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {
		}
	}

	private void databaseSearch2(String sql1, int i) {
		// TODO Auto-generated method stub
		DBUtil dbUtil = new DBUtil();

		con = dbUtil.getConnection();
		ResultSet rs;

		try {
			int rowcount = dtm4.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm4.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm4.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql1);
			String[] data = new String[5];
			while (rs.next()) {
				for (int j = 1; j <= 5; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm4.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {
		}
	}

	private void databaseSearch1(String sql, int i) {
		// TODO Auto-generated method stub

		DBUtil dbUtil = new DBUtil();

		con = dbUtil.getConnection();
		ResultSet rs;

		try {
			int rowcount = dtm2.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm2.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm2.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			String[] data = new String[5];
			while (rs.next()) {
				for (int j = 1; j <= 5; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm2.addRow(data); // 在Jtabl

			}

			con.close();
			// 設(shè)置表格隔行背景色(隔行背景色不同)
		} catch (Exception err) {
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == button2) {
			try {
				String s = (String) box2.getSelectedItem();
				int i = Integer.valueOf(s).intValue();
				DBUtil dbUtil = new DBUtil();
				Connection con;
				con = dbUtil.getConnection();
				int row = table1.getSelectedRow();
				String sql = "INSERT INTO DrugTable(PeNo,PeName,PePrice,PeNumber,PeUnit,PeClass,MrId)VALUES(?,?,?,?,?,?,?)";
				PreparedStatement parepare = con.prepareStatement(sql);
				parepare.setString(1, (String) table1.getValueAt(row, 0));
				parepare.setString(2, (String) table1.getValueAt(row, 1));
				parepare.setString(3, (String) table1.getValueAt(row, 2));
				parepare.setString(4, (String) table1.getValueAt(row, 3));
				parepare.setString(5, (String) table1.getValueAt(row, 4));
				parepare.setString(6, (String) table1.getValueAt(row, 5));
				parepare.setString(7, (String) table1.getValueAt(row, 6));

				String w = (String) table1.getValueAt(row, 3);
				String w1 = (String) table1.getValueAt(row, 1);
				if (table1.getValueAt(row, 0).equals("")) {
					JOptionPane.showMessageDialog(null, "請輸入完整信息", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				} else if (w1 == "") {
					JOptionPane.showMessageDialog(null, "請輸入藥物名稱", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				} else if (i <= 0 || w == null) {
					JOptionPane.showMessageDialog(null, "數(shù)量不能為空或者小于0", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				} else {
					parepare.executeUpdate();
					JOptionPane.showMessageDialog(null, "錄入成功", "錄入成功", JOptionPane.INFORMATION_MESSAGE);
					button1.setEnabled(true);
				}
			} catch (Exception et) {
				et.printStackTrace();
			}
		}

		else if (e.getSource() == button6) {
			try {
				String s = (String) box5.getSelectedItem();
				int i = Integer.valueOf(s).intValue();
				DBUtil dbUtil = new DBUtil();
				Connection con;

				con = dbUtil.getConnection();

				int row = table3.getSelectedRow();
				String sql = "INSERT INTO DrugTable(PeNo,PeName,PePrice,PeNumber,PeUnit,PeClass,MrId)VALUES(?,?,?,?,?,?,?)";
				PreparedStatement parepare = con.prepareStatement(sql);
				parepare.setString(1, (String) table3.getValueAt(row, 0));
				parepare.setString(2, (String) table3.getValueAt(row, 1));
				parepare.setString(3, (String) table3.getValueAt(row, 2));
				parepare.setString(4, (String) table3.getValueAt(row, 3));
				parepare.setString(5, (String) table3.getValueAt(row, 4));
				parepare.setString(6, (String) table3.getValueAt(row, 5));
				parepare.setString(7, (String) table3.getValueAt(row, 6));

				String w = (String) table3.getValueAt(row, 3);
				String w1 = (String) table3.getValueAt(row, 1);
				if (w1 == "") {
					JOptionPane.showMessageDialog(null, "請輸入藥物名稱", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				} else if (i <= 0 || w == null) {
					JOptionPane.showMessageDialog(null, "數(shù)量不能為空或者小于0", "錯誤", JOptionPane.INFORMATION_MESSAGE);

				} else {
					parepare.executeUpdate();
					JOptionPane.showMessageDialog(null, "錄入成功", "錄入成功", JOptionPane.INFORMATION_MESSAGE);
					button5.setEnabled(true);
				}
			} catch (Exception et) {
				et.printStackTrace();
			}
		}
	}

}

JIUYIModify.java

package com.sjsq;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class JIUYIModify extends JFrame implements ActionListener {
	private static final String KeyValue = null;
	public JLabel la0, la1, la2, la3, la4, la5, la6, la7, la8, la9;
	public JTextField t1, t2, t3, t4, t5, t6, t7, t8, t9;
	public JButton button;
	Font f2 = new Font("隸書", Font.BOLD, 25);
	public JComboBox jcbb2, comboBox, jcombobox1;
	public java.sql.Connection con = null;
	DBUtil dbUtil = new DBUtil();

	JIUYIModify() {
		JPanel panel2 = new JPanel();
		ImageIcon ic; // 按鈕圖片
		ic = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(ic);// 把背景圖片顯示在一個標簽里面

		panel2.setLayout(null);
		button = new JButton("保存");
		la0 = new JLabel("修改就醫(yī)檔案");
		la1 = new JLabel("就醫(yī)檔案編號:");
		la2 = new JLabel("病人編號 :");
		la3 = new JLabel("病人姓名 :");
		la4 = new JLabel("錄入時間 :");
		la5 = new JLabel("醫(yī)生編號 :");
		la6 = new JLabel("醫(yī)生姓名 :");
		la7 = new JLabel("就醫(yī)科室 :");
		la8 = new JLabel("病因 :");
		la9 = new JLabel("");

		t1 = new JTextField();
		t2 = new JTextField();
		t3 = new JTextField();
		t4 = new JTextField();
		t5 = new JTextField();
		t6 = new JTextField();
		t7 = new JTextField();
		t8 = new JTextField();

		t1.setBounds(90, 150, 100, 40);
		t1.setEditable(false);
		t2.setBounds(290, 150, 100, 40);
		t3.setBounds(90, 200, 100, 40);
		t4.setBounds(90, 250, 100, 40);
		t5.setBounds(290, 250, 80, 40);
		t6.setBounds(90, 300, 120, 40);
		t7.setBounds(290, 300, 120, 40);
		t8.setBounds(290, 200, 120, 40);

		t3.setEditable(false);
		t4.setEditable(false);
		t5.setEditable(false);
		t6.setEditable(false);
		t2.setEditable(false);

		la0.setBounds(10, 10, 200, 50);
		la0.setFont(f2);
		la1.setBounds(20, 150, 100, 50);
		la2.setBounds(220, 150, 100, 50);
		la3.setBounds(20, 200, 100, 50);
		la4.setBounds(220, 200, 100, 50);
		la5.setBounds(20, 250, 100, 50);
		la6.setBounds(220, 250, 100, 50);
		la7.setBounds(20, 300, 100, 50);
		la8.setBounds(220, 300, 100, 50);
		la9.setBounds(20, 350, 100, 50);

		Vector model1 = new Vector();
		String locationid1 = "";
		String locationname1 = "";
		String locationdept1 = "";
		try {

			con = dbUtil.getConnection();
			Statement st = con.createStatement();
			String sql = "select DrId, DrName,DeptName from Doctor";
			ResultSet rss = st.executeQuery(sql);
			while (rss.next()) {
				locationid1 = rss.getString("DrName");
				locationname1 = rss.getString("DrId");
				locationdept1 = rss.getString("DeptName");
				KeyValue Itemlocation1 = new KeyValue(locationname1, locationid1, locationdept1);
				model1.addElement(Itemlocation1);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		jcbb2 = new JComboBox(model1);
		jcbb2.addActionListener(this);

		jcbb2.setBounds(290, 250, 100, 40);

		panel2.add(jcbb2);
		panel2.add(button);
		button.setBounds(350, 400, 80, 50);
		button.addActionListener(this);
		label.setBounds(0, 0, 500, 500);

		panel2.add(la0);
		panel2.add(la1);
		panel2.add(la2);
		panel2.add(la3);
		panel2.add(la4);
		panel2.add(la5);
		panel2.add(la6);
		panel2.add(la7);
		panel2.add(la8);
		panel2.add(la9);
		panel2.add(t1);
		panel2.add(t2);
		panel2.add(t3);
		panel2.add(t4);
		panel2.add(t5);
		panel2.add(t6);
		panel2.add(t7);
		panel2.add(t8);
		panel2.add(label);
		this.add(panel2);
		this.setSize(450, 500); // 設(shè)置窗口大小
		this.setResizable(false); // 設(shè)置不可調(diào)整窗口大小
		this.setLocationRelativeTo(null);
		this.setVisible(true);

	}

	public void openDialog(JTable table, DefaultTableModel dtm) {
		// TODO Auto-generated method stub
		int rown;
		int coln;
		rown = table.getSelectedRow();
		coln = table.getSelectedColumn();
		String value0 = (String) table.getValueAt(rown, 0);
		String value1 = (String) table.getValueAt(rown, 1);
		String value2 = (String) table.getValueAt(rown, 2);
		String value3 = (String) table.getValueAt(rown, 3);
		String value4 = (String) table.getValueAt(rown, 4);
		String value5 = (String) table.getValueAt(rown, 5);
		String value6 = (String) table.getValueAt(rown, 6);
		String value7 = (String) table.getValueAt(rown, 7);

		t1.setText(value0);
		t2.setText(value1);
		t3.setText(value2);
		t4.setText(value4);
		t5.setText(value5);
		t6.setText(value6);
		t7.setText(value7);
		t8.setText(value3);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object aa = jcbb2.getSelectedItem();
				String se = aa.toString();
				Connection con;

				con = dbUtil.getConnection();
				Statement stmt;
				String sql = "update  Medical_records set PaId='" + t2.getText().trim() + "',PaName='"
						+ t3.getText().trim() + "',MrDate='" + t8.getText().trim() + "',DrId='" + t4.getText().trim()
						+ "',DrName='" + jcbb2.getSelectedItem().toString() + "',DeptName='" + t6.getText().trim()
						+ "',SickCause='" + t7.getText().trim() + "' where MrId='" + t1.getText().trim() + "'";
				try {
					stmt = con.createStatement();
					stmt.executeUpdate(sql);
					JOptionPane.showMessageDialog(null, "修改成功", "修改成功", JOptionPane.INFORMATION_MESSAGE, null);
					int row2 = table.getSelectedRow();
					String[] xiugai = new String[] { t1.getText(), t2.getText(), t3.getText(), t8.getText(),
							t4.getText(), jcbb2.getSelectedItem().toString(), t6.getText(), t7.getText() };

					dtm.removeRow(row2);
					dtm.insertRow(row2, xiugai);

					dtm.fireTableDataChanged();

					dispose();

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (jcbb2 == e.getSource()) {
			KeyValue item1 = (KeyValue) jcbb2.getSelectedItem();
			String a = item1.getId();
			String b = item1.getdept();
			t6.setText(b);
			t4.setText(a);

		}

	}

	class KeyValue {
		public String id;
		public String name;
		public String dept;

		public KeyValue(String id, String name, String dept) {
			this.id = id;
			this.name = name;
			this.dept = dept;
		}

		public KeyValue(Object selectedItem) {

			// TODO Auto-generated constructor stub
		}

		public String getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getdept() {
			return dept;
		}

		public String toString() {
			return name;
		}
	}

}

JIUYIModifyQuery1.java

package com.sjsq;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class JIUYIModifyQuery1 extends JFrame implements ActionListener {

	public JLabel la1, la2, la3, la4, la5, la6, la7, la8, la9;
	public JTextField tx1, tx2, tx3, tx4, tx5, tx6, tx7;
	public static DefaultTableModel dtm;
	Font f2 = new Font("隸書", Font.BOLD, 30);
	Font f1 = new Font("草書", Font.CENTER_BASELINE, 30);
	public static JTable table;
	String o;
	private JComboBox jcbb1;
	private JScrollPane JScrollPane = new JScrollPane();
	public JPanel panel = new JPanel();
	JButton button1 = new JButton("查詢");
	JButton button2 = new JButton("修改");
	JButton button3 = new JButton("刪除");
	JButton button4 = new JButton("診療明細操作");
	JButton button5 = new JButton("用藥明細操作");
	DBUtil dbUtil = new DBUtil();

	JIUYIModifyQuery1(String Stitle) {
		super(Stitle);
		panel.setLayout(null);
		this.add(panel);
		ImageIcon ic; // 按鈕圖片
		ic = new ImageIcon("picture/right_bg.jpg");
		JLabel label = new JLabel(ic);// 把背景圖片顯示在一個標簽里面

		panel.add(label);
		String columnNames[] = { "就醫(yī)檔案編號", "病人編號", "病人姓名", "就醫(yī)檔案錄入時間", "醫(yī)生編號", "醫(yī)生姓名", "所屬科室", "病因" };
		dtm = new DefaultTableModel(columnNames, 0) {
			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return false;
			}
		};

		table = new JTable(dtm);
		JScrollPane.setViewportView(table);
		JScrollPane.setBounds(new Rectangle(40, 60, 870, 250));

		String SQL = "select * from Medical_records";
		databaseSearch(SQL, 8);

		//
		la5 = new JLabel("病例編號:");
		la6 = new JLabel("病人姓名:");
		la7 = new JLabel("就醫(yī)檔案綜合操作");
		la8 = new JLabel("病人人數(shù)統(tǒng)計:");
		la9 = new JLabel("人");
		la7.setFont(f2);
		la8.setFont(f1);
		la9.setFont(f1);
		tx5 = new JTextField();
		tx6 = new JTextField();
		tx7 = new JTextField();
		tx7.setFont(f1);

		button2.setBounds(400, 400, 100, 50);
		button3.setBounds(600, 400, 100, 50);
		button4.setBounds(910, 60, 100, 100);
		button5.setBounds(910, 200, 100, 100);

		la7.setBounds(40, 0, 300, 50);
		la8.setBounds(800, 350, 250, 50);
		la9.setBounds(870, 395, 50, 50);
		tx7.setBounds(800, 400, 60, 35);

		button4.addActionListener(this);
		button5.addActionListener(this);
		button2.addActionListener(this);
		panel.add(button4);
		panel.add(button3);
		panel.add(button2);
		panel.add(button5);
		panel.add(JScrollPane);
		panel.add(la7);
		panel.add(tx7);
		panel.add(la8);
		panel.add(la9);
		tx7.setEditable(false);
		// 設(shè)置表格隔行顏色
		setbgcolor();

		final JPanel panel1 = new JPanel();
		panel1.setLayout(null);
		panel1.setBackground(Color.YELLOW);
		panel1.setBorder(new TitledBorder(null, "查詢操作", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		panel1.setBounds(50, 350, 300, 150);
		panel.add(panel1);
		button1.setBounds(220, 80, 60, 40);
		panel1.add(button1);
		button1.setBackground(Color.white);
		la5.setBounds(20, 73, 80, 50);
		la6.setBounds(20, 73, 80, 50);
		la5.setVisible(false);
		tx6.setVisible(false);
		la6.setVisible(false);
		tx5.setVisible(false);
		panel1.add(la5);
		panel1.add(la6);
		tx5.setBounds(90, 80, 120, 35);
		panel1.add(tx5);
		tx6.setBounds(90, 80, 120, 35);
		panel1.add(tx6);

		Connection con;
		con = dbUtil.getConnection();
		ResultSet rs;
		try {
			String sql1 = "select Count(*) from Medical_records";
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(sql1);
			while (rs.next()) {
				o = rs.getString(1);

			}
			tx7.setText(o);

		} catch (Exception r) {
			r.printStackTrace();
		}

		jcbb1 = new JComboBox();
		jcbb1.addActionListener(new ActionListener() {// 下拉框添加事件,用于顯示相應(yīng)的查詢文本框,以及標簽
			public void actionPerformed(final ActionEvent e) {
				Object SelectName = jcbb1.getSelectedItem();
				String selectNamecontent = SelectName.toString();

				if (selectNamecontent.equals("請選擇查詢種類")) {
					la5.setVisible(false);
					la6.setVisible(false);
					tx5.setVisible(false);
					tx6.setVisible(false);
					System.out.println("什么也不做");
					return;
				} else if (selectNamecontent.equals("病例編號")) {
					la5.setVisible(true);
					tx5.setVisible(true);
					tx5.setText("");
					la6.setVisible(false);
					tx6.setVisible(false);
					return;
				} else if (selectNamecontent.equals("病人姓名")) {
					tx5.setVisible(false);
					la5.setVisible(false);
					tx6.setVisible(true);
					la6.setVisible(true);
					tx6.setText("");
					return;
				}

				// ++++++++++++++++++++++++++++++++++++++++++++++++++
			}
		});

		jcbb1.setModel(new DefaultComboBoxModel(new String[] { "請選擇查詢種類", "病例編號", "病人姓名" }));
		jcbb1.setBounds(20, 28, 135, 25);
		panel1.add(jcbb1);

		// 添加背景
		panel.add(label);
		label.setBounds(0, 0, 1100, 700);

		button3.addMouseListener(new MouseAdapter() { // 刪除按鈕實現(xiàn)刪除記錄的功能
			public void mouseClicked(MouseEvent e) {
				int row = table.getSelectedRow();// 這句選擇要刪除的行
				Connection con;
				con = dbUtil.getConnection();
				Statement stmt;
				String val = (String) table.getValueAt(row, 0);
				String sql = "delete from Medical_records where MrId='" + val + "'";
				try {
					stmt = con.createStatement();
					stmt.executeUpdate(sql);
					JOptionPane.showMessageDialog(null, "  刪除成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (row != -1) { // 這句判斷是否有選中的行
					dtm.removeRow(row);
				} // 這句刪除指定行
			}
		});

		table.addMouseListener(new MouseAdapter() {// 設(shè)置TABLE雙擊鼠標事件
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) // 單擊鼠標左鍵
					if (e.getClickCount() == 2) {
						JIUYIModify c = new JIUYIModify();
						c.openDialog(table, dtm);

					}
			}
		});
		button1.addMouseListener(new MouseAdapter() {// 查詢按鈕添加鼠標事件,對應(yīng)相應(yīng)的查詢功能
			public void mouseClicked(final MouseEvent e) {
				System.out.println("Good idea!!!");
				Object SelectName = jcbb1.getSelectedItem();
				String selectNamecontent = SelectName.toString();
				if (selectNamecontent.equals("請選擇查詢種類")) {
					System.out.println("什么也不做");
					return;
				} else if (selectNamecontent.equals("病例編號")) {
					if (tx5.getText().equals("")) {
						JOptionPane.showMessageDialog(null, "  請輸入病例編號!", "注意", JOptionPane.ERROR_MESSAGE);
						return;
					}
					String sickName = tx5.getText().trim();
					String SQL = "select * from Medical_records where MrId like '%" + sickName + "%'";
					databaseSearch(SQL, 8);

					return;
				}

				else if (selectNamecontent.equals("病人姓名")) {
					if (tx6.getText().equals("")) {
						JOptionPane.showMessageDialog(null, "  請輸入病人姓名!", "注意", JOptionPane.ERROR_MESSAGE);
						return;
					}
					String sickCaseID = tx6.getText().trim();

					String SQL = "select * from Medical_records where PaName like '%" + sickCaseID + "%'";
					databaseSearch(SQL, 8);
					return;
				}
			}

		});

	}

	private void setbgcolor() {
		// TODO Auto-generated method stub
		// 設(shè)置表格隔行背景顏色不同

		try {
			DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
				public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
						boolean hasFocus, int row, int column) {
					if (row % 2 == 0)
						setBackground(new Color(223, 220, 239)); // 設(shè)置奇數(shù)行底色
					else if (row % 2 == 1)
						setBackground(Color.white); // 設(shè)置偶數(shù)行底色
					return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				}
			};
			for (int i = 0; i < table.getColumnCount(); i++) {
				table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private void databaseSearch(String SQL, int i) {
		// TODO Auto-generated method stub
		Connection con;

		con = dbUtil.getConnection();
		ResultSet rs;

		try {
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			int rowcount = dtm.getRowCount() - 1;
			if (rowcount != -1) {
				for (int i1 = rowcount; i1 >= 0; i1--) {
					dtm.removeRow(i1); // 刪除Jtable中的所有行
				}
				dtm.setRowCount(0); // 將Jtable中的行數(shù)設(shè)為零
			}
			Statement stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);
			String[] data = new String[8];
			while (rs.next()) {
				for (int j = 1; j <= 8; j++) {
					data[j - 1] = rs.getString(j); // 取出數(shù)據(jù)庫中的數(shù)組裝載到數(shù)組中
				}
				dtm.addRow(data); // 在Jtabl
			}

			con.close();

		} catch (Exception err) {
			String error = err.getMessage();
			JOptionPane.showMessageDialog(null, error);
			err.printStackTrace();
		} finally {
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == button2) {
			JIUYIModify t = new JIUYIModify();
			t.openDialog(table, dtm);

		} else if (e.getSource() == button4) {
			ChufangModify y = new ChufangModify("診療明細");

			int row = table.getSelectedRow();
			String r = (String) table.getValueAt(row, 0);
			String SQL1 = "select * from DrugTable where MrId='" + r + "'and PeClass='其他類'";
			y.databaseSearch2(SQL1, 6);
			y.addrow(table);
		} else if (e.getSource() == button5) {
			ProjectModify y = new ProjectModify("用藥明細");

			int row = table.getSelectedRow();
			String r = (String) table.getValueAt(row, 0);

			String SQL1 = "select * from DrugTable where MrId='" + r + "' and PeClass='藥品類'or MrId='" + r
					+ "'and PeClass='診斷類'";
			y.databaseSearch2(SQL1, 7);
			y.addrow(table);
		}
	}

}

MainPanelCashier.java

package com.sjsq;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MainPanelCashier extends JFrame implements MouseListener {
	// 基本組件
	public CardLayout layout = new CardLayout(); // 卡片布局方式
	JPanel top, left, bottom, right;
	JLabel head;
	Font f1 = new Font("隸書", Font.BOLD, 54);
	Font f2 = new Font("隸書", Font.BOLD, 25);
	// 菜單欄組件
	private JPanel pNorth, pSouth, subMenuContainer;
	private JButton item1, item2, item3, item4, item5, item6, item7, item8, htn[], gtn[], btn[], ctn[], dtn[], etn[],
			ftn[];
	// 時間組件
	private JPanel timePanel;
	private JLabel displayArea;
	private String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; // 設(shè)置時間格式
	private String time;
	private int ONE_SECOND = 1000; // 設(shè)置時間間隔
	JLabel label1 = new JLabel("歡迎你:");

	public MainPanelCashier(String title) {

		super("醫(yī)院管理系統(tǒng)(收費員界面)");

		String aa = Login.username; // 獲取登錄的用戶名,放在標簽中,顯示在上面板。
		JLabel label8 = new JLabel(aa);

		// 窗口圖標設(shè)置
		Toolkit tk = getToolkit();
		Image icon = tk.getImage("picture/icon.jpg");
		setIconImage(icon);

		// 時間控件設(shè)置
		timePanel = new JPanel();
		displayArea = new JLabel();
		configTimeArea();
		timePanel.add(displayArea);
		timePanel.setBounds(1000, 0, 200, 50);
		timePanel.setOpaque(false); // 時間面板設(shè)置為透明

		// 組件基本設(shè)置
		left = new JPanel();
		left.setBounds(5, 150, 150, 515);
		this.add(left);

		right = new JPanel();
		right.setLayout(layout);
		right.setBounds(155, 155, 1045, 510);
		HomePage s = new HomePage();
		right.add(s.homePage);
		this.add(right);

		bottom = new JPanel();
		bottom.setLayout(null);
		bottom.setBounds(0, 666, 1200, 35);
		bottom.setBackground(Color.WHITE);
		bottom.add(timePanel); // 添加時間控件
		this.add(bottom);

		top = new JPanel(); // 設(shè)置背景圖片"picture/top_bg.jpg"
		ImageIcon background = new ImageIcon("picture/top_bg.jpg");
		JLabel label = new JLabel(background);
		top.setLayout(null);
		label.setBounds(0, 0, 1200, 145);
		top.setBounds(0, 0, 1200, 145);
		this.add(top);
		top.add(label8);
		label8.setBounds(1080, 90, 100, 50);
		label8.setFont(f2);

		head = new JLabel("醫(yī)院信息管理系統(tǒng)");
		head.setFont(f1);
		head.setBounds(375, 40, 500, 75);
		top.add(head);

		top.add(label1);
		label1.setBounds(980, 90, 200, 50);
		label1.setFont(f2);
		top.add(label);

		// Left面板折疊式菜單設(shè)置,三面板網(wǎng)格式布局
		pNorth = new JPanel();
		pNorth.setLayout(new GridLayout(4, 1));
		pSouth = new JPanel();
		subMenuContainer = new JPanel();

		subMenuContainer.setLayout(new GridLayout(4, 1));

		item1 = new JButton("首頁"); // 設(shè)置按鈕

		item6 = new JButton("價格管理");
		item7 = new JButton("收費管理");
		item8 = new JButton("系統(tǒng)設(shè)置");

		item1.setPreferredSize(new Dimension(150, 47)); // 優(yōu)先設(shè)置按鈕大小

		item6.setPreferredSize(new Dimension(150, 47));
		item7.setPreferredSize(new Dimension(150, 47));
		item7.setPreferredSize(new Dimension(150, 47));
		item8.setPreferredSize(new Dimension(150, 47));

		item1.setContentAreaFilled(false); // 設(shè)置為透明
		item6.setContentAreaFilled(false);
		item7.setContentAreaFilled(false);
		item8.setContentAreaFilled(false);

		pNorth.add(item1);
		pNorth.add(item6);
		pNorth.add(item7);
		pNorth.add(item8);

		ftn = new JButton[1];
		ftn[0] = new JButton("綜合操作");
		for (int i = 0; i < ftn.length; i++) {
			ftn[i].setBackground(Color.WHITE);
			ftn[i].setPreferredSize(new Dimension(150, 30));
			ftn[i].addMouseListener(this);
		}
		gtn = new JButton[2];
		gtn[0] = new JButton("結(jié)賬頁面");
		gtn[1] = new JButton("綜合操作");
		for (int i = 0; i < gtn.length; i++) {
			gtn[i].setBackground(Color.WHITE);
			gtn[i].setPreferredSize(new Dimension(150, 30));
			gtn[i].addMouseListener(this);
		}
		htn = new JButton[1];
		htn[0] = new JButton("修改密碼");

		for (int i = 0; i < htn.length; i++) {
			htn[i].setBackground(Color.WHITE);
			htn[i].setPreferredSize(new Dimension(150, 30));
			htn[i].addMouseListener(this);
		}

		left.add(pNorth, "North"); // 按鈕添加到left面板中
		left.add(subMenuContainer, "Center");
		left.add(pSouth, "South");

		// 監(jiān)聽器添加
		item1.addMouseListener(this);

		item6.addMouseListener(this);
		item7.addMouseListener(this);
		item8.addMouseListener(this);

		// 窗體設(shè)置
		this.setLayout(null);
		this.setSize(1200, 730);
		this.setLocationRelativeTo(null); // 窗口居中顯示
		this.setVisible(true);
		this.setResizable(false); // 窗體不可改變大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public static void main(String[] args) {
		MainPanelCashier mainPanelCashier = new MainPanelCashier("醫(yī)院信息管理系統(tǒng)");
	}

	// 時間控件方法
	private void configTimeArea() {
		Timer tmr = new Timer();
		tmr.scheduleAtFixedRate(new JLabelTimerTask(), new Date(), ONE_SECOND);
	}

	protected class JLabelTimerTask extends TimerTask {
		SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

		public void run() {
			time = dateFormatter.format(Calendar.getInstance().getTime());
			displayArea.setText(time);
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == item1) {
			this.add(right);
			HomePage s = new HomePage(); // 調(diào)用主頁的類。
			right.add(s.homePage, "homePage");
			layout.next(right);
		}
		// 設(shè)置折疊按鈕功能,先把南面板清空,讓后在給各面板設(shè)置布局方式,添加按鈕也添加子功能按鈕,清楚別的子功能按鈕

		if (e.getSource() == item6) {
			pSouth.removeAll();
			pNorth.setLayout(new GridLayout(2, 1));
			pSouth.setLayout(new GridLayout(2, 1));
			pNorth.add(item1);

			pNorth.add(item6);
			pSouth.add(item7);
			pSouth.add(item8);

			for (int i = 0; i < ftn.length; i++)
				subMenuContainer.add(ftn[i]);
			for (int i = 0; i < gtn.length; i++)
				subMenuContainer.remove(gtn[i]);
			for (int i = 0; i < htn.length; i++)
				subMenuContainer.remove(htn[i]);
			validate();
			getContentPane().repaint();
		}
		if (e.getSource() == item7) {
			pSouth.removeAll();
			pNorth.setLayout(new GridLayout(3, 1));
			pSouth.setLayout(new GridLayout(1, 1));
			pNorth.add(item1);

			pNorth.add(item6);
			pNorth.add(item7);
			pSouth.add(item8);

			for (int i = 0; i < ftn.length; i++)
				subMenuContainer.remove(ftn[i]);
			for (int i = 0; i < gtn.length; i++)
				subMenuContainer.add(gtn[i]);
			for (int i = 0; i < htn.length; i++)
				subMenuContainer.remove(htn[i]);
			validate();
			getContentPane().repaint();
		}
		if (e.getSource() == item8) {
			pSouth.removeAll();
			pNorth.setLayout(new GridLayout(4, 1));

			pNorth.add(item1);

			pNorth.add(item6);
			pNorth.add(item7);
			pNorth.add(item8);

			for (int i = 0; i < ftn.length; i++)
				subMenuContainer.remove(ftn[i]);
			for (int i = 0; i < gtn.length; i++)
				subMenuContainer.remove(gtn[i]);
			for (int i = 0; i < htn.length; i++)
				subMenuContainer.add(htn[i]);
			validate();
			getContentPane().repaint();
		}

		else if (e.getSource() == ftn[0]) {
			this.add(right);
			PriceManage s = new PriceManage();
			s.save.setEnabled(false);
			s.modify.setEnabled(false);
			s.delete.setEnabled(false);
			right.add(s.priceManage, "priceManage");
			layout.next(right);
		} else if (e.getSource() == gtn[0]) {

			this.add(right);
			Charge t = new Charge();

			right.add(t.panel2, "第一0個面板");
			layout.next(right);
		} else if (e.getSource() == gtn[1]) {

			this.add(right);
			ChargeQuery t = new ChargeQuery();

			right.add(t.panel2, "第一0個面板");
			layout.next(right);
		} else if (e.getSource() == htn[0]) {

			this.add(right);
			PassWordModifyCashier t = new PassWordModifyCashier("密碼修改");

			right.add(t.panel2, "第一0個面板");
			layout.next(right);

		}
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

}

到此這篇關(guān)于Java+Swing實現(xiàn)醫(yī)院管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Java Swing醫(yī)院管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis 一對多嵌套查詢的實現(xiàn)

    mybatis 一對多嵌套查詢的實現(xiàn)

    本文主要介紹了mybatis 一對多嵌套查詢的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • Java實現(xiàn)國產(chǎn)加密算法SM4的示例詳解

    Java實現(xiàn)國產(chǎn)加密算法SM4的示例詳解

    這篇文章主要為大家詳細介紹了Java如何實現(xiàn)國產(chǎn)加密算法SM4(ECB和CBC兩種模式),文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-01-01
  • Java應(yīng)用層協(xié)議WebSocket實現(xiàn)消息推送

    Java應(yīng)用層協(xié)議WebSocket實現(xiàn)消息推送

    后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程,需要的朋友可以參考下
    2023-02-02
  • 教你怎么實現(xiàn)java語言的在線編譯

    教你怎么實現(xiàn)java語言的在線編譯

    這篇文章主要介紹了教你怎么實現(xiàn)java語言的在線編譯,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 值得收藏的SpringBoot 實用的小技巧

    值得收藏的SpringBoot 實用的小技巧

    最近分享的一些源碼、框架設(shè)計的東西。我發(fā)現(xiàn)大家熱情不是特別高,想想大多數(shù)應(yīng)該還是正兒八經(jīng)寫代碼的居多;這次就分享一點接地氣的: SpringBoot 使用中的一些小技巧 ,需要的朋友可以參考下
    2018-10-10
  • springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié)

    springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié)

    這篇文章主要介紹了springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié),本文給出了解決方案,需要的朋友可以參考下
    2018-11-11
  • Java如何讀取jar包中的resource資源文件

    Java如何讀取jar包中的resource資源文件

    這篇文章主要介紹了Java如何讀取jar包中的resource資源文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • SpringBoot手寫自定義starter源碼

    SpringBoot手寫自定義starter源碼

    這篇文章主要介紹了SpringBoot手寫自定義starter源碼,SpringBoot擁有很多方便使用的starter,比如spring-boot-starter-log4j、mybatis-spring-boot-starter.jar等,各自都代表了一個相對完整的功能模塊,需要的朋友可以參考下
    2023-10-10
  • Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行

    Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行

    本文主要介紹了Springboot項目啟動成功后可通過五種方式繼續(xù)執(zhí)行,主要包括CommandLineRunner接口,ApplicationRunner接口,ApplicationListener接口,@PostConstruct注解,InitalizingBean接口,感興趣的可以了解一下
    2023-12-12
  • Java異常--常見方法--自定義異常--增強try(try-with-resources)詳解

    Java異常--常見方法--自定義異常--增強try(try-with-resources)詳解

    這篇文章主要介紹了Java異常--常見方法--自定義異常--增強try(try-with-resources)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03

最新評論