该项目用javaswing+sqlserver+WindowBuilder插件完成!
本文只展示主界面和病人档案界面代码,另外三个界面的代码思路也是一样的,可以自己尝试敲一下
一.建立数据库
表1:病人档案(Patient)
表2:医生信息(Doctor)
表3:科室信息(Department)
表4:就医档案(Precord)
E-R图
二.连接数据库
import java.io.IOError;
import java.io.IOException;
import java.sql.*;
import java.util.*;
public class DButils {
public static Connection getconnection() {
Connection con=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=医院管理系统", "sa", "123456");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException d){
d.printStackTrace();
}
return con;
}
public static void closeAll(Connection con, Statement st, ResultSet set){
try{
if(set!=null)
set.close();
if(st!=null)
st.close();
if(con!=null)
con.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
三.创建主界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JFrame {
JButton button1;
JButton button2;
JButton button3;
JButton button4;
public Window(){
super("医院管理系统");
setBounds(300,300,600,400);
ImageIcon img=new ImageIcon("img/beijing.jpg");
JLabel jLabel=new JLabel(img);
this.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
jLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
JPanel panel=new JPanel();
button1=new JButton("病人档案");
button2=new JButton("科室信息");
button3=new JButton("医生信息");
button4=new JButton("就医档案");
button1.setBounds(250,40,100,50);
button2.setBounds(250,110,100,50);
button3.setBounds(250,180,100,50);
button4.setBounds(250,250,100,50);
button1.addActionListener(new MyActionListener());
button2.addActionListener(new MyActionListener());
button3.addActionListener(new MyActionListener());
button4.addActionListener(new MyActionListener());
panel.setLayout(null);
panel.add(jLabel);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
setContentPane(panel);
setVisible(true);
}
//内部类对按钮事件进行处理
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("病人档案")){
PatientDangan frame = new PatientDangan();
}
if(e.getActionCommand().equals("科室信息")){
DepartmentPage departmentPage=new DepartmentPage();
}
if(e.getActionCommand().equals("医生信息")){
DoctorPage doctorPage=new DoctorPage();
}
if(e.getActionCommand().equals("就医档案")){
PrecordPage precordPage=new PrecordPage();
}
}
}
public static void main(String[] args) {
new Window();
}
}
四.创建ORM实体类和Dao类(已病人档案为例)
创建Patient类(要有get、set方法)
import javax.xml.crypto.Data;
import java.sql.Date;
public class Patient {
int Pno;
String Pname;
String Psex;
String Pbirthday;
String area;
String condition;
int prepay;
String respon_doctor;
public Patient(){
}
public Patient(int pno, String pname,String psex, String pbirthday, String area, String condition, int prepay, String respon_doctor) {
Pno = pno;
Pname=pname;
Psex = psex;
this.Pbirthday = pbirthday;
this.area = area;
this.condition = condition;
this.prepay = prepay;
this.respon_doctor = respon_doctor;
}
public int getPno() {
return Pno;
}
public void setPno(int pno) {
Pno = pno;
}
public void setPname(String pname) {
Pname=pname;
}
public void setPsex(String psex) {
Psex = psex;
}
public String getPbirthday() {
return Pbirthday;
}
public void setPbirthday(String pbirthday) {
Pbirthday = pbirthday;
}
public String getArea() {
return area;
}
public String getPname() {
return Pname;
}
public void setArea(String area) {
this.area = area;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public int getPrepay() {
return prepay;
}
public void setPrepay(int prepay) {
this.prepay = prepay;
}
public String getRespon_doctor() {
return respon_doctor;
}
public void setRespon_doctor(String respon_doctor) {
this.respon_doctor = respon_doctor;
}
@Override
public String toString() {
return "Patient{" +
"Pno=" + Pno +
", Pname='" + Pname + '\'' +
", Psex='" + Psex + '\'' +
", Pbirthday=" + Pbirthday +
", area='" + area + '\'' +
", condition='" + condition + '\'' +
", prepay=" + prepay +
", respon_doctor='" + respon_doctor + '\'' +
'}';
}
public String getPsex() {
return Psex;
}
}
创建PatientDaolmpl
import sun.security.x509.DNSName;
import javax.xml.crypto.Data;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class PatientDaolmpl {
//插入档案
public int insert(Patient patient) {
Connection con = null;
PreparedStatement pst = null;
int result=0;
try {
con = DButils.getconnection();
pst = con.prepareStatement("insert into Patient values (?,?,?,?,?,?,?,?)");
pst.setInt(1, patient.getPno());
pst.setString(2,patient.getPname());
pst.setString(3, patient.getPsex());
pst.setString(4, patient.getPbirthday());
pst.setString(5, patient.getArea());
pst.setString(6, patient.getCondition());
pst.setInt(7, patient.getPrepay());
pst.setString(8, patient.getRespon_doctor());
result = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButils.closeAll(con, pst, null);
}return result;
}
//查询档案
public Patient select(int pno) {
Connection con = null;
PreparedStatement pst = null;
ResultSet set = null;
Patient patient=null;
try {
con = DButils.getconnection();
pst = con.prepareStatement("select * from Patient where Pno=?");
pst.setInt(1, pno);
set = pst.executeQuery();
while (set.next()) {
int no = set.getInt("Pno");
String pname=set.getString("Pname");
String sex = set.getString("Psex");
String b = set.getString("Pbirthday");
String areas = set.getString("area");
String conditions = set.getString("condition");
int p = set.getInt("prepay");
String rd = set.getString("respon_doctor");
patient = new Patient(no,pname, sex, b, areas, conditions, p, rd);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButils.closeAll(con, pst, set);
}
return patient;
}
//修改档案
public int update(Patient patient) {
Connection con = null;
PreparedStatement pst = null;
int result=0;
try {
con = DButils.getconnection();
pst = con.prepareStatement("update Patient set Pname=?,Psex=?,Pbirthday=?,area=?,condition=?,prepay=?,respon_doctor=? where Pno=?");
pst.setString(1, patient.getPname());
pst.setString(2, patient.getPsex());
pst.setString(3, patient.getPbirthday());
pst.setString(4, patient.getArea());
pst.setString(5, patient.getCondition());
pst.setInt(6, patient.getPrepay());
pst.setString(7, patient.getRespon_doctor());
pst.setInt(8, patient.getPno());
result=pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DButils.closeAll(con, pst, null);
}
return result;
}
//删除档案
public int delete(int pno){
Connection con=null;
PreparedStatement pst=null;
int result=0;
try {
con=DButils.getconnection();
pst=con.prepareStatement("delete from Patient where Pno=?");
pst.setInt(1,pno);
result=pst.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}finally {
DButils.closeAll(con,pst,null);
}
return result;
}
//查询所有
public List<Patient> selectAll() {
List<Patient> lists=new ArrayList<>();
Connection con=null;
PreparedStatement pst=null;
ResultSet set=null;
try{
con=DButils.getconnection();
pst=con.prepareStatement("select * from Patient");
set=pst.executeQuery();
while (set.next()){
int no = set.getInt("Pno");
String pname=set.getString("Pname");
String sex = set.getString("Psex");
String b = set.getString("Pbirthday");
String areas = set.getString("area");
String conditions = set.getString("condition");
int p = set.getInt("prepay");
String rd = set.getString("respon_doctor");
Patient patient=new Patient(no,pname,sex,b,areas,conditions,p,rd);
lists.add(patient);
}
return lists;
}catch (SQLException e){
e.printStackTrace();
}finally {
DButils.closeAll(con,pst,set);
}
return null;
}
}
五.创建界面(已病人档案为例)
注:界面的布局是用插件WindowBuilder完成的!!!
import com.mysql.fabric.xmlrpc.base.Data;
import jdk.internal.dynalink.linker.LinkerServices;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.*;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PatientDangan extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JLabel label;
private JTextField textField_1;
private JLabel label_1;
private JLabel label_2;
private JTextField textField_3;
private JComboBox comboBox;
private JLabel label_3;
private JLabel label_4;
private JTextField textField_4;
private JLabel label_5;
private JTextField textField_5;
private JComboBox comboBox_1;
private JTextField textField_2;
private JTextField textField_6;
private JLabel label_8;
private JTextField textField_7;
JTextField textField_8;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PatientDangan frame = new PatientDangan();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
DefaultTableModel model;
public void table(){
model = new DefaultTableModel();
model.setColumnIdentifiers(new Object[]{"病人编号", "姓名", "性别", "生日", "地区", "住院情况", "预交费用", "负责医生"});
model.addRow(new Object[]{"Pno", "Pname", "Psex", "Pbirthday", "area", "condition", "prepay", "respon_doctor"});
PatientDaolmpl patientDaolmpl = new PatientDaolmpl();
List<Patient> list = patientDaolmpl.selectAll();
int i = 0;
for (Object p : list) {
model.addRow(new Object[]{list.get(i).Pno, list.get(i).Pname, list.get(i).Psex, list.get(i).Pbirthday, list.get(i).area, list.get(i).condition, list.get(i).prepay, list.get(i).respon_doctor});
i++;
}
JTable fk = new JTable(model);
JScrollPane jsp = new JScrollPane(fk);
jsp.setBounds(100, 200, 600, 130);
contentPane.add(jsp);
}
public PatientDangan() {
super("病人档案");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 470);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JButton btnNewButton = new JButton("\u6DFB\u52A0\u6863\u6848");
btnNewButton.addActionListener(null);
btnNewButton.setToolTipText("rf");
btnNewButton.setBounds(318, 87, 113, 39);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.setToolTipText("");
textField.setBounds(84, 13, 86, 24);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("\u75C5\u4EBA\u7F16\u53F7:");
lblNewLabel.setBounds(14, 16, 72, 18);
contentPane.add(lblNewLabel);
label = new JLabel("\u59D3\u540D:");
label.setBounds(195, 16, 72, 18);
contentPane.add(label);
textField_1 = new JTextField();
textField_1.setBounds(234, 13, 86, 24);
contentPane.add(textField_1);
textField_1.setColumns(10);
label_1 = new JLabel("\u6027\u522B:");
label_1.setBounds(350, 16, 72, 18);
contentPane.add(label_1);
label_2 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
label_2.setBounds(468, 16, 86, 18);
contentPane.add(label_2);
textField_3 = new JTextField();
textField_3.setBounds(540, 13, 86, 24);
contentPane.add(textField_3);
textField_3.setColumns(10);
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[]{"\u4F4F\u9662\u4E2D", "\u5DF2\u51FA\u9662"}));
comboBox.setSelectedIndex(1);
comboBox.setBounds(234, 50, 93, 24);
contentPane.add(comboBox);
label_3 = new JLabel("\u4F4F\u9662\u60C5\u51B5\uFF1A");
label_3.setBounds(159, 53, 75, 18);
contentPane.add(label_3);
label_4 = new JLabel("\u7C4D\u8D2F\uFF1A");
label_4.setBounds(14, 53, 72, 18);
contentPane.add(label_4);
textField_4 = new JTextField();
textField_4.setBounds(59, 47, 86, 24);
contentPane.add(textField_4);
textField_4.setColumns(10);
label_5 = new JLabel("\u9884\u4ED8\u533B\u7597\u8D39\uFF1A");
label_5.setBounds(350, 56, 93, 18);
contentPane.add(label_5);
textField_5 = new JTextField();
textField_5.setBounds(434, 50, 86, 24);
contentPane.add(textField_5);
textField_5.setColumns(10);
comboBox_1 = new JComboBox();
comboBox_1.setModel(new DefaultComboBoxModel(new String[]{"\u7537", "\u5973"}));
comboBox_1.setSelectedIndex(1);
comboBox_1.setBounds(388, 13, 55, 24);
contentPane.add(comboBox_1);
JLabel label_6 = new JLabel("\u75C5\u4EBA\u7F16\u53F7\uFF1A");
label_6.setBounds(26, 138, 86, 18);
contentPane.add(label_6);
textField_2 = new JTextField();
textField_2.setBounds(101, 135, 86, 24);
contentPane.add(textField_2);
textField_2.setColumns(10);
JButton button = new JButton("\u67E5\u8BE2\u6863\u6848");
button.addActionListener(null);
button.setBounds(201, 128, 106, 39);
contentPane.add(button);
JLabel label_7 = new JLabel("\u75C5\u4EBA\u7F16\u53F7\uFF1A");
label_7.setBounds(419, 138, 86, 18);
contentPane.add(label_7);
JButton button_1 = new JButton("\u5220\u9664\u6863\u6848");
button_1.setBounds(592, 128, 106, 39);
contentPane.add(button_1);
textField_6 = new JTextField();
textField_6.setBounds(492, 135, 86, 24);
contentPane.add(textField_6);
textField_6.setColumns(10);
label_8 = new JLabel("\u8D1F\u8D23\u533B\u751F\uFF1A");
label_8.setBounds(540, 53, 82, 18);
contentPane.add(label_8);
textField_7 = new JTextField();
textField_7.setBounds(612, 50, 86, 24);
contentPane.add(textField_7);
textField_7.setColumns(10);
table();
JButton button_2 = new JButton("\u4FEE\u6539\u6863\u6848");
button_2.addActionListener(null);
button_2.setBounds(392, 360, 113, 39);
contentPane.add(button_2);
JLabel label_9 = new JLabel("\u75C5\u4EBA\u7F16\u53F7\uFF1A");
label_9.setBounds(208, 370, 84, 18);
contentPane.add(label_9);
textField_8 = new JTextField();
textField_8.setBounds(291, 370, 86, 24);
contentPane.add(textField_8);
textField_8.setColumns(10);
setVisible(true);
btnNewButton.addActionListener(new MyActionListen2());
button.addActionListener(new MyActionListen2());
button_1.addActionListener(new MyActionListen2());
button_2.addActionListener(new MyActionListen2());
}
public void removetable(){
int sum=model.getRowCount();
for(int i=1;i<sum;i++)
model.removeRow(1);
}
class MyActionListen2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("添加档案")) {
int no = Integer.parseInt(textField.getText());
String pname = textField_1.getText();
String sex = comboBox_1.getSelectedItem() + "";
String bir = textField_3.getText();
String area = textField_4.getText();
String condition = comboBox.getSelectedItem() + "";
int py = Integer.parseInt(textField_5.getText());
String doctor = textField_7.getText();
PatientDaolmpl patientDaolmpl = new PatientDaolmpl();
Patient patient = new Patient(no, pname, sex, bir, area, condition, py, doctor);
int result = patientDaolmpl.insert(patient);
if (result != 0) {
JOptionPane.showMessageDialog(contentPane, "插入成功!", "提示", JOptionPane.WARNING_MESSAGE);
model.addRow(new Object[]{no, pname, sex, bir, area, condition, py, doctor});
}else {
JOptionPane.showMessageDialog(contentPane, "插入失败!", "提示", JOptionPane.WARNING_MESSAGE);
}
textField.setText("");
textField_1.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
textField_7.setText("");
}
if (e.getActionCommand().equals("查询档案")) {
int no=Integer.parseInt(textField_2.getText());
PatientDaolmpl patientDaolmpl2=new PatientDaolmpl();
Patient patient=patientDaolmpl2.select(no);
if(patient!=null){
removetable();
model.addRow(new Object[]{patient.Pno,patient.Pname,patient.Psex,patient.Pbirthday,patient.area,patient.condition,patient.prepay,patient.respon_doctor});
}else
JOptionPane.showMessageDialog(contentPane, "该编号不存在!", "提示", JOptionPane.WARNING_MESSAGE);
textField_2.setText("");
}
if (e.getActionCommand().equals("删除档案")) {
int no=Integer.parseInt(textField_6.getText());
PatientDaolmpl patientDaolmpl3=new PatientDaolmpl();
int result=patientDaolmpl3.delete(no);
if(result!=0){
table();
}else {
JOptionPane.showMessageDialog(contentPane, "删除失败!", "提示", JOptionPane.WARNING_MESSAGE);
}
textField_6.setText("");
}
if (e.getActionCommand().equals("修改档案")) {
int no=Integer.parseInt(textField_8.getText());
updataPatient updataPatient=new updataPatient(no);
textField_8.setText("");
}
}
}
}