javaweb简化的医院管理系统
醫院管理系統
源碼: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 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=1234563306是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 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简化的医院管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DHCP的原理与配置
- 下一篇: 轻博客只是大众喜新厌旧的产物