部门员工表操作 多项选择并删除

##实体类
#1. Emp.java

package com.neu.entity;

import java.util.Date;

public class Emp {
	private Integer empno;
	private String ename;
	private Date hiredate;
	private String job;
	private Integer mgr;
	private Double sal;
	private Double comm;
	private Dept dept;
	public Emp() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Emp(Integer empno, String ename, Date hiredate, String job, Integer mgr, Double sal, Double comm,
			Dept dept) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.hiredate = hiredate;
		this.job = job;
		this.mgr = mgr;
		this.sal = sal;
		this.comm = comm;
		this.dept = dept;
	}
	public Integer getEmpno() {
		return empno;
	}
	public void setEmpno(Integer empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public Integer getMgr() {
		return mgr;
	}
	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}
	public Double getSal() {
		return sal;
	}
	public void setSal(Double sal) {
		this.sal = sal;
	}
	public Double getComm() {
		return comm;
	}
	public void setComm(Double comm) {
		this.comm = comm;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((comm == null) ? 0 : comm.hashCode());
		result = prime * result + ((dept == null) ? 0 : dept.hashCode());
		result = prime * result + ((empno == null) ? 0 : empno.hashCode());
		result = prime * result + ((ename == null) ? 0 : ename.hashCode());
		result = prime * result + ((hiredate == null) ? 0 : hiredate.hashCode());
		result = prime * result + ((job == null) ? 0 : job.hashCode());
		result = prime * result + ((mgr == null) ? 0 : mgr.hashCode());
		result = prime * result + ((sal == null) ? 0 : sal.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Emp other = (Emp) obj;
		if (comm == null) {
			if (other.comm != null)
				return false;
		} else if (!comm.equals(other.comm))
			return false;
		if (dept == null) {
			if (other.dept != null)
				return false;
		} else if (!dept.equals(other.dept))
			return false;
		if (empno == null) {
			if (other.empno != null)
				return false;
		} else if (!empno.equals(other.empno))
			return false;
		if (ename == null) {
			if (other.ename != null)
				return false;
		} else if (!ename.equals(other.ename))
			return false;
		if (hiredate == null) {
			if (other.hiredate != null)
				return false;
		} else if (!hiredate.equals(other.hiredate))
			return false;
		if (job == null) {
			if (other.job != null)
				return false;
		} else if (!job.equals(other.job))
			return false;
		if (mgr == null) {
			if (other.mgr != null)
				return false;
		} else if (!mgr.equals(other.mgr))
			return false;
		if (sal == null) {
			if (other.sal != null)
				return false;
		} else if (!sal.equals(other.sal))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", hiredate=" + hiredate + ", job=" + job + ", mgr=" + mgr
				+ ", sal=" + sal + ", comm=" + comm + ", dept=" + dept + "]";
	}
}

#2.Dept.java

package com.neu.entity;

public class Dept {
	private Integer deptno;
	private String dname;
	private String loc;
	public Dept() {
		super();
		// TODO 自动生成的构造函数存根
	}
	public Dept(Integer deptno, String dname, String loc) {
		super();
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}
	public Integer getDeptno() {
		return deptno;
	}
	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((deptno == null) ? 0 : deptno.hashCode());
		result = prime * result + ((dname == null) ? 0 : dname.hashCode());
		result = prime * result + ((loc == null) ? 0 : loc.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Dept other = (Dept) obj;
		if (deptno == null) {
			if (other.deptno != null)
				return false;
		} else if (!deptno.equals(other.deptno))
			return false;
		if (dname == null) {
			if (other.dname != null)
				return false;
		} else if (!dname.equals(other.dname))
			return false;
		if (loc == null) {
			if (other.loc != null)
				return false;
		} else if (!loc.equals(other.loc))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
	}
}

##数据访问层
#1.1 EmpDao.java

package com.neu.dao;

import java.util.List;

import com.neu.entity.Dept;
import com.neu.entity.Emp;

public interface EmpDao {
	List<Emp> getAll() throws Exception;
	Emp getById(int empno) throws Exception;
	int getInsert(Emp emp) throws Exception;
	int getUpdate(Emp emp) throws Exception;
	int getDelete(int empno) throws Exception;
	int getbadel(String[] empnos) throws Exception;
	List<Emp> getFind(Emp emp) throws Exception;

#1.2 EmpDaoImpl.java 实现类

package com.neu.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.mysql.fabric.xmlrpc.base.Array;
import com.neu.entity.Dept;
import com.neu.entity.Emp;

public class EmpDaoImpl implements EmpDao {

