Java+Swing实现医院管理系统的完整代码
本系统实现的以下功能管理员功能:登录系统、病人信息的增删改查、就医档案的录入、医生信息的增删改查、科室信息的增删改查、收费统计功能、修改密码。医生功能:登录系统、病人信息的增删改查、医生信息的增删改查、科室信息的增删改查、收费统计功能、修改密码。收费员功能:价格管理、收费管理、修改密码。JDK版本:1.8数据库:Mysql8.0.13
数据库用到的表cashierchargedepartmentdoctordrugtablemanagermedical_recordspatientprice
工程截图
二、系统展示1.登录页
2.主页面
3.病人信息录入
4.病人信息操作
5.就医档案录入
6.处方单录入
7.就医档案操作
8.医生信息录入
9.医生信息操作
10.科室信息录入
11.科室信息操作
12.收费操作
13.收费统计
14.修改密码
15.医生主页面
16.收费员主页面
三、系统实现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;// 输入的用户Idpublic 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:医生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('医生');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);// 登录监听loginJButton.addActionListener(new ActionListener() {public boolean flag = false;public void actionPerformed(ActionEvent e) {// 医生if (r3.isSelected()) {try {String usernameText = usernameJTextField.getText().toString(); // 获取帐号文本框内容String passwordText = passwordJPasswordField.getText().toString(); // 获取密码文本框内容Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery('select * from doctor'); // 执行SQL语句,返回结果集while (rs.next()) {userId = rs.getString('DrId'); // 获取登录的用户编号,username = rs.getString('DrName');// 获取登录的用户姓名password = rs.getString('Password'); // 获取数据库中的数据项的密码if (userId.equals(usernameText) && password.equals(passwordText)) {// 判断数据库的用户编号以及密码是否与文本框的值相同loginFlag = 1;break;}}if (loginFlag == 1) {JOptionPane.showMessageDialog(null, '登录成功');// 显示系统主界面MainPanelDoctor a = new MainPanelDoctor('医生界面'); a.setVisible(true);Login.this.setVisible(false);// 关闭登录按钮} else {usernameJTextField.setText(''); // 错误的话则文本框内容设置为空,显示错误标签passwordJPasswordField.setText('');JOptionPane.showMessageDialog(null, '登陆错误');}} catch (SQLException e2) {System.out.println(e2);}}// 管理员else if (r1.isSelected()) {try {String usernameText = usernameJTextField.getText().toString(); // 获取帐号文本框内容String passwordText = passwordJPasswordField.getText().toString(); // 获取密码文本框内容Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery('select * from manager'); // 执行SQL语句,返回结果集while (rs.next()) {userId = rs.getString('ManagerID'); // 获取登录的用户编号,username = rs.getString('ManagerName');// 获取登录的用户姓名password = rs.getString('MaPassWord'); // 获取数据库中的数据项的密码if (userId.equals(usernameText) && password.equals(passwordText)) {// 判断数据库的用户编号以及密码是否与文本框的值相同loginFlag = 1;break;}}// 登录成功if (loginFlag == 1) {JOptionPane.showMessageDialog(null, '登录成功');new MainPanelManager('管理员界面'); // 显示系统主界面Login.this.setVisible(false);// 关闭登录按钮// 登录失败} else {usernameJTextField.setText(''); // 错误的话则文本框内容设置为空,显示错误标签passwordJPasswordField.setText('');JOptionPane.showMessageDialog(null, '登陆错误');}} catch (SQLException e3) {System.out.println(e3);}}// 收费员else if (r2.isSelected()) {try {String usernameText = usernameJTextField.getText().toString(); // 获取帐号文本框内容String passwordText = passwordJPasswordField.getText().toString(); // 获取密码文本框内容Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery('select * from cashier'); // 执行SQL语句,返回结果集while (rs.next()) {userId = rs.getString('cashierId'); // 获取登录的用户编号,username = rs.getString('cashierName');// 获取登录的用户姓名password = rs.getString('cashierPassWord'); // 获取数据库中的数据项的密码if (userId.equals(usernameText) && password.equals(passwordText)) {// 判断数据库的用户编号以及密码是否与文本框的值相同loginFlag = 1;break;}}if (loginFlag == 1) {JOptionPane.showMessageDialog(null, '登录成功');new MainPanelCashier('收费员页面'); // 显示系统主界面Login.this.setVisible(false);// 关闭登录按钮} else {usernameJTextField.setText(''); // 错误的话则文本框内容设置为空,显示错误标签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); // 设置窗口大小this.setResizable(false); // 设置不可调整窗口大小this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String args[]) {Login login = new Login('医院管理系统');}}
HomePage.java
HomePage.javapackage 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('医院信息管理系统');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[] = { '就医档案编号', '病人编号', '病人姓名', '就医科室' };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('确定结算');btn2 = new JButton('撤销结算');la0 = new JLabel('费用总计 :');la1 = new JLabel('就医档案编号 :');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('结账日期: ');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);// 设置文本框的边缘不显示tx0.setBorder(null);tx2.setBorder(null);tx3.setBorder(null);tx4.setBorder(null);tx14.setBorder(null);tx14.setEditable(false);// 设置一个新的面板final JPanel panel1 = new JPanel();JPanel panel12 = new JPanel();JPanel panel13 = new JPanel();panel13.setBackground(Color.pink);panel12.setBackground(Color.pink);panel1.setLayout(null);// 设置空布局panel1.setBorder(new TitledBorder(null, '收费结算', 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); // 设置按钮事件btn2.addActionListener(this);// 默认表格模版的设置,添加表头和设置表格不可编辑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);// 面板添加控件,设置位置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);// 设置文本框不可编辑tx0.setEditable(false);tx2.setEditable(false);tx3.setEditable(false);tx4.setEditable(false);tx11.setEditable(false);tx12.setEditable(false);tx13.setEditable(false);panel2.add(label);// 面板添加背景图片,设置位置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文本框添加事件,根据文本框内容的改变模糊查询到数据库内容,显示到表格中tx1.getDocument().addDocumentListener(new DocumentListener() {@Overridepublic void removeUpdate(DocumentEvent e) {updata_combobox();}@Overridepublic void insertUpdate(DocumentEvent e) {updata_combobox();}@Overridepublic void changedUpdate(DocumentEvent e) {updata_combobox();}private void updata_combobox() {String s1 = null;s1 = tx1.getText(); // 根据S1的内容模糊查询数据库对应的数据JScrollPane5.setVisible(true);String sql = 'select MrId,PaId,PaName,DeptName from Medical_records where MrId like ’%' + s1 + '%’';databaseSearch1(sql, 5);}});// 根据你选择表格的某一行内容,输入到对应的文本框内。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);// 连接数据库,查询对应的价格DBUtil dbUtil = new DBUtil();Connection con = dbUtil.getConnection();ResultSet rs;try {// 查询数据库中用药的费用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;// 设置为0,否则会应为再次输入而无法清楚原来的数值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 stubDBUtil 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中的行数设为零}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); // 取出数据库中的数组装载到数组中}dtm3.addRow(data); // 在Jtabl}con.close();// 设置表格隔行背景色(隔行背景色不同)} catch (Exception err) {}}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (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, '请输入结算的档案号', '错误', JOptionPane.INFORMATION_MESSAGE);} else {parepare.executeUpdate();JOptionPane.showMessageDialog(null, '结账成功,需要交(退)' + tx12.getText() + '', '结账成功',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 blocke1.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[] = { '就医档案编号', '病人编号', '病人姓名', '就医科室', '用药费用', '治疗费', '检查费', '挂号费', '处置费', '化验费','费用总额', '结账时间' };ChargeQuery() {// 添加背景ImageIcon background = new ImageIcon('picture/right_bg.jpg'); // 背景图片JLabel label = new JLabel(background);panel2.setLayout(null);// 设置默认表格面板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('收费统计');panel2.add(label1);label1.setBounds(30, 10, 400, 50);label1.setFont(f1);la1 = new JLabel('总费用统计:');la2 = new JLabel('人数统计:');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);// 面板添加背景图片,设置位置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 stubtry {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)); // 设置奇数行底色else if (row % 2 == 1)setBackground(Color.white); // 设置偶数行底色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 stubDBUtil 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中的行数设为零}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); // 取出数据库中的数组装载到数组中}dtm.addRow(data); // 在Jtable中添加数据行}con.close();// 设置表格隔行背景色(隔行背景色不同)} 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[] = { '编码', '名称', '单价', '数量', '计数单位', '类别', '档案编号' };private String columnNames1[] = { '编码', '名称', '单价', '计数单位', '类别' };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('数量');JTextField box3 = new JTextField();box2 = new JComboBox(fontSize1);box2.addActionListener(this);box2.addItemListener(this);box3.getDocument().addDocumentListener(new DocumentListener() {@Overridepublic void removeUpdate(DocumentEvent e) {System.out.println('removeUpdate');updata_combobox();}@Overridepublic void insertUpdate(DocumentEvent e) {System.out.println('insertUpdate');updata_combobox();}@Overridepublic 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() {// 设置TABLE双击鼠标事件public void mouseClicked(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON1) // 单击鼠标左键JScrollPane5.setVisible(true);}});button8.addMouseListener(new MouseAdapter() { // 删除按钮实现删除记录的功能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 blocke1.printStackTrace();}if (row != -1) { // 这句判断是否有选中的行dtm2.removeRow(row);} // 这句删除指定行}});table3.addMouseListener(new MouseAdapter() {// 设置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); // 设置窗口大小this.setResizable(false); // 设置不可调整窗口大小this.setLocationRelativeTo(null);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif (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, '数量不能小于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 stubDBUtil 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中的行数设为零}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); // 取出数据库中的数组装载到数组中}dtm3.addRow(data); // 在Jtabl}con.close();// 设置表格隔行背景色(隔行背景色不同)} catch (Exception err) {}}public void addrow(JTable table) {// TODO Auto-generated method stubint 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中的行数设为零}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); // 取出数据库中的数组装载到数组中}dtm2.addRow(data); // 在Jtabl}con.close();// 设置表格隔行背景色(隔行背景色不同)} catch (Exception err) {}}@Overridepublic 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;}// 关闭连接public void close() {try {if (con != null) {con.close();}con = null;System.out.println('数据库连接关闭');} 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[] = { '查询全部', '按科室编号查询', '按科室名称查询' };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() {// 背景设置departmentManage.setLayout(null);ImageIcon background = new ImageIcon('picture/right_bg.jpg');JLabel label = new JLabel(background);// 标题设置manageTitle.setFont(new Font('宋体', Font.BOLD, 50));manageTitle.setBounds(60, 10, 1000, 50);departmentManage.add(manageTitle);// 录入操作面板设置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);// 查询操纵面板设置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);// 表格设置defaultTableModel(); // 设置表格不可编辑setTableColumnCenter(); // 设置表格内容居中显示setbgcolor(); // 设置表格隔行不同颜色JScrollPane1.setBounds(new Rectangle(45, 230, 850, 270));JScrollPane1.setViewportView(table); // 创建一个滚动条(如果有必要)并设置其视图// table.getColumnModel().getColumn(0).setMinWidth(40);// table.getColumnModel().getColumn(0).setMaxWidth(40);table.getTableHeader().setReorderingAllowed(false); // 列不可拖动table.getTableHeader().setResizingAllowed(false); // 列宽不能改变departmentManage.add(JScrollPane1);// 按钮设置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);// 添加监听器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);}// 设置表格不可编辑private void defaultTableModel() {dtm = new DefaultTableModel(columnNames, 0);table = new JTable(dtm) {public boolean isCellEditable(int row, int column) {return false;}};}// 设置表格内容居中显示private void setTableColumnCenter() {DefaultTableCellRenderer r = new DefaultTableCellRenderer();r.setHorizontalAlignment(JLabel.CENTER);table.setDefaultRenderer(Object.class, r);}// 设置表格隔行背景颜色不同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)); // 设置奇数行底色else if (row % 2 == 1)setBackground(Color.white); // 设置偶数行底色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位数字else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {JOptionPane.showMessageDialog(null, '请输入正确的3位数科室编号!', '错误', JOptionPane.ERROR_MESSAGE);}// 名字长度不能超过20个字符else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {JOptionPane.showMessageDialog(null, '名字长度不能超过10个汉字!', '错误', JOptionPane.ERROR_MESSAGE);}// 电话号码为8位数字else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {JOptionPane.showMessageDialog(null, '请输入正确的8位数电话号码!', '错误', 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(); // 创建一个数据库会话对象Statement st2 = con.createStatement(); // 创建一个数据库会话对象Statement st3 = con.createStatement(); // 创建一个数据库会话对象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()) { // 判断结果集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 {// 信息添加到数据库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); // 在表格添加一行刚添加的数据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(); // 创建一个数据库会话对象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();}}// 修改操作设置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(); // 创建一个数据库会话对象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();}}}}// 把数据库数据传入表格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();}}// 科室信息修改操作对话框设置++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++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(); // 创建一个数据库会话对象// 输入信息不能为空if (tx2.getText().equals('') || tx4.getText().equals('')) {JOptionPane.showMessageDialog(null, '请输入完整的科室信息!', '错误', JOptionPane.ERROR_MESSAGE);}// 编号为3位数字else if (tx1.getText().length() > 3 || pattern.matcher(tx1.getText()).matches() == false) {JOptionPane.showMessageDialog(null, '请输入正确的3位数科室编号!', '错误', JOptionPane.ERROR_MESSAGE);}// 名字长度不能超过20个字符else if (tx2.getText().length() > 10 || tx3.getText().length() > 10) {JOptionPane.showMessageDialog(null, '名字长度不能超过10个汉字!', '错误', JOptionPane.ERROR_MESSAGE);}// 电话号码为8位数字else if (pattern.matcher(tx4.getText()).matches() == false || tx4.getText().length() != 8) {JOptionPane.showMessageDialog(null, '请输入正确的8位数电话号码!', '错误', 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[] = { '查询全部', '按科室编号查询', '按科室名称查询' };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() {// 背景设置departmentManage.setLayout(null);ImageIcon background = new ImageIcon('picture/right_bg.jpg');JLabel label = new JLabel(background);// 标题设置manageTitle.setFont(new Font('宋体', Font.BOLD, 50));manageTitle.setBounds(60, 10, 1000, 50);departmentManage.add(manageTitle);// 录入操作面板设置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);// 查询操纵面板设置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);// 表格设置defaultTableModel(); // 设置表格不可编辑setTableColumnCenter(); // 设置表格内容居中显示setbgcolor(); // 设置表格隔行不同颜色JScrollPane1.setBounds(new Rectangle(45, 230, 850, 270));JScrollPane1.setViewportView(table); // 创建一个滚动条(如果有必要)并设置其视图// table.getColumnModel().getColumn(0).setMinWidth(40);// table.getColumnModel().getColumn(0).setMaxWidth(40);table.getTableHeader().setReorderingAllowed(false); // 列不可拖动table.getTableHeader().setResizingAllowed(false); // 列宽不能改变departmentManage.add(JScrollPane1);// 按钮设置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);// 添加监听器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);}// 设置表格不可编辑private void defaultTableModel() {dtm = new DefaultTableModel(columnNames, 0);table = new JTable(dtm) {public boolean isCellEditable(int row, int column) {return false;}};}// 设置表格内容居中显示private void setTableColumnCenter() {DefaultTableCellRenderer r = new DefaultTableCellRenderer();r.setHorizontalAlignment(JLabel.CENTER);table.setDefaultRenderer(Object.class, r);}// 设置表格隔行背景颜色不同private static void setbgcolor() {try {DefaultTableCellRenderer tcr = new DefaultTa
相关文章: