日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

javaweb简化的医院管理系统

發(fā)布時(shí)間:2023/12/15 windows 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javaweb简化的医院管理系统 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

醫(yī)院管理系統(tǒng)

源碼:https://pan.baidu.com/s/1wV8Sz_lpR5WSIHH2u0cG8w 提取碼:6q9n
數(shù)據(jù)庫(kù)文件:https://pan.baidu.com/s/16EfM41xYniXELWRT-ZANcg 提取碼:dhm5
這里用javaweb進(jìn)行數(shù)據(jù)庫(kù)的增刪改查操作,只是一個(gè)極其簡(jiǎn)單的程序,之所以叫醫(yī)院管理系統(tǒng),因?yàn)槊质抢蠋煻ǖ?#xff0c;核心跟學(xué)生管理系統(tǒng)等一樣。數(shù)據(jù)庫(kù)連接也沒用到連接池,網(wǎng)頁(yè)前端的制作也沒有編寫,這里只是后端代碼實(shí)現(xiàn)簡(jiǎn)單的增刪改查功能。建議javaweb初學(xué)者學(xué)習(xí)。

思路

首先拿到這樣的題目,應(yīng)該先思考需要實(shí)現(xiàn)哪些功能,先不急著敲代碼。對(duì)于管理系統(tǒng),增刪改查是肯定要的,醫(yī)院系統(tǒng),需要把醫(yī)院醫(yī)生信息和病人信息分隔開避免混淆。然后再做一下界面就滿足老師的要求了。

1、數(shù)據(jù)庫(kù)的建立

創(chuàng)建醫(yī)院醫(yī)生信息表和病人信息表,由于只是需要一些增刪改查功能,我建的表很簡(jiǎn)單,醫(yī)生表(用戶名和密碼),病人表(姓名和床位)。(沒有設(shè)置主鍵,只有字段,簡(jiǎn)易展示一下)

創(chuàng)建各個(gè)包

這個(gè)看個(gè)人習(xí)慣,有點(diǎn)人喜歡先寫前端代碼再寫后端,有的人喜歡先寫后端數(shù)據(jù)庫(kù)代碼,再編寫前端,我這里是從數(shù)據(jù)庫(kù)開始編寫的(后者),先創(chuàng)了多個(gè)包和一個(gè).properties

說明一下每個(gè)包的作用

  • .bean:存放一些實(shí)體類
  • .dao:存放數(shù)據(jù)訪問的接口
  • .dao.impl:數(shù)據(jù)訪問接口的實(shí)現(xiàn)類
  • .service:業(yè)務(wù)邏輯接口
  • .servlet.impl:業(yè)務(wù)邏輯的接口實(shí)現(xiàn)類
  • .servlet:web層代碼
  • .util:存放工具類
  • .properties: 數(shù)據(jù)庫(kù)連接文件

如果你是一個(gè)新手,你看到這可能會(huì)一頭霧水,當(dāng)然在厲害的程序員看來,這只是很基本的開發(fā)模式而已。為什么要用這個(gè)開發(fā)模式呢,這個(gè)開發(fā)模式就是簡(jiǎn)易的MVC開發(fā)模式。

  • MVC模式的優(yōu)點(diǎn):降低各個(gè)模塊之間的耦合,能夠增強(qiáng)程序的可維護(hù)性和可擴(kuò)展性,提高了模型的復(fù)用性。
  • MVC模式的缺點(diǎn):增加了程序源碼的復(fù)雜性。

利用分層的思想將不同的模塊內(nèi)容分開,可以方便軟件開發(fā)人員分工協(xié)作,提高開發(fā)效率。

編寫實(shí)體類

分別編寫病人和醫(yī)生的實(shí)體類
病人

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;}}

醫(yī)生

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;} }

沒什么好解釋的,跟數(shù)據(jù)庫(kù)的字段對(duì)應(yīng)就可以了

數(shù)據(jù)庫(kù)的連接

JDBC規(guī)范在jdk中的
java.sql.;
javax.sql.;
這兩個(gè)包下面,不過這里面都是接口,要想使用JDBC的話,需要下載相關(guān)的數(shù)據(jù)庫(kù)驅(qū)動(dòng)jar包,這里咱們使用的是MySQL數(shù)據(jù)庫(kù),所以需要下載MySQL的數(shù)據(jù)庫(kù)驅(qū)動(dòng):網(wǎng)盤地址。
JDBC的四個(gè)核心接口:

  • DriverManager:用于注冊(cè)驅(qū)動(dòng)并創(chuàng)建符合該驅(qū)動(dòng)的數(shù)據(jù)庫(kù)的連接。
  • Connection: 表示與數(shù)據(jù)庫(kù)創(chuàng)建的連接對(duì)象,即一個(gè)connection對(duì)應(yīng)著一個(gè)會(huì)話,相當(dāng)于在mysql workbench中打開了一個(gè)連接。
  • Statement: 操作數(shù)據(jù)庫(kù)sql語(yǔ)句的對(duì)象,有個(gè)兩個(gè)實(shí)現(xiàn)類:Statement和PreparedStatement(常用)。
  • ResultSet: 從數(shù)據(jù)庫(kù)中查詢的結(jié)果集。

基本上通過使用上面4個(gè)接口就能使用java實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查了。

為了提高可維護(hù)性,可以將這些經(jīng)常變換內(nèi)容寫到一個(gè)配置文件中,這里創(chuàng)建一個(gè)名為db.properties的文件:

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

3306是Mysql的默認(rèn)端口,hospital是數(shù)據(jù)庫(kù)的名字
username是數(shù)據(jù)庫(kù)的用戶名,一般為root
password是數(shù)據(jù)庫(kù)的密碼,輸入你的密碼就可以了

創(chuàng)建數(shù)據(jù)庫(kù)連接工具類

創(chuàng)建一個(gè)DBUtil的工具類,在這個(gè)類中注冊(cè)驅(qū)動(dòng)和獲取連接:

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常用接口簡(jiǎn)單介紹一下

  • DriverManager:該類的主要作用就是創(chuàng)建連接(不同的數(shù)據(jù)庫(kù),在forName中的參數(shù)寫法不同)
  • Statement:該接口的作用是操作sql語(yǔ)句,并返回相應(yīng)結(jié)果的對(duì)象
  • ResultSet:該接口的作用主要用來封裝結(jié)果集。
    數(shù)據(jù)庫(kù)的就不多說了,如果實(shí)在看不懂或者有問題,可以自行百度多看看大佬的文章。

編寫Dao

主要實(shí)現(xiàn)的功能都寫在這里,方便我們?nèi)蘸蟠a的調(diào)試.
醫(yī)院管理員:

public interface UserDao {//醫(yī)院管理員注冊(cè)public void addUser(User user) throws Exception;//醫(yī)生管理員登錄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;//通過床號(hào)判斷是否有人public int findId(Patient patient)throws Exception; }

注釋寫的很明白了,這里兩個(gè)都是接口。接下來寫實(shí)現(xiàn)類。

.dao.impl實(shí)現(xiàn)類的編寫

病人:

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;}

醫(yī)生:

//注冊(cè)醫(yī)院管理員@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();}}//醫(yī)院管理員登錄@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;}

看起來代碼挺多的,其實(shí)都是些差不多的重復(fù)性代碼。拿查看病人為例,步驟如下:

  • 創(chuàng)建Connection對(duì)象、PreparedStatement對(duì)象、ResultSet對(duì)象。
  • 創(chuàng)建Map<Integer,Patient>用來接收返回的數(shù)據(jù)
  • 編寫原生SQL:“select * from patient”
  • 創(chuàng)建連接:conn = DButil.getConnection();
  • 用PreparedStatement對(duì)象執(zhí)行SQL語(yǔ)句
  • 然后用ResultSet對(duì)象接收結(jié)果。
  • 用while語(yǔ)句循環(huán),把結(jié)果放到我們的Map中。返回Map

再比如添加病人這段:

  • 創(chuàng)建Connection對(duì)象、PreparedStatement對(duì)象
  • 編寫原生SQL:insert patient(name,id) value (?,?)由于這里的查詢條件需要我們網(wǎng)頁(yè)輸入的信息來判斷,約束條件有?代替。
  • PreparedStatement對(duì)象的executeUpdate()方法來更新數(shù)據(jù)庫(kù)。

