• javaweb作品---宿舍管理


    技术框架
    数据库:mysql
    服务器:tomcat
    框架:JDBC,druid,javeweb
    前端:html+css+jquery+ajax+jsp
    项目展示:
    登录界面(分为管理员登录和用户–宿管登录;注册)
    在这里插入图片描述
    注册界面:
    在这里插入图片描述
    使用宿管账号采用管理员方式登录会报错
    在这里插入图片描述
    界面内容
    在这里插入图片描述
    在这里插入图片描述
    主要功能
    1 采用管理员方式和宿管方式两种不同登录,并显示不同界面
    2 不同用户登录显示不同用户名
    3 账户注销登录:销毁存储登录信息的session
    4 添加,删除,查询宿舍,学生,楼寓信息
    5 数据库多表查询,模糊查询,条件查询
    6 数据库的逻辑删除和物理删除
    请添加图片描述
    基本思路

    浏览器(用户)
    点击页面中的链接或者按钮携带参数会向服务器发送请求,DNS解析器负责解析url,分析访问的ip地址和应用端口(tomcat默认为8080),然后将请求发送到web服务器。

    服务器
    web服务器加载servlet容器,加载servelet实体类。servelet调用service方法,判断请求方式(默认是get),然后进行方式分配,执行servlet内相应service方法,调用request方法获取相应参数。
    service不同方法调用数据库查询,将结果一步步传回到servlet。最后用request请求转发和reponse或者利用输入流写回到页面。

    页面解析服务器传回数据,修改和添加数据,进行页面的重新渲染。

    dao层连接数据库,并将查询结果包装成实体类进行返回给相应service方法
    
    package com.cainiao.dao.impl;
    
    import com.cainiao.dao.BuildingDao;
    import com.cainiao.pojo.Building;
    import com.cainiao.pojo.Dormitory;
    import com.cainiao.pojo.SystemAdmin;
    import com.cainiao.util.JDBCUtils;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class BuildingDaoImpl implements BuildingDao {
        @Override
        public List<Building> list() throws SQLException {
            Connection connection = JDBCUtils.getConnection();
            String sql = "select b.id,b.name,b.introduction,d.name,d.id from dormitory.dormitory_admin d ,dormitory.building b where b.admin_id =d.id";
            PreparedStatement statement = null; //预编译 防止sql注入
            ResultSet resultSet = null; //结果集
    
            List<Building> list = new ArrayList<>();
            try {
                statement = connection.prepareStatement(sql);
                resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String introduction = resultSet.getString(3);
                    String adminName = resultSet.getString(4);
                    Integer adminId = resultSet.getInt(5);
                    list.add(new Building(id,name,introduction,adminId,adminName));
    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection, statement);
            }
            return list;
        }
    
        @Override
        public List<Building> search(String key, String value) throws SQLException {
            Connection connection = JDBCUtils.getConnection();
            String sql = "select b.id,b.name,b.introduction,d.name,d.id from dormitory.dormitory_admin d ,dormitory.building b where b.admin_id =d.id and b."+key+" like '%"+value+"%' ";
            PreparedStatement statement = null; //预编译 防止sql注入
            ResultSet resultSet = null; //结果集
    
            List<Building> list = new ArrayList<>();
            try {
                statement = connection.prepareStatement(sql);
                resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String introduction = resultSet.getString(3);
                    String adminName = resultSet.getString(4);
                    Integer adminId = resultSet.getInt(5);
                    list.add(new Building(id,name,introduction,adminId,adminName));
    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection, statement);
            }
            return list;
        }
    
        @Override
        public Integer save(String name, String introduction, Integer adminId) throws SQLException {
            Connection connection = JDBCUtils.getConnection();
            String sql = "insert into dormitory.building(name,introduction,admin_id) values(?,?,?) ";
            PreparedStatement statement = null; //预编译 防止sql注入
            Integer result = null;
            try {
                statement = connection.prepareStatement(sql);
                statement.setString(1, name);
                statement.setString(2, introduction);
                statement.setInt(3, adminId);
                result = statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection, statement);
            }
            return result;
        }
    
        @Override
        public Integer update(Integer id, String name, String introduction, Integer adminId) throws SQLException {
            Connection connection = JDBCUtils.getConnection();
            String sql = "update dormitory.building set name=?,introduction=?,admin_id=? where id= ?";
            PreparedStatement statement = null; //预编译 防止sql注入
            Integer result = null;
            try {
                statement = connection.prepareStatement(sql);
                statement.setString(1,name);
                statement.setString(2, introduction);
                statement.setInt(3, adminId);
                statement.setInt(4, id);
                result = statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection, statement);
            }
            return result;
        }
    
        @Override
        public Integer delete(Integer id) throws SQLException {
            Connection connection = JDBCUtils.getConnection();
            String sql = "delete  from dormitory.building  where id= ?";
            PreparedStatement statement = null; //预编译 防止sql注入
            Integer result = null;
            try {
                statement = connection.prepareStatement(sql);
                statement.setInt(1,id);
                result = statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(connection, statement);
            }
            return result;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    service方法:业务层 负责调用不同方法
    package com.cainiao.service.impl;
    
    import com.cainiao.dao.BuildingDao;
    import com.cainiao.dao.DormitoryDao;
    import com.cainiao.dao.StudentManagerDao;
    import com.cainiao.dao.impl.BuildingDaoImpl;
    import com.cainiao.dao.impl.DormitoryDaoImpl;
    import com.cainiao.dao.impl.StudentManagerDaoImpl;
    import com.cainiao.pojo.Building;
    import com.cainiao.pojo.Dormitory;
    import com.cainiao.service.BuildingService;
    
    import java.sql.SQLException;
    import java.util.List;
    
    public class BuildingServiceImpl implements BuildingService {
        private  BuildingDao buildingDao = new BuildingDaoImpl();
        private DormitoryDao dormitoryDao =new DormitoryDaoImpl();
        private StudentManagerDao studentManagerDao = new StudentManagerDaoImpl();
        @Override
        public List<Building> list() throws SQLException {
            return this.buildingDao.list();
        }
    
        @Override
        public List<Building> search(String key, String value) throws SQLException {
            if (value.equals("")) return this.buildingDao.list();
            return this.buildingDao.search(key, value);
        }
    
        @Override
        public void save(String name, String introduction, Integer adminId) throws SQLException {
            Integer save = this.buildingDao.save(name,introduction,adminId);
            if (save!=1) {
                throw new RuntimeException("宿管信息添加失败");
            }
        }
    
        @Override
        public void update(Integer id, String name, String introduction, Integer adminId) throws SQLException {
            Integer update = this.buildingDao.update(id, name, introduction, adminId);
            if (update != 1) throw new RuntimeException("更新樓宇信息失敗");
        }
    
    
        @Override
        public void delete(Integer id) throws SQLException {
            //學生換宿舍
             List<Integer> dormitoryIdList = this.dormitoryDao.findDormitoryIdByBuildingId(id);//通过宿舍楼找里面的所有宿舍
            for (Integer dormitoryId : dormitoryIdList) {  //遍历每个宿舍
                List<Integer> studentManagerIdList = this.studentManagerDao.findStudentManagerByDormitoryId(dormitoryId); //通过宿舍找宿舍里的所有学生
                for (Integer studentManagerId : studentManagerIdList) { //对学生遍历
                    Integer availableId = this.dormitoryDao.availableId(); //找到所有空宿舍
                    Integer updateDormitory = this.studentManagerDao.updateDormitory(studentManagerId, availableId); //把学上调到有空宿舍
                    Integer subAvailable = this.dormitoryDao.subAvailable(availableId); //有空宿舍减去一个空床位
                    if (updateDormitory != 1 || subAvailable != 1) throw new RuntimeException("学生更新宿舍失败");
                }
            }
            // 删除宿舍
            for (Integer integer : dormitoryIdList) {
                 Integer delete = this.dormitoryDao.delete(integer);
                if (delete != 1) throw new RuntimeException("宿舍信息删除失敗");
            }
            //删除楼宇
            Integer delete = this.buildingDao.delete(id);
            if (delete !=1 ) throw new RuntimeException("楼宇信息删除失败");
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    控制层:负责接收页面请求数据,然后进行方法分发,执行不同service方法
    
    package com.cainiao.web;
    
    import com.cainiao.dto.DormitoryAdminDto;
    import com.cainiao.dto.SystemAdminDto;
    import com.cainiao.service.DormitoryAdminService;
    import com.cainiao.service.impl.DormitoryAdminServiceImpl;
    import com.cainiao.service.impl.SystemAdminServiceImpl;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.Map;
    
    @WebServlet("/account")
    public class AccountServlet extends HttpServlet {
        SystemAdminServiceImpl systemAdminService = new SystemAdminServiceImpl();
        DormitoryAdminService dormitoryAdminService = new DormitoryAdminServiceImpl();
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");  //设置编码格式,防止中文乱码
            String type = request.getParameter("type"); //获取登录账户类型
            System.out.println(type);
    //        String registerType = request.getParameter("registerType");//获取注册账户类型
    //        System.out.println(registerType);
            String method =request.getParameter("method");
            String username = request.getParameter("username"); //获取登录界面账号
            String password = request.getParameter("password"); //获取登录界面密码
    
            String dormitoryUsername = request.getParameter("dormitoryUsername"); //获取注册界面账号
            String dormitoryPassword = request.getParameter("dormitoryPassword"); //获取注册界面密码
            String dormitoryName = request.getParameter("dormitoryName"); //获取注册界面姓名
            String dormitoryGender = request.getParameter("dormitoryGender"); //获取注册界面性别
            System.out.println(dormitoryGender);
            String dormitoryTelephone = request.getParameter("dormitoryTelephone"); //获取注册界面电话
            /*对登录用户类型进行判断*/
            if(type.equals("systemAdmin")){  //管理员类型管理员
                switch(method){
                    case "logout":
                        request.getSession().invalidate();  //销毁session,跳转页面
                        response.sendRedirect("login.jsp");
                        break;
                    case "login":
                        try {
                            SystemAdminDto systemAdminDto = systemAdminService.login(username, password);
                            //判断用户名是否存在,进行信息反馈
                            switch(systemAdminDto.getCode()){
                                case -1:
                                    request.setAttribute("usernameError","用户名不存在");
                                    request.getRequestDispatcher("login.jsp").forward(request,response);
                                    break;
                                case 0:
                                    request.setAttribute("passwordError","密码错误");
                                    request.getRequestDispatcher("login.jsp").forward(request,response);
                                    break;
                                case 1:
                                    System.out.println("登录成功");
                                    HttpSession session = request.getSession();
                                    session.setAttribute("systemAdmin",systemAdminDto.getSystemAdmin());
                                    session.setAttribute("type",type);
                                    response.sendRedirect("systemadmin.jsp");
                                    break;
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }else{                       // 楼管类型管理员
                switch(method){
                    case "dormitoryCheckUsername": //用ajax异步核对是否存在重复账号
                        try {
                            DormitoryAdminDto dormitoryAdminDto = systemAdminService.dormitoryCheckUsername(dormitoryUsername);
                            //判断用户名是否存在,进行信息反馈
                            switch(dormitoryAdminDto.getCode()){
    //                       case -1:
    //                         request.setAttribute("usernameError","用户名存在");
    //                         response.getWriter().write("-1");
                             request.getRequestDispatcher("login.jsp").forward(request,response);
    //                         break;
                                case 0:
                                    response.getWriter().write("0");
    //                           request.getRequestDispatcher("login.jsp").forward(request,response);
                                    break;
                                case 1:
                                    response.getWriter().write("1");
                                    break;
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        break;
                    case "dormitorySave": //把注册好的账号进行保存
                        try {
                            Integer integer = systemAdminService.dormitorySave(dormitoryUsername, dormitoryPassword, dormitoryName, dormitoryGender, dormitoryTelephone);
                            response.getWriter().write("注册成功,请登录");
                            response.sendRedirect("login.jsp");
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        break;
                    case "login": //楼管登录
                        try {
                            DormitoryAdminDto login = dormitoryAdminService.login(username, password);
                            //判断用户名是否存在,进行信息反馈
                            switch(login.getCode()){
                                case -1:
                                    request.setAttribute("usernameError","用户名不存在");
                                    request.getRequestDispatcher("login.jsp").forward(request,response);
                                    break;
                                case 0:
                                    request.setAttribute("passwordError","密码错误");
                                    request.getRequestDispatcher("login.jsp").forward(request,response);
                                    break;
                                case 1:
                                    HttpSession session = request.getSession();
                                    session.setAttribute("dormitoryAdmin",login.getDormitoryAdmin());
                                    session.setAttribute("type","dormitoryAdmin");
                                    response.sendRedirect("systemadmin.jsp");
                                    break;
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        break;
                    case "logout":
                        request.getSession().invalidate();  //销毁session,跳转页面
                        response.sendRedirect("login.jsp");
                        break;
    
                }
            }
    
    //        System.out.println(request.getContextPath());
    
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
         }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    页面视图层view
    
    //点击登录按钮将表单数据以post方式发送到名字为account的servlet那里
     <form class="form-horizontal col-md-offset-3" id="login_form" action="<%= request.getContextPath()%>/account?method=login" method="post">  <%--request.getContextPath() /web-demo --%>
          <h3 class="form-title">用户登录</h3>
          <div class="col-md-9">
            <div class="form-group">
              <i class="fa fa-user fa-lg"></i>
              <span style="color: red;font-size: 13px;margin-left: -17px;">${usernameError}</span>
              <input class="form-control required" required placeholder="请输入用户名" type="text" name="username"/>
            </div>
            <div class="form-group">
              <i class="fa fa-lock fa-lg"></i>
              <span style="color: red;font-size: 13px;margin-left: -17px;">${passwordError}</span>
              <input class="form-control required" required placeholder="请输入密码" type="password" name="password"/>
            </div>
            <div class="form-group">
              <label class="radio-inline">
                <input type="radio" name="type" checked value="systemAdmin" class="radio-inline"> 系统管理员
              </label>
              <label class="radio-inline">
                <input type="radio" name="type" value="dormitoryAdmin" class="radio-inline" > 宿舍管理员
              </label>
            </div>
            <div class="form-group col-md-offset-9">
              <button type="submit" class="btn btn-success pull-left" name="submit">登录</button>
              <button type="reset" class="btn btn-success pull-right" name="submit">重置</button>
              <a href="register.html" style="cursor: pointer;font-size: small">没有账号,点击注册</a>
            </div>
          </div>
        </form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    Git下载源码地址

  • 相关阅读:
    燕京啤酒何以至此?
    ​力扣解法汇总790. 多米诺和托米诺平铺
    Python学习之-分支语句-基础训练
    Linux入门教程:P13->磁盘管理类
    .NET性能优化-是时候换个序列化协议了
    Matlab/Simulink Coder: 代码生成中的数据处理类型控制
    wangshusen学习笔记
    胆囊结石的后果
    Lum proxy代理IP如何在AdsPower浏览器上使用
    Linux -- 进阶 Web服务器 搭建基于 https 协议的静态网站 ( 预备知识 )
  • 原文地址:https://blog.csdn.net/m0_64288219/article/details/127688836