	@Override
	public List<Emp> getAll() throws Exception {
		Connection connection = JDBCUtil.getConnection();
		String sql = "select * from emp";
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
		List<Emp> list = new ArrayList<>();
		Emp emp=null;
		Integer empno;
		String ename;
		Date hiredate;
		String job;
		Integer mgr;
		Double sal;
		Double comm;
		int deptno;
		Dept dept;
		DeptDao deptDao = new DeptDaoImpl();
		while(rs.next()) {
			empno=rs.getInt("empno");
			ename=rs.getString("ename");
			hiredate=rs.getDate("hiredate");
			job=rs.getString("job");
			mgr=rs.getInt("mgr");
			sal= rs.getDouble("sal");
			comm=rs.getDouble("comm");
			deptno=rs.getInt("deptno");
			dept = deptDao.getById(deptno);
			emp=new Emp(empno, ename, hiredate, job, mgr, sal, comm, dept);
			list.add(emp);
		}
		JDBCUtil.closeConnection(connection);
		return list;
	}

	@Override
	public Emp getById(int empno) throws Exception {
		Connection connection = JDBCUtil.getConnection();
		String sql = "select * from emp where empno = ?";
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {empno});
		Emp emp=null;
		String ename;
		Date hiredate;
		String job;
		Integer mgr;
		Double sal;
		Double comm;
		int deptno;
		Dept dept;
		DeptDao deptDao = new DeptDaoImpl();
		if(rs.next()) {
			ename=rs.getString("ename");
			hiredate=rs.getDate("hiredate");
			job=rs.getString("job");
			mgr=rs.getInt("mgr");
			sal= rs.getDouble("sal");
			comm=rs.getDouble("comm");
			deptno=rs.getInt("deptno");
			dept = deptDao.getById(deptno);
			emp=new Emp(empno, ename, hiredate, job, mgr, sal, comm, dept);
		}
		JDBCUtil.closeConnection(connection);
		return emp;
	}

	@Override
	public int getInsert(Emp emp) throws Exception {
		String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)";
		Object[] params = {
				emp.getEmpno(),
				emp.getEname(),
				emp.getJob(),
				emp.getMgr(),
				emp.getHiredate(),
				emp.getSal(),
				emp.getComm(),
				emp.getDept().getDeptno()
		};
		
		int n = JDBCUtil.executeUpdate(sql, params);
		return n;
	}

	@Override
	public int getUpdate(Emp emp) throws Exception {
		String sql = "update emp set ename = ?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?";
		Object[] params = {
				emp.getEname(),
				emp.getJob(),
				emp.getMgr(),
				emp.getHiredate(),
				emp.getSal(),
				emp.getComm(),
				emp.getDept().getDeptno(),
				emp.getEmpno(),
		};
		int n = JDBCUtil.executeUpdate(sql, params);
		return n;
	}

	@Override
	public int getDelete(int empno) throws Exception {
		String sql = "delete from emp where empno = ?";
		int n = JDBCUtil.executeUpdate(sql, new Object[] {empno});
		return n;
	}

	@Override
	public int getbadel(String[] empnos) throws Exception {
		String s = Arrays.toString(empnos).replace("[", "(").replace("]", ")");
		String sql = "delete from emp where empno in"+s;
		int n = JDBCUtil.executeUpdate(sql, null);
		return n;
	}

	@Override
	public List<Emp> getFind(Emp emp) throws Exception {
		StringBuilder stb = new StringBuilder();
		stb.append("select * from emp where 1=1 "); //1=1后面要有一个空格
		if(!emp.getEname().trim().equals("")) { //trim()去掉字符串两端的空格
			stb.append("and ename = '"+emp.getEname().trim()+"'");
		}
		if(!emp.getJob().trim().equals("")) {
			stb.append("and job = '"+emp.getJob().trim()+"'");
		}
		
		if(!(emp.getDept()==null)) {
			stb.append("and deptno = "+emp.getDept().getDeptno());
		}
		Connection connection = JDBCUtil.getConnection();
		String sql = stb.toString();
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
		List<Emp> list = new ArrayList<>();
		
		Integer empno;
		String ename;
		Date hiredate;
		String job;
		Integer mgr;
		Double sal;
		Double comm;
		int deptno;
		Dept dept;
		DeptDao deptDao = new DeptDaoImpl();
		while(rs.next()) {
			empno=rs.getInt("empno");
			ename=rs.getString("ename");
			hiredate=rs.getDate("hiredate");
			job=rs.getString("job");
			mgr=rs.getInt("mgr");
			sal= rs.getDouble("sal");
			comm=rs.getDouble("comm");
			deptno=rs.getInt("deptno");
			dept = deptDao.getById(deptno);
			emp=new Emp(empno, ename, hiredate, job, mgr, sal, comm, dept);
			list.add(emp);
		}
		JDBCUtil.closeConnection(connection);
		return list;
	}

}