其他的不說了,差不多的代碼,就SQL語(yǔ)句不同,返回的值不同而已。

邏輯層代碼

醫(yī)生:

public interface UserService {//醫(yī)院管理員注冊(cè)public void addUser(User user)throws Exception;//醫(yī)院管理員登錄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;//查找床號(hào)public int findId(Patient patient)throws Exception; }

這里的代碼和Dao里的代碼完全一樣。就不說了。

邏輯層實(shí)現(xiàn)類

醫(yī)生:

public class UserServiceImpl implements UserService {UserDao u = new UserDaoImpl();//醫(yī)院管理員登錄@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);}}

這里的實(shí)現(xiàn)類相信你應(yīng)該能看明白,因?yàn)榫蛣?chuàng)建了之前數(shù)據(jù)層的接口實(shí)現(xiàn)類的實(shí)例,然后調(diào)用其方法而已。有一點(diǎn)要注意:因?yàn)槭墙涌?#xff0c;所以接口在實(shí)例化的時(shí)候跟類不一樣:

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

Servlet類的編寫

為了方便觀看學(xué)習(xí)研究,我把增刪改查做了4個(gè)servlet,來分別實(shí)現(xiàn)其功能。
由于前段代碼過于簡(jiǎn)陋就不貼了,無非就幾個(gè)表單。

醫(yī)院管理員注冊(cè)

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方法,由于還沒有寫字符編碼過濾器,先確定編碼,然后用一個(gè)User對(duì)象接受表單數(shù)據(jù)。實(shí)現(xiàn)業(yè)務(wù)邏輯層代碼的實(shí)例:

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

用us的addUser()方法,把注冊(cè)的醫(yī)生放到數(shù)據(jù)庫(kù)中
用ps的Look()方法,把目前的病人信息展示給這名醫(yī)生。
最后返回jsp顯示到頁(yè)面上。

醫(yī)院管理員登錄

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);//分發(fā)轉(zhuǎn)向if(u!=null){//如果登錄成功,就把user對(duì)象放到session對(duì)象中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();}}

剛剛是注冊(cè),現(xiàn)在是登錄,前面基本差不多,不過需要findUserByNameAndPassword()方法先判斷一下該用戶是否已經(jīng)注冊(cè)過

  • 如果沒有,頁(yè)面會(huì)返回,提示登錄失敗。
  • 如果有,登錄成功,再把病人信息調(diào)出來顯示

添加病人

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();}}

接受表單數(shù)據(jù)后,需要先去數(shù)據(jù)庫(kù)中查詢,是否存在這位病人,根據(jù)床號(hào)是否空余給病人安排床號(hào)。添加成功后重新調(diào)用PatientService的Look()方法,刷新病人信息反饋給醫(yī)生

刪除病人

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();}}

如果你看懂了前面幾個(gè),這個(gè)你肯定不難看出其原理。這個(gè)刪除功能就留給你思考吧。

總結(jié)

這樣簡(jiǎn)單的項(xiàng)目就寫完了。當(dāng)然只適合給沒有多少基礎(chǔ)的javaweb初學(xué)者參考。因?yàn)檫@里的代碼都是很簡(jiǎn)易的版本,實(shí)際應(yīng)用比這個(gè)要復(fù)雜的多的多。但是完整的敲完這些代碼也能讓我們更好的體會(huì)到MVC這種開發(fā)模式。對(duì)分層思想有更好的體悟。

一些問題

  • 當(dāng)請(qǐng)求request中攜帶了用戶提交的數(shù)據(jù)時(shí),需要將這些數(shù)據(jù)封裝到JavaBean中,像之前寫法需要一一賦值,倘若request攜帶了非常多的表單數(shù)據(jù),此時(shí)的賦值操作就顯得比較繁瑣了,那有沒有好的解決方法呢?這里可以使用apache的commons-beanutils搞定這個(gè)問題。
  • 密碼在儲(chǔ)存過程中換需要加密過程。
  • 在項(xiàng)目中還需要加入權(quán)限代碼的編寫,和過濾器的使用。

總結(jié)

以上是生活随笔為你收集整理的javaweb简化的医院管理系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。