最近做了一个简单的医院管理系统,查阅资料时发现,网上的很多java语言做的语言管理系统项目过于庞大,代码过于繁杂,不适于初学者更好的理解,下面我把我做的简单的医院管理系统分享给大家。当然了大牛直接略过,此文仅使用于小白更好的理解java语言。
使用java语言链接SQLserver数据库,当然也可以链接MYSQL数据库,只需要修改一下driver和url就可以了。本文仅演示sqlserver链接数据库
首先需要在webroot--webinf--lib里面导入jar包,整个项目我已在上传文件创作栏里上传了,由于文章创作不能加入这个jar包,所以需要jar包的可以去我所上传的文件创作里面找。
如下图所示mssql-jdbc-8.4.1.jre8.jar就是sqlsever的jar包位置
下图是整个项目的所编写的类所在位置
下图是使用MyEclipse链接数据库的代码
package hospital;
import java.sql.*;
public class DBUtil {
// 连接
private Connection con = null;
public String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=HospitalManager";
public String username = "sa";
public String password = "123456";
// 获取连接
public Connection getConnection() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
System.out.println("连接成功");
} 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();
}
}
创建登录页面并实现从数据库中获取账号密码
package hospital;
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 userId;
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("密码:");
public JRadioButton r1,r2,r3;
ImageIcon bg=new ImageIcon("WebRoot\image\doctor.jpg");
JLabel bgJLabel=new JLabel(bg);
JButton loginJButton=new JButton("登录");
JButton cancelJButton=new JButton("取消");
// private boolean flag;
static JTextField userIdJTextField = new JTextField();
static JPasswordField passwordJPasswordField = new JPasswordField();
public 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(userIdJTextField);
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(r2);
this.add(r3);
rg.add(r3);
this.add(r1);
rg.add(r1);
r1.setBounds(150, 180, 80, 30);
r2.setBounds(230, 180, 80, 30);
r3.setBounds(310, 180, 80, 30);
r1.setForeground(Color.RED);
r2.setForeground(Color.RED);
r3.setForeground(Color.RED);
r1.setFont(new Font("",3,15));
r2.setFont( new Font("",3,15));
r3.setFont(new Font("",3,15));
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 (r1.isSelected()) {
try {
String userIdText = userIdJTextField.getText().toString(); // 获取帐号文本框内容
String passwordText =String.valueOf(passwordJPasswordField.getPassword()); // 获取密码文本框内容
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from tb_User"); // 执行SQL语句,返回结果集
while (rs.next()) {
userId = rs.getString("UserID"); // 获取登录的用户编号,
password = rs.getString("UserPasswd"); // 获取数据库中的数据项的密码
if (userId.equals(userIdText) && password.equals(passwordText)) {// 判断数据库的用户编号以及密码是否与文本框的值相同
loginFlag = 1;
break;
}
}
// 登录成功
if (loginFlag == 1) {
JOptionPane.showMessageDialog(null, "登录成功");
new HomePage("管理员界面"); // 显示系统主界面
Login.this.setVisible(false);// 关闭登录按钮
// 登录失败
} else {
userIdJTextField.setText(""); // 错误的话则文本框内容设置为空,显示错误标签
passwordJPasswordField.setText("");
JOptionPane.showMessageDialog(null, "账号或密码不正确,请重新输入!");
}
} catch (SQLException e3) {
System.out.println(e3);
}
}
}
});
// 登录按钮添加功能事件
// 账号
accountJLabel.setBounds(150, 50, 100, 50);
accountJLabel.setFont(new Font("", 3, 25));
accountJLabel.setForeground(Color.RED);
// 密码
passwordJLabel.setBounds(150, 120, 100, 50);
passwordJLabel.setFont(new Font("", 3, 25));
passwordJLabel.setForeground(Color.red);
// 登录
loginJButton.setBounds(150, 220, 100, 40);
loginJButton.setBackground(Color.CYAN);
// 取消
cancelJButton.setBounds(280, 220, 100, 40);
cancelJButton.setBackground(Color.CYAN);
// 账号输入框
userIdJTextField.setBounds(250, 60, 150, 30);
// 密码输入框
passwordJPasswordField.setBounds(250, 120, 150, 30);
this.add(bgJLabel);
this.setVisible(true);
this.setSize(600, 350); // 设置窗口大小
this.setResizable(true); // 设置不可调整窗口大小
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
Login login = new Login("医院管理系统");
}
}
创建主页窗体
package hospital;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.enterprise.inject.New;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HomePage extends Jframe{
private JLabel la1, la2;
private Font laFont = new Font("隶书", Font.BOLD, 60);
private Font btFont=new Font("宋体",Font.BOLD,25);
ImageIcon background = new ImageIcon("WebRoot\image\2051854.jpg");
JLabel bgJLabel = new JLabel(background);
JButton doctorJButton=new JButton("医生信息管理");
JButton patientJButton=new JButton("病人信息管理");
JButton wardJButton=new JButton("科室信息管理");
public HomePage() {}
public HomePage(String a){
super(a);
this.setLayout(null);
this.add(doctorJButton);
this.add(patientJButton);
this.add(wardJButton);
doctorJButton.setFont(btFont);
patientJButton.setFont(btFont);
wardJButton.setFont(btFont);
doctorJButton.setBounds(0, 0, 200, 200);
patientJButton.setBounds(0, 200, 200, 200);
wardJButton.setBounds(0, 400, 200, 200);
la1 = new JLabel("欢迎使用");
la2 = new JLabel("医院信息管理系统");
this.add(la1);
this.add(la2);
la1.setBounds(500, 100, 500, 100);
la1.setFont(laFont);
la2.setBounds(400,200, 600, 100);
la2.setFont(laFont);
this.add(bgJLabel);
bgJLabel.setBounds(0, 0, 1000, 600);
wardJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new department("科室管理界面");
HomePage.this.setVisible(false);
}
});
this.setTitle("主页面");
this.setResizable(true);
this.setVisible(true);
this.setSize(1000, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
HomePage homePage=new HomePage("主页面1");
}
}
创建科室表窗体并实现简单的增删改查
package hospital;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.plaf.OptionPaneUI;
public class department extends Jframe{
private Font laFont=new Font("宋体", Font.BOLD, 25);
private Font btFont=new Font("宋体", Font.BOLD, 25);
DBUtil dbUtil = new DBUtil();
Connection con = dbUtil.getConnection();
JButton addJButton=new JButton("添加");
JButton deleteJButton=new JButton("删除");
JButton updateJButton=new JButton("修改");
JButton selectJButton=new JButton("查询");
JLabel denoJLabel=new JLabel("科室编号");
JLabel denameJLabel=new JLabel("科室姓名");
JLabel deaddrJLabel=new JLabel("科室地址");
JLabel dephoneJLabel=new JLabel("科室电话");
JLabel demasterJLabel=new JLabel("科室主任");
JTextField denoJTextField=new JTextField();
JTextField denameJTextField=new JTextField();
JTextField deaddrJTextField=new JTextField();
JTextField dephoneJTextField=new JTextField();
JTextField demasterJTextField=new JTextField();
public department(){}
public department(String b){
super(b);
this.setLayout(null);
this.setSize(1000,600);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
this.add(denoJLabel);
this.add(denoJTextField);
denoJLabel.setBounds(100, 50, 120,50 );
// denoJLabel.setBackground(Color.blue);
// denoJLabel.setOpaque(true);
denoJLabel.setFont(laFont);
denoJTextField.setBounds(220, 50, 200, 50);
this.add(denameJLabel);
denameJLabel.setBounds(100, 150, 120, 50);
denameJLabel.setFont(laFont);
this.add(denameJTextField);
denameJTextField.setBounds(220, 150, 200, 50);
this.add(deaddrJLabel);
deaddrJLabel.setBounds(550, 150, 120, 50);
deaddrJLabel.setFont(laFont);
// deaddrJLabel.setBackground( Color.red);
// deaddrJLabel.setOpaque(true);
this.add(deaddrJTextField);
deaddrJTextField.setBounds(670, 150, 200, 50);
this.add(dephoneJLabel);
dephoneJLabel.setBounds(100, 250, 120, 50);
dephoneJLabel.setFont(laFont);
this.add(dephoneJTextField);
dephoneJTextField.setBounds(220, 250, 200, 50);
this.add(demasterJLabel);
demasterJLabel.setBounds(550, 250, 200, 50);
demasterJLabel.setFont(laFont);
this.add(demasterJTextField);
demasterJTextField.setBounds(670, 250, 200, 50);
this.add(selectJButton);
selectJButton.setBounds(600, 50, 120, 50);
selectJButton.setFont(btFont);
this.add(addJButton);
addJButton.setBounds(200, 400, 120, 50);
addJButton.setFont(btFont);
this.add(deleteJButton);
deleteJButton.setBounds(400, 400, 120, 50);
deleteJButton.setFont(btFont);
this.add(updateJButton);
updateJButton.setBounds(600, 400, 120, 50);
updateJButton.setFont(btFont);
selectJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String denojString=denoJTextField.getText().toString();
String s1="select * from Department where Deno=?";
PreparedStatement pStatement=con.prepareStatement(s1);
pStatement .setString(1, denojString);
ResultSet rSet=pStatement.executeQuery();
while (rSet.next()) {
denoJTextField.setText(rSet.getString("Deno"));
denameJTextField.setText(rSet.getString("Dename"));
deaddrJTextField.setText(rSet.getString("Deaddr"));
dephoneJTextField.setText(rSet.getString("Dephone"));
demasterJTextField.setText(rSet.getString("Demaster"));
}
} catch (Exception e2) {
// TODO: handle exception
}
}
});
deleteJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String s1="delete from Department where Deno=?";
PreparedStatement ps=con.prepareStatement(s1);
String denoJText=denoJTextField.getText().toString();
ps.setString(1, denoJText);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
}
catch (Exception e2) {
System.out.println(e2);
}
}
});
updateJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String s1="update department set dename=?,deaddr=?,dephone=?,demaster=? where deno=?";
PreparedStatement pStatement=con.prepareStatement(s1);
String denoText=denoJTextField.getText().toString();
String deNameText=denameJTextField.getText().toString();
String deAddrText=deaddrJTextField.getText().toString();
String dePhoneText=dephoneJTextField.getText().toString();
String deMasterText=demasterJTextField.getText().toString();
pStatement.setString(1, deNameText);
pStatement.setString(2, deAddrText);
pStatement.setString(3, dePhoneText);
pStatement.setString(4, deMasterText);
pStatement.setString(5, denoText);
pStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "更新成功!");
} catch (Exception e2) {
// TODO: handle exception
}
}
});
addJButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String s1="insert into department values(?,?,?,?,?)";
PreparedStatement pStatement=con.prepareStatement(s1);
String denoText=denoJTextField.getText().toString();
String deNameText=denameJTextField.getText().toString();
String deAdderText=deaddrJTextField.getText().toString();
String dePhoneText=dephoneJTextField.getText().toString();
String deMasterText=demasterJTextField.getText().toString();
pStatement.setString(1, denoText);
pStatement.setString(2, deNameText);
pStatement.setString(3, deAdderText);
pStatement.setString(4, dePhoneText);
pStatement.setString(5, deMasterText);
pStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "插入成功!");
} catch (Exception e1) {
System.out.println(e1);
}
}
});
}
public static void main(String[] args) {
department departments=new department("科室管理界面");
}
}
科室窗体的增删改查已经实现,病人窗体和医生窗体可以根据以上代码,自行创作一下。