#2.1 DeptDao.java

package com.neu.dao;

import java.util.List;

import com.neu.entity.Dept;


public interface DeptDao {
	List<Dept> getAll() throws Exception;
	int getInsert(Dept dept) throws Exception;
	Dept getById(int deptno) throws Exception;
}

#2.2 DeptDaoImpl.java 实现类

package com.neu.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.neu.entity.Dept;
import com.neu.entity.Emp;

public class DeptDaoImpl implements DeptDao {

	@Override
	public List<Dept> getAll() throws Exception {
		Connection connection = JDBCUtil.getConnection();
		String sql="select * from dept";
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
		List<Dept> list = new ArrayList<>();
		Dept dept = null;
		int deptno;
		String dname;
		String loc;
		while(rs.next()) {
			deptno=rs.getInt("deptno");
			dname=rs.getString("dname");
			loc=rs.getString("loc");
			dept = new Dept(deptno, dname, loc);
			list.add(dept);
		}
		JDBCUtil.closeConnection(connection);
		return list;
	}

	@Override
	public int getInsert(Dept dept) throws Exception {
		String sql = "insert into dept values (?,?,?)";
		int n = JDBCUtil.executeUpdate(sql, new Object[] {dept.getDeptno(),dept.getDname(),dept.getLoc()});
		return n;
	}

	@Override
	public Dept getById(int deptno) throws Exception {
		Connection connection = JDBCUtil.getConnection();
		String sql="select * from dept where deptno = ?";
		ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {deptno});
		
		Dept dept = null;
		
		String dname;
		String loc;
		if(rs.next()) {
			
			dname=rs.getString("dname");
			loc=rs.getString("loc");
			dept = new Dept(deptno, dname, loc);
			
		}
		JDBCUtil.closeConnection(connection);
		return dept;
	}

}

##业务逻辑层
#1.1 EmpService.java

package com.neu.service;

import java.util.List;

import com.neu.entity.Emp;

public interface EmpService {
	List<Emp> getAll() throws Exception;
	Emp getById(int empno) throws Exception;
	int getInsert(Emp emp) throws Exception;
	int getUpdate(Emp emp) throws Exception;
	int getDelete(int empno) throws Exception;
	int getbadel(String[] empnos) throws Exception;
	List<Emp> getFind(Emp emp) throws Exception;
}

#1.2 EmpServiceImpl.java 实现类

package com.neu.service;

import java.util.List;

import com.neu.dao.EmpDao;
import com.neu.dao.EmpDaoImpl;
import com.neu.entity.Emp;

public class EmpServiceImpl implements EmpService {
	private EmpDao empDao = new EmpDaoImpl();
	@Override
	public List<Emp> getAll() throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getAll();
	}

	@Override
	public Emp getById(int empno) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getById(empno);
	}

	@Override
	public int getInsert(Emp emp) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getInsert(emp);
	}

	@Override
	public int getUpdate(Emp emp) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getUpdate(emp);
	}

	@Override
	public int getDelete(int empno) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getDelete(empno);
	}

	@Override
	public int getbadel(String[] empnos) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getbadel(empnos);
	}

	@Override
	public List<Emp> getFind(Emp emp) throws Exception {
		// TODO 自动生成的方法存根
		return empDao.getFind(emp);
	}

}

#2.1 DeptService.java

package com.neu.service;

import java.util.List;

import com.neu.entity.Dept;

public interface DeptService {
	List<Dept> getAll() throws Exception;
	int getInsert(Dept dept) throws Exception;
	Dept getById(int deptno) throws Exception;
}

#2.2 DeptServiceImpl.java 实现类

package com.neu.service;

import java.util.List;

