概述
学完了servlet、数据库、以及之前的jsp。我们可以 做一个管理系统,将知识综合起来运用!
一、创建数据库,表名 easyui。
二、实现增删改查。
1.新建web项目,导入所需要的4个包
2.包的分层 工具包将之前的复制过来就好了。
dao部分 创建 接口和 实现类。实现类在dao包下新建包impl。
package com.aaa.dao; import com.aaa.entity.Employee; import java.util.List; import java.util.Map; public interface IDengDAO { boolean goDeng(String username,String password); //查询所有员工 List<Map<String,Object>> getAll(); //添加员工的方法 boolean add(Employee emp); //删除员工的方法 boolean delete(int id); //修改员工的方法 boolean update(Employee emp); //根据id 获取员工 Map<String ,Object>getEmp(int id); }
impl部分
package com.aaa.dao.Impl; import com.aaa.dao.IDengDAO; import com.aaa.entity.Employee; import com.aaa.util.DBUtil; import java.util.List; import java.util.Map; public class DengDAOImpl implements IDengDAO { @Override public boolean goDeng(String username, String password) { String sql="select * from login where username=? and password=?"; List<Map<String, Object>> list = DBUtil.executeQuery(sql, username, password); return list.size()>0; } @Override public List<Map<String, Object>> getAll() { String sql = "select e.id,e.name,e.telephone,e.hiredate,e.state,d.name dname,r.name rname from employee e INNER JOIN department d on e.deptID = d.id INNER JOIN role r on e.roleID = r.id"; return DBUtil.executeQuery(sql); } @Override public boolean add(Employee emp) { String sql = "insert into employee (name,telephone,deptID,roleID,state) values (?,?,?,?,?)"; return DBUtil.executeUpdate(sql,emp.getName(),emp.getTelephone(),emp.getDeptID(),emp.getRoleID(),emp.getState()); } @Override public boolean delete(int id) { String sql = "delete from employee where id = ?"; return DBUtil.executeUpdate(sql, id); } @Override public boolean update(Employee emp) { String sql = "update employee set name = ? , telephone = ? , deptID=? , roleID=? ,state = ? where id = ?"; return DBUtil.executeUpdate(sql,emp.getName(),emp.getTelephone(),emp.getDeptID(),emp.getRoleID(),emp.getState(),emp.getId()); } @Override public Map<String, Object> getEmp(int id) { String sql = "select * from employee where id = ?"; List<Map<String, Object>> list = DBUtil.executeQuery(sql, id); if(list.size()>0){ return list.get(0); } return null; } }
entity实体类部分
package com.aaa.entity; public class Employee { private int id; private String name; private String telephone; private int deptID; private int roleID; private int state; public Employee() { } public Employee(int id, String name, String telephone, int deptID, int roleID, int state) { this.id = id; this.name = name; this.telephone = telephone; this.deptID = deptID; this.roleID = roleID; this.state = state; } public Employee(String name, String telephone, int deptID, int roleID, int state) { this.name = name; this.telephone = telephone; this.deptID = deptID; this.roleID = roleID; this.state = state; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public int getDeptID() { return deptID; } public void setDeptID(int deptID) { this.deptID = deptID; } public int getRoleID() { return roleID; } public void setRoleID(int roleID) { this.roleID = roleID; } public int getState() { return state; } public void setState(int state) { this.state = state; } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + ''' + ", telephone='" + telephone + ''' + ", deptID=" + deptID + ", roleID=" + roleID + ", state=" + state + '}'; } }
3.创建登录界面index.jsp,在web目下。并创建LoginServlet 进行业务操作。
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/4/18 Time: 20:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%-- 此时 这里仅仅只是登录界面 不通过servlet 用户直接在浏览器中访问index.jsp --%> <title>用户登录界面</title> <style> fieldset{width: 300px;margin: 100px auto;} </style> </head> <body> <fieldset> <legend>用户登录</legend> <%--密码错误 通过 error 关键字 获得 错误提示信息--%> <div style="color: red">${error}</div> <%--提交的地址 是我们所写的loginServlet 注意路径格式--%> <form action="/zxf/deng" method="post"> <div> 姓名:<input type="type" name="username"/> </div> <div> 密码: <input type="password" name="password"/> </div> <button >登录</button> </form> </fieldset> </body> </html>
package com.aaa.servelt; import com.aaa.dao.IDengDAO; import com.aaa.dao.Impl.DengDAOImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/deng") //虚拟路径 form表单的提交地址 /zxf/deng public class LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取 我们在登录界面 输入的密码和用户名 String username = req.getParameter("username"); String password = req.getParameter("password"); //2.调用dao方法进行验证 IDengDAO dao=new DengDAOImpl(); boolean goDeng = dao.goDeng(username, password); //3.判断密码 是否正确 if (goDeng){ //4.正确 重定向 到main.jsp页面 resp.sendRedirect("/zxf/main.jsp"); }else { //5.错误 共享数据 请求转发到 index.jsp req.setAttribute("error","账号密码错误,请重试!"); req.getRequestDispatcher("/index.jsp").forward(req,resp); } } }
登录不成功 会提示错误。登陆成功,就跳转到main.jsp部分
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/4/18 Time: 20:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>员工数据库系统</title> <style> ul,iframe{ float: left; } ul{ width: 15%; } iframe{ width: 80%; } </style> </head> <body> <h1>欢迎来到员工管理系统</h1> <div> <ul> <li><a href="/zxf/emp" target="show">员工管理</a></li> <li><a href="/zxf/add.jsp" target="show">添加员工</a></li> </ul> <iframe name="show" height="800px"></iframe> </div> </body> </html>
点击员工管理,跳转到
package com.aaa.servelt; import com.aaa.dao.IDengDAO; import com.aaa.dao.Impl.DengDAOImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; @WebServlet("/emp") //main.jsp 的员工管理 跳转到这 public class EmpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取所有数据 IDengDAO dao=new DengDAOImpl(); List<Map<String, Object>> list = dao.getAll(); //共享数据 req.setAttribute("list",list); //请求转发 req.getRequestDispatcher("/emp.jsp").forward(req,resp); } }
转发到 emp.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/4/19 Time: 16:37 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <table border="1" cellspacing="0" width="95%" align="center"> <tr> <th>员工编号</th> <th>员工姓名</th> <th>员工手机号</th> <th>入职时间</th> <th>员工部门</th> <th>员工角色</th> <th>员工状态</th> <th>员工操作</th> </tr> <c:forEach var="emp" items="${list}"> <tr> <th>${emp.id}</th> <th>${emp.name}</th> <th>${emp.telephone}</th> <th>${emp.hiredate}</th> <th>${emp.dname}</th> <th>${emp.rname}</th> <th>${emp.state==1?"在职":"离职"}</th> <th> <%-- 修改和删除 有各自要跳转的servlet页面--%> <a href="/zxf/delete?id=${emp.id}">删除</a> <a href="/zxf/toUpdate?id=${emp.id}">修改</a> </th> </tr> </c:forEach> </table> </body> </html>
点节删除 跳转到DeleteEmpServlet
package com.aaa.servelt; import com.aaa.dao.IDengDAO; import com.aaa.dao.Impl.DengDAOImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/delete") public class DeleteEmpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求参数 String id = req.getParameter("id"); //调用dao 根据id删除员工。 IDengDAO dao=new DengDAOImpl(); boolean delete = dao.delete(Integer.parseInt(id)); //重定向到 emp resp.sendRedirect("/zxf/emp"); } }
点击修改员工 ,跳转到ToUpdateServlet
package com.aaa.servelt; import com.aaa.dao.IDengDAO; import com.aaa.dao.Impl.DengDAOImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; @WebServlet("/toUpdate") /* 修改 用户点击修改按钮请求到servlet 先要获取 要修改的员工信息 共享给jsp 请求转发过去 展示给用户看 */ public class ToUpdate extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取参数 String id = req.getParameter("id"); //根据ID获取员工信息 IDengDAO dao=new DengDAOImpl(); Map<String, Object> emp= dao.getEmp(Integer.parseInt(id)); //共享员工信息 req.setAttribute("emp",emp); //请求转发 req.getRequestDispatcher("/update.jsp").forward(req,resp); } }
他会请求转发到 update.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/4/18 Time: 9:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改</title> <style type="text/css"> fieldset{width: 300px;margin: 100px auto;} </style> </head> <body> <fieldset id=""> <legend>修改员工</legend> <form action="/zxf/update" method="post"> <input type="hidden" name="id" value="${emp.id}" /> <div> 员工姓名: <input name="name" value="${emp.name}" /> </div> <div> 员工手机: <input name="telephone" value="${emp.telephone}" /> </div> <div> 员工部门: <select name="deptID" id="deptID" > <option value="1" ${emp.deptID==1?"selected":""} >总经办</option> <option value="2" ${emp.deptID==2?"selected":""} >人事部</option> <option value="3" ${emp.deptID==3?"selected":""} >业务部</option> </select> </div> <div> 员工角色: <select name="roleID" id="roleID" > <option value="1" id="r1">超级管理员</option> <option value="2" id="r2">员工管理员</option> <option value="3" id="r3">业务管理员</option> </select> </div> <div> 员工状态: <select name="state" > <option value="0" id="s0">离职</option> <option value="1" id="s1">在职</option> </select> </div> <button>修改</button> </form> </fieldset> <script> // 当前表单的表单元素 够不够 // 让 角色下拉框中的 1 2 3 document.getElementById("r"+${emp.roleID}).selected = "selected"; document.getElementById("s"+${emp.state}).selected = "selected"; </script> </body> </html>
这里的jsp页面会跳转到UpdateServlet
package com.aaa.servelt; import com.aaa.dao.IDengDAO; import com.aaa.dao.Impl.DengDAOImpl; import com.aaa.entity.Employee; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/update") public class UpdateEmpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求参数 处理请求乱码 req.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); // 1 获取请求参数 String name = req.getParameter("name"); String telephone = req.getParameter("telephone"); String deptID = req.getParameter("deptID"); String roleID = req.getParameter("roleID"); String state = req.getParameter("state"); String id = req.getParameter("id"); Employee employee = new Employee(Integer.parseInt(id), name, telephone, Integer.parseInt(deptID), Integer.parseInt(roleID), Integer.parseInt(state)); //2 通过dao将数据添加到数据库中 IDengDAO dao=new DengDAOImpl(); boolean update = dao.update(employee); //3 重定向到/emp resp.sendRedirect("/zxf/emp"); } }
他又回到了emp 就是EmpServlet。
到此 删除和修改的功能完成。
回到 man.jsp 点击添加 员工。他会跳转到 add.jsp 页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/4/17 Time: 11:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style type="text/css"> fieldset{width: 300px;margin: 100px auto;} </style> </head> <body> <fieldset id=""> <legend>添加员工</legend> <%-- 跳转到 /zxf/addEmp 这个servlet--%> <form action="/zxf/addEmp" method="post"> <div> 员工姓名: <input name="name" /> </div> <div> 员工手机: <input name="telephone" /> </div> <div> 员工部门: <select name="deptID"> <option value="1">总经办</option> <option value="2">人事部</option> <option value="3">业务部</option> </select> </div> <div> 员工角色: <select name="roleID"> <option value="1">超级管理员</option> <option value="2">员工管理员</option> <option value="3">业务管理员</option> </select> </div> <div> 员工状态: <select name="state"> <option value="0">离职</option> <option value="1">在职</option> </select> </div> <button>添加</button> </form> </fieldset> </body> </html>
这里的jsp 会跳转到AddServlet
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/addEmp") public class AddEmpServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //处理1.请求乱码---->浏览器发给 servlet 乱码 // 2.响应乱码----->servlet 回传给浏览器之后乱码 不用处理 为啥? 因为我们将其交给了 jsp 来解决! req.setCharacterEncoding("utf-8"); // 1 获取请求参数 String name = req.getParameter("name"); String telephone = req.getParameter("telephone"); String deptID = req.getParameter("deptID"); String roleID = req.getParameter("roleID"); String state = req.getParameter("state"); Employee employee = new Employee(name,telephone,Integer.parseInt(deptID),Integer.parseInt(roleID),Integer.parseInt(state)); // 2 调用dao 将数据添加到数据库中 IDengDAO dao=new DengDAOImpl(); dao.add(employee); // 3 回传信息 成功/失败 req.setAttribute("msg","添加成功"); // 4 请求转发到 add.jsp req.getRequestDispatcher("/add.jsp").forward(req,resp); } }
这里会将数据 传送给add.jsp。
好了,添加员工完成! 增删改查实现,舒服。
三、总结。
1.jsp是用来展示数据的
2.servlet 是用来进行业务操作的 请求转发和重定向。
3.bug虽然痛苦,但自己解决 的时候,也很开心。-------菜鸟的心得!
四,展示所有的文件目录
转载于:https://www.cnblogs.com/ZXF6/p/10738961.html
最后
以上就是震动音响为你收集整理的员工管理系统 ,实现增删改查!的全部内容,希望文章能够帮你解决员工管理系统 ,实现增删改查!所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复