//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
//这是regist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.dao.EmpDAO"%>
<%@page import="com.entity.Emp"%>
<%@page import="com.entity.Dep"%>
//这是show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.entity.Emp"%>
注册员工
编号 | 姓名 | 性别 | 部门 | 操作 |
---|---|---|---|---|
<%= e.getEmpid() %> | <%= e.getEmpname() %> | <%= e.getEmpsex()==1 ?"男":"女" %> | <%= e.getDepname() %> | 删除 | 修改 |
//这是update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.entity.Dep"%>
<%@page import="com.entity.Emp"%>
<%
//取出在servlet保存的数据
List
Emp emp = (Emp) request.getAttribute("emp");
%>
//这是web.xml的代码
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
//这是servlet的代码
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.dao.EmpDAO;
import com.entity.Emp;
public class EmpServlet implements Servlet {
EmpDAO empDAO = new EmpDAO();
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String p = req.getParameter("p");
if (p.equals("getAllDep2RegistView")) {
getAllDep2RegistView(req, res);
} else if (p.equals("regist")) {
regist(req, res);
} else if (p.equals("getAllEmps")) {
getAllEmps(req, res);
} else if (p.equals("del")) {
delEmp(req, res);
} else if (p.equals("updateView")) {
updateView(req, res);
}
else if (p.equals("update")){
update(req, res);
}
}
//保存修改
public void update(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
//编号
int empid = Integer.parseInt(request.getParameter("empid"));
//姓名
String empname = request.getParameter("empname");
//性别
int empsex= Integer.parseInt(request.getParameter("empsex"));
//部门
int depid =Integer.parseInt(request.getParameter("depid"));
//封装成Emp
Emp emp = new Emp();
emp.setEmpid(empid);
emp.setEmpname(empname);
emp.setDepid(depid);
emp.setEmpsex(empsex);
//修改
int n = empDAO.update(emp);
if (n>0)
getAllEmps(request, res);
else{
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出流
PrintWriter out = res.getWriter();
// 输出
out.write("");
}
}
//根据员工id查找员工,并查找所有部门,保存起来,转到修改界面
public void updateView(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
//根据id查找对象
Emp emp = empDAO.findById(id);
//查找所有部门
List deps = empDAO.getAllDep();
//保存
request.setAttribute("emp", emp);
request.setAttribute("deps", deps);
//转到修改界面
request.getRequestDispatcher("/update.jsp").forward(request, res);
}
// 删除方法
public void delEmp(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
int n = empDAO.delEmp(id);
if (n > 0)
getAllEmps(request, res);
else {
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出流
PrintWriter out = res.getWriter();
// 输出
out.write("");
}
}
// 取所有员工
public void getAllEmps(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
// 取所有员工
List emps = empDAO.getAllEmps();
request.setAttribute("emps", emps);
request.getRequestDispatcher("/show.jsp").forward(request, res);
}
// 查部门转转到注册页面
public void getAllDep2RegistView(ServletRequest request, ServletResponse res)
throws ServletException, IOException {
// 调用业务
List deps = empDAO.getAllDep();
// 保存部门
request.setAttribute("deps", deps);
// 转到注册页
request.getRequestDispatcher("/regist.jsp").forward(request, res);
}
// 注册
public void regist(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// 姓名
String empname = req.getParameter("empname");
// 性别
String empsex = req.getParameter("empsex");
// 爱好
String[] emploves = req.getParameterValues("emploves");
// 生日
String empage = req.getParameter("empage");
// 部门
String depid = req.getParameter("depid");
// 封装到Emp
Emp emp = new Emp();
emp.setDepid(Integer.parseInt(depid));
emp.setEmpage(java.sql.Date.valueOf(empage));
// 爱好
StringBuffer sb = new StringBuffer();
if (emploves != null) {
for (String str : emploves) {
sb.append(str).append(";");
}
emp.setEmploves(sb.toString());
}
emp.setEmpname(empname);
emp.setEmpsex(Integer.parseInt(empsex));
int n = empDAO.regist(emp);
// 客户显示中文
res.setContentType("text/html;charset=utf-8");
// 得到输出对象,准备客户端写内容
PrintWriter out = res.getWriter();
if (n > 0) {
// 输出
out
.write("");
} else {
out.write("");
}
}
}
//这是实体
public class Dep {
private int depid;
private String depname;
public int getDepid() {
return depid;
}
public void setDepid(int depid) {
this.depid = depid;
}
public String getDepname() {
return depname;
}
public void setDepname(String depname) {
this.depname = depname;
}
}
import java.sql.Date;
public class Emp extends Dep {
private int empid;
private String empname;
private int empsex;
private String emploves;
private Date empage;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public int getEmpsex() {
return empsex;
}
public void setEmpsex(int empsex) {
this.empsex = empsex;
}
public String getEmploves() {
return emploves;
}
public void setEmploves(String emploves) {
this.emploves = emploves;
}
public Date getEmpage() {
return empage;
}
public void setEmpage(Date empage) {
this.empage = empage;
}
}
//这是dao
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Dep;
import com.entity.Emp;
public class EmpDAO {
// 封装连接数据库
public Connection getCon() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test", "sa",
"sasa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 修改员工
public int update(Emp emp) {
Connection con = getCon();
String sql = "update emp set empname=?,empsex=?,depid=? where empid=?";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, emp.getEmpname());
ps.setObject(2, emp.getEmpsex());
ps.setObject(3, emp.getDepid());
ps.setObject(4, emp.getEmpid());
n = ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 取所有部门
public List getAllDep() {
Connection con = getCon();
List list = new ArrayList();
String sql = "select * from dep";
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int depid = rs.getInt("depid");
String depname = rs.getString("depname");
Dep dep = new Dep();
dep.setDepid(depid);
dep.setDepname(depname);
list.add(dep);
}
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 保存注册
public int regist(Emp emp) {
Connection con = getCon();
String sql = "insert into emp(empname,empsex,emploves,empage,depid) values(?,?,?,?,?)";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, emp.getEmpname());
ps.setObject(2, emp.getEmpsex());
ps.setObject(3, emp.getEmploves());
ps.setObject(4, emp.getEmpage());
ps.setObject(5, emp.getDepid());
n = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 删除员工
public int delEmp(int id) {
Connection con = getCon();
String sql = "delete from emp where empid=?";
int n = 0;
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, id);
n = ps.executeUpdate();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
// 取所有员工
public List getAllEmps() {
List list = new ArrayList();
Connection con = getCon();
String sql = "select * from emp join dep on emp.depid=dep.depid";
try {
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Emp emp = new Emp();
int empid = rs.getInt("empid");
String empname = rs.getString("empname");
int empsex = rs.getInt("empsex");
String depname = rs.getString("depname");
emp.setDepname(depname);
emp.setEmpname(empname);
emp.setEmpid(empid);
emp.setEmpsex(empsex);
list.add(emp);
}
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// 根据id查找员工
public Emp findById(int id) {
Connection con = getCon();
String sql = "select * from emp where empid=?";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setObject(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Emp emp = new Emp();
int empid = rs.getInt("empid");
String empname = rs.getString("empname");
int empsex = rs.getInt("empsex");
int depid = rs.getInt("depid");
emp.setDepid(depid);
emp.setEmpname(empname);
emp.setEmpid(empid);
emp.setEmpsex(empsex);
return emp;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
//这是servlet的一个生命周期
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletLife implements Servlet {
public ServletLife(){
System.out.println("实例化");
}
public void destroy() {
// TODO Auto-generated method stub
System.out.println("死亡");
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("初始化");
}
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("服务");
}
}