import com.neu.dao.DeptDao;
import com.neu.dao.DeptDaoImpl;
import com.neu.entity.Dept;

public class DeptServiceImpl implements DeptService {
	private DeptDao deptDao = new DeptDaoImpl();
	@Override
	public List<Dept> getAll() throws Exception {
		// TODO 自动生成的方法存根
		return deptDao.getAll();
	}

	@Override
	public int getInsert(Dept dept) throws Exception {
		// TODO 自动生成的方法存根
		return deptDao.getInsert(dept);
	}

	@Override
	public Dept getById(int deptno) throws Exception {
		// TODO Auto-generated method stub
		return deptDao.getById(deptno);
	}

}

##表示层
#1.1 getAll.jsp 显示所有部门员工

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function del(empno) {
	var b=confirm("是否删除?");
	if(b){
		location.href="${ pageContext.request.contextPath }/DeleteEmp?empno="+empno;
	}
}

function bat() {
	var nos = document.getElementsByName("no");
	var sum = 0;
	for(var i = 0;i < nos.length;i++ ){
		if(nos[i].checked){
			sum++;
		}
	}
	if(sum==0){
		alert("请选择删除的员工!");
		return false;
	}
	return confirm("是否删除选中的员工!");
}
</script>
</head>
<body>
	<form action="${ pageContext.request.contextPath }/findEmp" method="post">
	员工姓名:<input type="text" name="ename" value="${ param.ename }">
	员工岗位:<input type="text" name="job" value="${ param.job }">
	部门:<select name="deptno">
			<option value="0">-请选择-</option>
			<c:forEach items="${ deptlist }" var="dept">
				<option value="${dept.deptno }" ${ param.deptno==dept.deptno?"selected":"" }>${dept.dname }</option>
			</c:forEach>
		</select>
	<input type="submit" value="查询">
	</form>
	<form action="${ pageContext.request.contextPath }/badelEmpno" method="post" onsubmit="return bat()">
	<table border="1" width="700">
		<c:forEach items="${ list }" var="emp">
			<tr>
				<td><input type="checkbox" value="${ emp.empno }" name="no"> </td>
				<td>${ emp.empno }</td>
				<td>${ emp.ename }</td>
				<td>${ emp.job }</td>
				<td>${ emp.mgr }</td>
				<td><fmt:formatDate value="${ emp.hiredate }" pattern="yyyy-MM-dd"/></td>
				<td>${ emp.sal }</td>
				<td>${ emp.comm }</td>
				<td>${ emp.dept.dname }</td>
				<td>
					<a href="${ pageContext.request.contextPath }/EditEmpById?empno=${ emp.empno }">编辑</a>
				</td>
				<td>
					<a href="JavaScript:del(${ emp.empno })">删除</a>
				</td>
			</tr>
		</c:forEach>
		<tr align="center"><td colspan="11"><a href="${ pageContext.request.contextPath }/plgm">[添加]</a> <input type="submit" value="批量删除"></td></tr>
	</table>
	
	</form>
</body>
</html>

#1.2 GetAllServlet.java 显示所有员工

package com.neu.servlet;

import java.io.IOException;
import java.util.List;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class GetAllServlet
 */
