医院管理系统

源码:https://pan.baidu.com/s/1wV8Sz_lpR5WSIHH2u0cG8w 提取码:6q9n
数据库文件:https://pan.baidu.com/s/16EfM41xYniXELWRT-ZANcg 提取码:dhm5
这里用javaweb进行数据库的增删改查操作,只是一个极其简单的程序,之所以叫医院管理系统,因为名字是老师定的,核心跟学生管理系统等一样。数据库连接也没用到连接池,网页前端的制作也没有编写,这里只是后端代码实现简单的增删改查功能。建议javaweb初学者学习。

思路

首先拿到这样的题目,应该先思考需要实现哪些功能,先不急着敲代码。对于管理系统,增删改查是肯定要的,医院系统,需要把医院医生信息和病人信息分隔开避免混淆。然后再做一下界面就满足老师的要求了。

1、数据库的建立

创建医院医生信息表和病人信息表,由于只是需要一些增删改查功能,我建的表很简单,医生表(用户名和密码),病人表(姓名和床位)。(没有设置主键,只有字段,简易展示一下)

创建各个包

这个看个人习惯,有点人喜欢先写前端代码再写后端,有的人喜欢先写后端数据库代码,再编写前端,我这里是从数据库开始编写的(后者),先创了多个包和一个.properties

说明一下每个包的作用

  • .bean:存放一些实体类
  • .dao:存放数据访问的接口
  • .dao.impl:数据访问接口的实现类
  • .service:业务逻辑接口
  • .servlet.impl:业务逻辑的接口实现类
  • .servlet:web层代码
  • .util:存放工具类
  • .properties: 数据库连接文件

如果你是一个新手,你看到这可能会一头雾水,当然在厉害的程序员看来,这只是很基本的开发模式而已。为什么要用这个开发模式呢,这个开发模式就是简易的MVC开发模式。

  • MVC模式的优点:降低各个模块之间的耦合,能够增强程序的可维护性和可扩展性,提高了模型的复用性。
  • MVC模式的缺点:增加了程序源码的复杂性。

利用分层的思想将不同的模块内容分开,可以方便软件开发人员分工协作,提高开发效率。

编写实体类

分别编写病人和医生的实体类
病人

public class Patient {private String name;private String num;public Patient() {super();// TODO Auto-generated constructor stub}public Patient(String name, String num) {super();this.name = name;this.num = num;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}}

医生

public class User {private String name;private String password;public User() {super();// TODO Auto-generated constructor stub}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public User( String name, String password) {super();this.name = name;this.password = password;}
}

没什么好解释的,跟数据库的字段对应就可以了

数据库的连接

JDBC规范在jdk中的
java.sql.;
javax.sql.;
这两个包下面,不过这里面都是接口,要想使用JDBC的话,需要下载相关的数据库驱动jar包,这里咱们使用的是MySQL数据库,所以需要下载MySQL的数据库驱动:网盘地址。
JDBC的四个核心接口:

  • DriverManager:用于注册驱动并创建符合该驱动的数据库的连接。
  • Connection: 表示与数据库创建的连接对象,即一个connection对应着一个会话,相当于在mysql workbench中打开了一个连接。
  • Statement: 操作数据库sql语句的对象,有个两个实现类:Statement和PreparedStatement(常用)。
  • ResultSet: 从数据库中查询的结果集。

基本上通过使用上面4个接口就能使用java实现对数据库的增删改查了。

为了提高可维护性,可以将这些经常变换内容写到一个配置文件中,这里创建一个名为db.properties的文件:

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hospital
username=root
password=123456

3306是Mysql的默认端口,hospital是数据库的名字
username是数据库的用户名,一般为root
password是数据库的密码,输入你的密码就可以了

创建数据库连接工具类

创建一个DBUtil的工具类,在这个类中注册驱动和获取连接:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;public class DButil {private static String driverClass;private static String url;private static String username;private static String password;static {ResourceBundle rb = ResourceBundle.getBundle("db");driverClass = rb.getString("driverClass");url = rb.getString("url");username = rb.getString("username");password = rb.getString("password");try {Class.forName(driverClass);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static Connection getConnection() throws SQLException {return DriverManager.getConnection(url, username, password);}
}

JDBC常用接口简单介绍一下

  • DriverManager:该类的主要作用就是创建连接(不同的数据库,在forName中的参数写法不同)
  • Statement:该接口的作用是操作sql语句,并返回相应结果的对象
  • ResultSet:该接口的作用主要用来封装结果集。
    数据库的就不多说了,如果实在看不懂或者有问题,可以自行百度多看看大佬的文章。

编写Dao

主要实现的功能都写在这里,方便我们日后代码的调试.
医院管理员:

public interface UserDao {//医院管理员注册public void addUser(User user) throws Exception;//医生管理员登录public User findUserByNameAndPassword(User user) throws Exception;
}

病人:

public interface PatientDao {//查看病人public Map< Integer,Patient> Look() throws Exception;//添加病人public void addPatient(Patient patient)throws Exception;//删除病人public void deletePatient(Patient patient)throws Exception;//通过床号判断是否有人public int findId(Patient patient)throws Exception;
}

注释写的很明白了,这里两个都是接口。接下来写实现类。

.dao.impl实现类的编写

病人:

public class PatientDaoImpl implements PatientDao {//查看病人//@Overridepublic Map< Integer,Patient> Look() {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;Map<Integer,Patient> map=new HashMap<>();Patient patient = null;int a = 1;String sql="select * from patient";try {conn = DButil.getConnection();ps=conn.prepareStatement(sql);rs=ps.executeQuery();while(rs.next()) {patient = new Patient();patient.setName(rs.getString(1));patient.setNum(rs.getString(2));map.put(a, patient);a++;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return map;}//添加病人//@Overridepublic void addPatient(Patient patient)  {// TODO Auto-generated method stubConnection conn = null;PreparedStatement ps =null;String sql=" insert patient(`name`,`id`) value (?,?); ";try {conn=DButil.getConnection();ps=conn.prepareStatement(sql);ps.setString(1, patient.getName());ps.setString(2, patient.getNum());ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void deletePatient(Patient patient) {Connection conn=null;PreparedStatement ps =null;String sql = "delete from patient where name =?";try {conn=DButil.getConnection();ps=conn.prepareStatement(sql);ps.setString(1, patient.getName());ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic int findId(Patient patient) throws Exception {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;int a = 0;String sql = "select name, id from patient where id=?";conn = DButil.getConnection();ps = conn.prepareStatement(sql);ps.setString(1, patient.getNum());rs = ps.executeQuery();if(rs.next()) {a=1;}return a;}

医生:

//注册医院管理员@Overridepublic void addUser(User user) {// TODO Auto-generated method stubConnection conn=null;PreparedStatement ps =null;String sql="insert h_user(`name`,`password`) value (?,?)";try {conn=DButil.getConnection();ps=conn.prepareStatement(sql);ps.setString(1, user.getName());ps.setString(2, user.getPassword());ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//医院管理员登录@Overridepublic User findUserByNameAndPassword(User user) throws Exception {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;User u = null;try {conn = DButil.getConnection();ps = conn.prepareStatement("select * from h_user where name=? and password=?");ps.setString(1, user.getName());ps.setString(2, user.getPassword());rs = ps.executeQuery();if(rs.next()){u = new User();u.setName(rs.getString(1));u.setPassword(rs.getString(2));}} catch (Exception e) {e.printStackTrace();}return u;}

看起来代码挺多的,其实都是些差不多的重复性代码。拿查看病人为例,步骤如下:

  • 创建Connection对象、PreparedStatement对象、ResultSet对象。
  • 创建Map<Integer,Patient>用来接收返回的数据
  • 编写原生SQL:“select * from patient”
  • 创建连接:conn = DButil.getConnection();
  • 用PreparedStatement对象执行SQL语句
  • 然后用ResultSet对象接收结果。
  • 用while语句循环,把结果放到我们的Map中。返回Map

再比如添加病人这段:

  • 创建Connection对象、PreparedStatement对象
  • 编写原生SQL:insert patient(name,id) value (?,?)由于这里的查询条件需要我们网页输入的信息来判断,约束条件有?代替。
  • PreparedStatement对象的executeUpdate()方法来更新数据库。

其他的不说了,差不多的代码,就SQL语句不同,返回的值不同而已。

逻辑层代码

医生:

public interface UserService {//医院管理员注册public void addUser(User user)throws Exception;//医院管理员登录public User findUserByNameAndPassword(User user) throws Exception;
}

病人:

public interface PatientService {//查看病人public Map<Integer,Patient> Look() throws Exception;//添加病人public void addPatient(Patient patient) throws Exception;//删除病人public void deletePatient(Patient patient) throws Exception;//查找床号public int findId(Patient patient)throws Exception;
}

这里的代码和Dao里的代码完全一样。就不说了。

逻辑层实现类

医生:

public class UserServiceImpl implements UserService {UserDao u = new UserDaoImpl();//医院管理员登录@Overridepublic void addUser(User user) throws Exception {// TODO Auto-generated method stubu.addUser(user);}@Overridepublic User findUserByNameAndPassword(User user) throws Exception {return u.findUserByNameAndPassword(user);}
}

病人:

public class PatientServiceImpl implements PatientService {PatientDao pd = new PatientDaoImpl();@Overridepublic Map<Integer,Patient> Look() throws Exception {return pd.Look();}//添加病人@Overridepublic void addPatient(Patient patient) throws Exception {// TODO Auto-generated method stubpd.addPatient(patient);}@Overridepublic void deletePatient(Patient patient) throws Exception {// TODO Auto-generated method stubpd.deletePatient(patient);}@Overridepublic int findId(Patient patient) throws Exception {return pd.findId(patient);}}

这里的实现类相信你应该能看明白,因为就创建了之前数据层的接口实现类的实例,然后调用其方法而已。有一点要注意:因为是接口,所以接口在实例化的时候跟类不一样:

UserDao u = new UserDaoImpl();
PatientDao pd = new PatientDaoImpl();

Servlet类的编写

为了方便观看学习研究,我把增删改查做了4个servlet,来分别实现其功能。
由于前段代码过于简陋就不贴了,无非就几个表单。

医院管理员注册

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");User u = new User();u.setName(request.getParameter("username"));u.setPassword(request.getParameter("password"));UserService us = new UserServiceImpl();PatientService ps = new PatientServiceImpl();try {us.addUser(u);request.getSession().setAttribute("user", u);Map<Integer,Patient> map = ps.Look();request.getServletContext().setAttribute("map",map);request.getRequestDispatcher("/login_success.jsp").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}

重写doGet方法,由于还没有写字符编码过滤器,先确定编码,然后用一个User对象接受表单数据。实现业务逻辑层代码的实例:

UserService us = new UserServiceImpl();
PatientService ps = new PatientServiceImpl();

用us的addUser()方法,把注册的医生放到数据库中
用ps的Look()方法,把目前的病人信息展示给这名医生。
最后返回jsp显示到页面上。

医院管理员登录

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");User user = new User();user.setName(request.getParameter("name"));user.setPassword(request.getParameter("password"));UserService us = new UserServiceImpl();try {User u = us.findUserByNameAndPassword(user);//分发转向if(u!=null){//如果登录成功,就把user对象放到session对象中request.getSession().setAttribute("user", u);PatientService ps = new PatientServiceImpl();Map<Integer,Patient> map = ps.Look();request.getServletContext().setAttribute("map",map);request.getRequestDispatcher("/login_success.jsp").forward(request, response);}else{request.setAttribute("msg", "用户名或密码不正确!");request.getRequestDispatcher("/login.jsp").forward(request, response);}} catch (Exception e) {e.printStackTrace();}}

刚刚是注册,现在是登录,前面基本差不多,不过需要findUserByNameAndPassword()方法先判断一下该用户是否已经注册过

  • 如果没有,页面会返回,提示登录失败。
  • 如果有,登录成功,再把病人信息调出来显示

添加病人

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");Patient patient = new Patient();patient.setName(request.getParameter("patientname"));patient.setNum(request.getParameter("num"));PatientService ps = new PatientServiceImpl();try {if(ps.findId(patient)==0) {                ps.addPatient(patient);Map<Integer,Patient> map = ps.Look();request.getServletContext().setAttribute("map",map);request.getRequestDispatcher("/login_success.jsp").forward(request, response);}else {request.setAttribute("addFalsepatient",patient );request.getRequestDispatcher("/Addfalse.jsp").forward(request, response);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}

接受表单数据后,需要先去数据库中查询,是否存在这位病人,根据床号是否空余给病人安排床号。添加成功后重新调用PatientService的Look()方法,刷新病人信息反馈给医生

删除病人

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");Patient patient = new Patient();patient.setName(request.getParameter("deleteName"));PatientService ps = new PatientServiceImpl();try {ps.deletePatient(patient);Map<Integer,Patient> map = ps.Look();request.getServletContext().setAttribute("map",map);request.getRequestDispatcher("/login_success.jsp").forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}

如果你看懂了前面几个,这个你肯定不难看出其原理。这个删除功能就留给你思考吧。

总结

这样简单的项目就写完了。当然只适合给没有多少基础的javaweb初学者参考。因为这里的代码都是很简易的版本,实际应用比这个要复杂的多的多。但是完整的敲完这些代码也能让我们更好的体会到MVC这种开发模式。对分层思想有更好的体悟。

一些问题

  • 当请求request中携带了用户提交的数据时,需要将这些数据封装到JavaBean中,像之前写法需要一一赋值,倘若request携带了非常多的表单数据,此时的赋值操作就显得比较繁琐了,那有没有好的解决方法呢?这里可以使用apache的commons-beanutils搞定这个问题。
  • 密码在储存过程中换需要加密过程。
  • 在项目中还需要加入权限代码的编写,和过滤器的使用。

javaweb简化的医院管理系统相关推荐

  1. 基于javaweb+springboot的医院管理系统(java+Springboot+ssm+mysql+maven)

    基于javaweb+springboot的医院管理系统(java+Springboot+ssm+mysql+maven) 一.项目简述 功能:该项目是用springboot+layui+shiro写的 ...

  2. 基于javaweb的医院管理系统(java+springboot+mybatis+vue+mysql)

    基于javaweb的医院管理系统(java+springboot+mybatis+vue+mysql) 运行环境 Java≥8.MySQL≥5.7.Node.js≥10 开发工具 后端:eclipse ...

  3. 基于javaweb+jsp的医院住院管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Bootstrap)

    基于javaweb+jsp的医院住院管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Bootstrap) JavaWeb JavaBean JSP MVC ...

  4. 基于javaweb+springboot的医院分诊挂号住院管理系统(java+SpringBoot+FreeMarker+Mysql)

    基于javaweb+springboot的医院分诊挂号住院管理系统(java+SpringBoot+FreeMarker+Mysql) 主要实现了从挂号预约到分诊住院出诊等一些列医院基本操作流程的全部 ...

  5. javaweb JAVA JSP医院药品管理系统 JSP药品进销存系统 JSP医药进销存系统 JSP医药销售管理系统

    javaweb JAVA JSP医院药品管理系统 JSP药品进销存系统 JSP医药进销存系统 JSP医药销售管理系统 protected void doGet(HttpServletRequest r ...

  6. 基于javaweb的医院管理系统

    1.1 基于javaweb的医院管理系统 1.2 程序 编程语言: java 框架: springboot 前台: html js bootstrap框架 jquary 开发工具:IDEA2020,J ...

  7. 基于javaweb+jsp的医院住院管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Layui Ajax)

    基于javaweb+jsp的医院住院管理系统(JavaWeb JSP MySQL Servlet SSM SpringBoot Layui Ajax) 运行环境 Java≥8.MySQL≥5.7.To ...

  8. Java界面排号系统_【前端系统】javaweb技术的医院门诊在线预约及排号管理系统的实现...

    描述 本系统着力于实际生活需求,介绍基于javaweb技术的医院预约门诊挂号系统信息化软件工程思想,分析设计与实现该系统的需求.力在解决医院门诊优化,看诊用户挂号等待时间长问题,优化门诊效率,从而提高 ...

  9. javaweb JSP JAVA 医疗住院护士工作站系统(医疗住院 护士管理系统 医院管理系统 挂号管理系统)

    JSP JAVA 医疗住院护士工作站系统(医疗住院 护士管理系统 医院管理系统 挂号管理系统)

最新文章

  1. Python:尝试对知乎网验证码进行处理
  2. linux(ubuntu)环境下安装及配置JDK
  3. mysql 关联 update_关于SQL UPDATE关联更新
  4. (笔记)电路设计(三)之0欧姆电阻、磁珠、电感的应用
  5. 为何要领域驱动设计?
  6. 2017 年全国大学生电子设计竞赛试题——四旋翼自主飞行器探测跟踪系统(C 题)【本科组】1
  7. 跟我学Telerik公司的RadControls控件(四)
  8. php网站不能访问,php怎么不登录不能访问
  9. C++:求第k小的数
  10. c:\program files\microsoft visual studio\vc98\mfc\include\afxv_w32.h(14) : fatal error C1189:
  11. 高性能MySQL读书笔记——开天辟地
  12. 关于搭建测试环境(详细)
  13. 做一个有批判性思维的程序员!
  14. 手机号中间四位变成****
  15. 笔记本电脑连接(扩展)外接显示器之后桌面图标乱放位置解决办法-以win10系统为例
  16. CentOS8 启动错误,enter emergency mode 报错 Failed to mount /sysroot 解决方法
  17. 3轴/6轴/9轴传感器是什么, 加速计/陀螺仪/磁力计又是什么?
  18. 魅蓝5s即将发布 真假快充15日揭晓答案
  19. Happens-Before原则
  20. 小米4A千兆版刷机telnet失败解决办法

热门文章

  1. UI 设计中的视觉无障碍设计
  2. node.js请求接口
  3. php smtp用户名和密码错误,PHPMailer中的“SMTP错误:无法进行身份验证”
  4. java时间代码_java处理日期时间代码
  5. 【论文阅读】iSAM贝叶斯树相关内容理解与学习
  6. java中break和continue的用法
  7. Java 生成二维码。
  8. 用MPAndoidChart展示搜索到的GPS及卫星信息
  9. 有哪一些测不准原理?
  10. 【经验分享】Web前端开发测试常见问题总结