package com.neu.entity;
public class Post {
private Integer postId;
private String postName;
private String postDesc;
public Post() {
super();
}
public Post(Integer postId, String postName, String postDesc) {
super();
this.postId = postId;
this.postName = postName;
this.postDesc = postDesc;
}
@Override
public String toString() {
return "post [postId=" + postId + ", postName=" + postName + ", postDesc=" + postDesc + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((postDesc == null) ? 0 : postDesc.hashCode());
result = prime * result + ((postId == null) ? 0 : postId.hashCode());
result = prime * result + ((postName == null) ? 0 : postName.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;
Post other = (Post) obj;
if (postDesc == null) {
if (other.postDesc != null)
return false;
} else if (!postDesc.equals(other.postDesc))
return false;
if (postId == null) {
if (other.postId != null)
return false;
} else if (!postId.equals(other.postId))
return false;
if (postName == null) {
if (other.postName != null)
return false;
} else if (!postName.equals(other.postName))
return false;
return true;
}
public Integer getPostId() {
return postId;
}
public void setPostId(Integer postId) {
this.postId = postId;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
public String getPostDesc() {
return postDesc;
}
public void setPostDesc(String postDesc) {
this.postDesc = postDesc;
}
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neu.entity.Employee;
import com.neu.entity.Post;
public class EmployeeDaoImpl implements EmployeeDao {
@Override
public List<Employee> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from employee";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Employee> list=new ArrayList<>();
Employee employee=null;
Integer empId;
Integer postId;
Post post=null;
PostDao postDao=new PostDaoImpl();
String empName;
Integer empSex;
Integer empAge;
String empDepart;
Integer empYear;
while(rs.next()) {
empId=rs.getInt("emp_id");
postId=rs.getInt("post_Id");
post = postDao.getPostByPostId(postId);
empName=rs.getString("emp_name");
empSex=Integer.parseInt(rs.getString("emp_sex"));
empAge=rs.getInt("emp_age");
empDepart=rs.getString("emp_depart");
empYear=rs.getInt("emp_year");
employee=new Employee(empId, post, empName, empSex, empAge, empDepart, empYear);
list.add(employee);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public Employee getByEmpId(Integer empId) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from employee where emp_id=?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {empId});
Employee employee=null;
Integer postId;
Post post=null;
PostDao postDao=new PostDaoImpl();
String empName;
Integer empSex;
Integer empAge;
String empDepart;
Integer empYear;
while(rs.next()) {
empId=rs.getInt("emp_id");
postId=rs.getInt("post_Id");
post = postDao.getPostByPostId(postId);
empName=rs.getString("emp_name");
empSex=Integer.parseInt(rs.getString("emp_sex"));
empAge=rs.getInt("emp_age");
empDepart=rs.getString("emp_depart");
empYear=rs.getInt("emp_year");
employee=new Employee(empId, post, empName, empSex, empAge, empDepart, empYear);
}
JDBCUtil.closeConnection(connection);
return employee;
}
}
package com.neu.dao;
import java.util.List;
import com.neu.entity.Post;
public interface PostDao {
Post getPostByPostId(Integer postId) throws Exception;
List<Post> getAll() throws Exception;
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neu.entity.Post;
public class PostDaoImpl implements PostDao {
@Override
public Post getPostByPostId(Integer postId) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from post where post_id=?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {postId});
Post post=null;
String postName;
String postDesc;
if(rs.next()) {
postName=rs.getString("post_name");
postDesc=rs.getString("post_desc");
post=new Post(postId, postName, postDesc);
}
JDBCUtil.closeConnection(connection);
return post;
}
@Override
public List<Post> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from post";
ResultSet rs = JDBCUtil.executeQuery(connection,sql, null);
List<Post> list=new ArrayList<>();
Post post=null;
String postName;
String postDesc;
Integer postId;
while(rs.next()) {
postId=rs.getInt("post_id");
postName=rs.getString("post_name");
postDesc=rs.getString("post_desc");
post=new Post(postId, postName, postDesc);
list.add(post);
}
JDBCUtil.closeConnection(connection);
return list;
}
}
package com.neu.dao;
import java.util.List;
import com.neu.entity.Employee;
public interface EmployeeDao {
List<Employee> getAll() throws Exception;
Employee getByEmpId(Integer empId) throws Exception;
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neu.entity.Employee;
import com.neu.entity.Post;
public class EmployeeDaoImpl implements EmployeeDao {
@Override
public List<Employee> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from employee";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Employee> list=new ArrayList<>();
Employee employee=null;
Integer empId;
Integer postId;
Post post=null;
PostDao postDao=new PostDaoImpl();
String empName;
Integer empSex;
Integer empAge;
String empDepart;
Integer empYear;
while(rs.next()) {
empId=rs.getInt("emp_id");
postId=rs.getInt("post_Id");
post = postDao.getPostByPostId(postId);
empName=rs.getString("emp_name");
empSex=Integer.parseInt(rs.getString("emp_sex"));
empAge=rs.getInt("emp_age");
empDepart=rs.getString("emp_depart");
empYear=rs.getInt("emp_year");
employee=new Employee(empId, post, empName, empSex, empAge, empDepart, empYear);
list.add(employee);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public Employee getByEmpId(Integer empId) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql="select * from employee where emp_id=?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {empId});
Employee employee=null;
Integer postId;
Post post=null;
PostDao postDao=new PostDaoImpl();
String empName;
Integer empSex;
Integer empAge;
String empDepart;
Integer empYear;
while(rs.next()) {
empId=rs.getInt("emp_id");
postId=rs.getInt("post_Id");
post = postDao.getPostByPostId(postId);
empName=rs.getString("emp_name");
empSex=Integer.parseInt(rs.getString("emp_sex"));
empAge=rs.getInt("emp_age");
empDepart=rs.getString("emp_depart");
empYear=rs.getInt("emp_year");
employee=new Employee(empId, post, empName, empSex, empAge, empDepart, empYear);
}
JDBCUtil.closeConnection(connection);
return employee;
}
}
package com.neu.service;
import java.util.List;
import com.neu.entity.Post;
public interface PostService {
Post getPostByPostId(Integer postId) throws Exception;
List<Post> getAll() throws Exception;
}
package com.neu.service;
import java.util.List;
import com.neu.dao.PostDao;
import com.neu.dao.PostDaoImpl;
import com.neu.entity.Post;
public class PostServiceImpl implements PostService {
PostDao postDao=new PostDaoImpl();
@Override
public Post getPostByPostId(Integer postId) throws Exception {
Post post = postDao.getPostByPostId(postId);
return post;
}
@Override
public List<Post> getAll() throws Exception {
List<Post> list = postDao.getAll();
return list;
}
}
package com.neu.service;
import java.util.List;
import com.neu.dao.EmployeeDao;
import com.neu.dao.EmployeeDaoImpl;
import com.neu.entity.Employee;
public class EmployeeServiceImpl implements EmployeeService {
EmployeeDao employeeDao= new EmployeeDaoImpl();
@Override
public List<Employee> getAll() throws Exception {
List<Employee> list = employeeDao.getAll();
return list;
}
@Override
public Employee getByEmpId(Integer empId) throws Exception {
Employee employee = employeeDao.getByEmpId(empId);
return employee;
}
}
package com.neu.service;
import java.util.List;
import com.neu.dao.EmployeeDao;
import com.neu.dao.EmployeeDaoImpl;
import com.neu.entity.Employee;
public class EmployeeServiceImpl implements EmployeeService {
EmployeeDao employeeDao= new EmployeeDaoImpl();
@Override
public List<Employee> getAll() throws Exception {
List<Employee> list = employeeDao.getAll();
return list;
}
@Override
public Employee getByEmpId(Integer empId) throws Exception {
Employee employee = employeeDao.getByEmpId(empId);
return employee;
}
}
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.Post;
import com.neu.service.PostService;
import com.neu.service.PostServiceImpl;
@WebServlet("/beginServlet")
public class beginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PostService postService=new PostServiceImpl();
try {
List<Post> postList = postService.getAll();
request.setAttribute("postList",postList);
request.getRequestDispatcher("/WEB-INF/jsp/employee/default.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
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.Employee;
import com.neu.service.EmployeeService;
import com.neu.service.EmployeeServiceImpl;
@WebServlet("/testServlet")
public class testServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String empname="";
Integer postid=0;
empname = request.getParameter("empname");
postid = Integer.parseInt(request.getParameter("postid"));
EmployeeService employeeService=new EmployeeServiceImpl();
List<Employee> employeelist=new ArrayList<>();
try {
employeelist = employeeService.getAll();
} catch (Exception e1) {
e1.printStackTrace();
}
if(postid!=0 && "".equals(empname)) {
request.setAttribute("employeelist", employeelist);
request.getRequestDispatcher("/WEB-INF/jsp/employee/rs.jsp").forward(request, response);
}else if(postid!=0 && !("".equals(empname))) {
for(Employee emp:employeelist) {
if(emp.getEmpName().equals(empname)) {
try {
Employee employee = employeeService.getByEmpId(emp.getEmpId());
request.setAttribute("employee", employee);
request.getRequestDispatcher("/WEB-INF/jsp/employee/rs.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else if(postid==0) {
int error=1;
request.getRequestDispatcher("/WEB-INF/jsp/employee/default.jsp?error="+error).forward(request, response);
}else {
response.getWriter().println("都不满足");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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>123
<table border="1">
<c:if test="${!(empty employeelist)}">
<c:forEach items="${employeelist}" var="emp">
<tr>
<td>${emp.empId}</td>
<td>${emp.post.postName}</td>
<td>${emp.empName}</td>
<td>${emp.empSex}<c:choose><c:when test="${emp.empSex}==1">男</c:when><c:when test="${emp.empSex}==2">女</c:when> </c:choose></td>
<td>${emp.empAge}</td>
<td>${emp.empDepart}</td>
<td>${emp.empYear}年</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${!(empty employee)}">
<tr>
<td>${employee.empId}</td>
<td>${employee.post.postName}</td>
<td>${employee.empName}</td>
<td>${employee.empSex}<c:choose><c:when test="${emp.empSex}==1">男</c:when><c:when test="${emp.empSex}==2">女</c:when> </c:choose></td>
<td>${employee.empAge}</td>
<td>${employee.empDepart}</td>
<td>${employee.empYear}年</td>
</tr>
</c:if>
</body>
</html>
<%@ 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>
<c:if test="${error}==1">请选择员工职位</c:if>
<form action="${pageContext.request.contextPath}/testServlet" method="get">
<table border="1">
<tr>
<td colspan="2">雇员查询系统</td>
</tr>
<tr>
<td>雇员姓名:</td>
<td><input type="text" name="empname"></td>
</tr>
<tr>
<td>员工职位:</td>
<td>
<select name="postid">
<c:forEach items="${postList}" var="post1">
<option value="${post1.postId}">${post1.postName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>