@WebServlet("/GetAll")
public class GetAllServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EmpService empService = new EmpServiceImpl();
		
		try {
			List<Emp> list = empService.getAll();
			
			request.setAttribute("list", list);
			DeptService deptService = new DeptServiceImpl();
			List<Dept> deptlist = deptService.getAll();
			request.setAttribute("deptlist", deptlist);
			request.getRequestDispatcher("/WEB-INF/jsp/emp/getAll.jsp").forward(request, response);			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

1.3 edit.jsp 编辑页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/UpdateEmp" method="post">
		<table>
			<tr>
				<td>员工编号:</td>
				<td>
					<input type="text" name="empno" value="${ emp.empno }" readonly="readonly"> 
				</td>
				<tr>
				</tr>
				<td>员工姓名:</td>
				<td>
					<input type="text" name="ename" value="${ emp.ename }"> 
				</td>
				<tr>
				</tr>
				<td>员工岗位:</td>
				<td>
					<input type="text" name="job" value="${ emp.job }"> 
				</td>
				<tr>
				</tr>
				<td>员工经理:</td>
				<td>
					<input type="text" name="mgr" value="${ emp.mgr }"> 
				</td>
				<tr>
				</tr>
				<td>员工入职日期:</td>
				<td>
					<input type="text" name="hiredate" value="${ emp.hiredate }"> 
				</td>
				<tr>
				</tr>
				<td>员工薪水:</td>
				<td>
					<input type="text" name="sal" value="${ emp.sal }"> 
				</td>
				<tr>
				</tr>
				<td>员工奖金:</td>
				<td>
					<input type="text" name="comm" value="${ emp.comm }"> 
				</td>
				<tr>
				</tr>
				<td>员工部门:</td>
				<td>
					<select name="deptno">
						<c:forEach items="${ deptList }" var="dept">
							<option value="${ dept.deptno }" ${ emp.dept.deptno==dept.deptno?"selected":"" }>${ dept.dname }</option>
						</c:forEach>
					</select> 
				</td>
			</tr>
			<tr>
				<td>
				<input type="submit" value="提交">
				<input type="reset" value="重置">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

1.4 editEmpByIdServlet.java 根据员工ID跳转到员工编辑页

package com.neu.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class EditEmpByIdServlet
 */
@WebServlet("/EditEmpById")
public class EditEmpByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int empno = Integer.parseInt(request.getParameter("empno"));
		EmpService empService = new EmpServiceImpl();
		DeptService deptSerivce = new DeptServiceImpl();
		try {
			Emp emp = empService.getById(empno);
			request.setAttribute("emp", emp);
			List<Dept> deptList = deptSerivce.getAll();
			request.setAttribute("deptList", deptList);
			request.getRequestDispatcher("/WEB-INF/jsp/emp/edit.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#1.5 UpdateEmpServlet.java 修改员工信息并跳转回员工显示页

package com.neu.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class UpdateEmpServlet
 */
@WebServlet("/UpdateEmp")
public class UpdateEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Integer empno = Integer.parseInt(request.getParameter("empno"));
		String ename = request.getParameter("ename");
		String job = request.getParameter("job");
		Integer mgr = Integer.parseInt(request.getParameter("mgr"));
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Date hiredate = null;
		try {
			hiredate = f.parse(request.getParameter("hiredate"));
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		Double sal = Double.parseDouble(request.getParameter("sal"));
		Double comm = Double.parseDouble(request.getParameter("comm"));
		Integer deptno = Integer.parseInt(request.getParameter("deptno"));
		Dept dept = new Dept(deptno, null, null);
		Emp emp = new Emp(empno, ename, hiredate, job, mgr, sal, comm, dept);
		EmpService empService = new EmpServiceImpl();
		
		try {
			int n =empService.getUpdate(emp);
			request.getRequestDispatcher("/GetAll").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#1.6 DeleteEmpServlet.java 删除指定员工信息

package com.neu.servlet;

import java.io.IOException;
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 com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class DeleteEmpServlet
 */
@WebServlet("/DeleteEmp")
public class DeleteEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer empno = Integer.parseInt(request.getParameter("empno"));
		EmpService empService = new EmpServiceImpl();
		try {
			empService.getDelete(empno);
			request.getRequestDispatcher("/GetAll").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#1.7 addin.jsp 添加页面

<%@page import="com.neu.entity.Dept"%>
<%@page import="java.util.List"%>
<%@page import="com.neu.service.DeptServiceImpl"%>
<%@page import="com.neu.service.DeptService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	DeptService deptService = new DeptServiceImpl();
	List<Dept> list = deptService.getAll();
	request.setAttribute("list", list);
	%>
	<form action="${ pageContext.request.contextPath }/insertEmp" method="post">
		<table>
		<tr>
			<td>员工编号:</td>
			<td><input type="text" name="empno" ></td>
		</tr>
		<tr>
			<td>员工姓名:</td>
			<td><input type="text" name="ename" ></td>
		</tr>
		<tr>
			<td>岗位:</td>
			<td><input type="text" name="job" ></td>
		</tr>
		<tr>
			<td>经理编号:</td>
			<td><input type="text" name="mgr" ></td>
		</tr>
		<tr>
			<td>入职日期:</td>
			<td><input type="text" name="hiredate" ></td>
		</tr>
		<tr>
			<td>员工薪水:</td>
			<td><input type="text" name="sal" ></td>
		</tr>
		<tr>
			<td>员工奖金:</td>
			<td><input type="text" name="comm" ></td>
		</tr>
		<tr>
			<td>部门名称:</td>
			<td>
				<select name="deptno">
					<c:forEach items="${ list }" var="dept">
						<option value="${ dept.deptno }">${ dept.dname }</option>
					</c:forEach>
				</select>
			</td>
		</tr>
		<tr>
			<td>
			<input type="submit" value="提交">
			<input type="reset" value="重置">
			</td>
		</tr>
		</table>
	</form>
</body>
</html>

#1.8 insertEmpServlet.java 添加员工

package com.neu.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class insertEmpServlet
 */
@WebServlet("/insertEmp")
public class insertEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		EmpService empService = new EmpServiceImpl();
		DeptService deptService = new DeptServiceImpl();
		
		int empno = Integer.parseInt(request.getParameter("empno"));
		String ename = request.getParameter("ename");
		String job = request.getParameter("job");
		int mgr = Integer.parseInt(request.getParameter("mgr"));
		
		SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
		Date hiredate = null;
		try {
			hiredate = d.parse(request.getParameter("hiredate"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		double sal = Double.parseDouble(request.getParameter("sal"));
		double comm = Double.parseDouble(request.getParameter("comm"));
		int deptno = Integer.parseInt(request.getParameter("deptno"));
		Dept dept = null;
		try {
			dept = deptService.getById(deptno);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, dept);
		try {
			empService.getInsert(emp);
			request.getRequestDispatcher("/GetAll").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#badelEmpnoServlet.java 批量删除

package com.neu.servlet;

import java.io.IOException;
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 com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class badelEmpnoServlet
 */
@WebServlet("/badelEmpno")
public class badelEmpnoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] empnos = request.getParameterValues("no");
		EmpService empService = new EmpServiceImpl();
		
		try {
			empService.getbadel(empnos);
			request.getRequestDispatcher("/GetAll").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#findEmpServlet.java 根据输入条件进行查找

package com.neu.servlet;

import java.io.IOException;
import java.util.List;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class findEmpServlet
 */
@WebServlet("/findEmp")
public class findEmpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String ename = request.getParameter("ename");
		String job = request.getParameter("job");
		int deptno =Integer.parseInt(request.getParameter("deptno"));
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setJob(job);
		DeptService deptService = new DeptServiceImpl();
		Dept dept=null;
		try {
			dept = deptService.getById(deptno);
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		emp.setDept(dept);
		EmpService empService = new EmpServiceImpl();
		
		try {
			List<Emp> list = empService.getFind(emp);
			request.setAttribute("list", list);
			List<Dept> deptlist = deptService.getAll();
			request.setAttribute("deptlist", deptlist);
			request.getRequestDispatcher("WEB-INF/jsp/emp/getAll.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

#empServlet.java 用一个servlet完成多个请求
例:同时处理 显示全部、根据查询条件查询数据

package com.neu.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

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 com.neu.entity.Dept;
import com.neu.entity.Emp;
import com.neu.service.DeptService;
import com.neu.service.DeptServiceImpl;
import com.neu.service.EmpService;
import com.neu.service.EmpServiceImpl;

/**
 * Servlet implementation class empServlet
 */
@WebServlet("/empServlet")
public class empServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method==null) {
			doGetAll(request, response);
		}else if(method.equals("find")) {
			doGetFind(request, response);
		}
	}

	private void doGetFind(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		
		String ename = request.getParameter("ename");
		String job = request.getParameter("job");
		int deptno =Integer.parseInt(request.getParameter("deptno"));
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setJob(job);
		DeptService deptService = new DeptServiceImpl();
		Dept dept=null;
		try {
			dept = deptService.getById(deptno);
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		emp.setDept(dept);
		EmpService empService = new EmpServiceImpl();
		
		try {
			List<Emp> list = empService.getFind(emp);
			request.setAttribute("list", list);
			List<Dept> deptlist = deptService.getAll();
			request.setAttribute("deptlist", deptlist);
			request.getRequestDispatcher("WEB-INF/jsp/emp/getAll.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	private void doGetAll(HttpServletRequest request, HttpServletResponse response) {
		EmpService empService = new EmpServiceImpl();
		
		try {
			List<Emp> list = empService.getAll();
			
			request.setAttribute("list", list);
			DeptService deptService = new DeptServiceImpl();
			List<Dept> deptlist = deptService.getAll();
			request.setAttribute("deptlist", deptlist);
			request.getRequestDispatcher("/WEB-INF/jsp/emp/getAll.jsp").forward(request, response);			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值