JavaWeb_day03_员工信息添加修改删除
day03员工的添加,修改,删除
修改功能
思路 : 点击修改员工数据之后,跳转到单行文本,查询要修改的员工id的全部信息,主键id设置为readonly,其余的都可以修改,
修改之后,提交按钮,提交之后跳转到全部员工界面
修改界面 :
部分代码 :
//点击修改跳转到UpShowEmp中转单行修改页面,并带上ID,可以查询员工id
"<td><a href=\"UpShowEmp?EmpID="+bean.getEmpID()+"\">修改<a></td>"
//中转修改界面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置页面编码格式,显示汉字
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("EmpID");
EmpDao dao = new EmpDao();
EmpBean bean = new EmpBean();
bean = dao.ShowMsgByID(id);
PrintWriter pw = response.getWriter();//页面显示
StringBuffer sb = new StringBuffer();
sb.append("<html><body><form action=\"UpdateEmp\">");
sb.append("<table align=center cellpadding=3 cellspacing=0>");
sb.append("<tr align=center><td>员工编码</td><td>员工姓氏</td><td>员工名字</td><td>员工工资</td><td>入职时间</td>");
sb.append("</tr><tr><td><input type=text value="+bean.getEmpID()+" readonly name="+"empId"+"></td>");
sb.append("<td><input type=text name="+"firstName"+" value="+bean.getFirstName()+"></td>");
sb.append("<td><input type=text name="+"lastName"+" value="+bean.getLastName()+"></td>");
sb.append("<td><input type=text name="+"salary"+" value="+bean.getSalary()+"></td>");
sb.append("<td><input type=text name="+"hireDate"+" value="+bean.getHireDate()+"></td></tr>");
sb.append("</table><table align=center cellpadding=3 cellspacing=0><tr><td><input type=submit value=\"提交\" name="+"submit></tr></td>");
sb.append("</table ></form></body></html>");
pw.write(sb.toString());
//修改提交之后的代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EmpBean bean = new EmpBean();
EmpDao dao = new EmpDao();
String empId = request.getParameter("empId");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String salary = request.getParameter("salary");
String hireDate = request.getParameter("hireDate");
int EmpId = Integer.parseInt(empId);
double sal = Double.parseDouble(salary);
bean.setEmpID(EmpId);
bean.setFirstName(firstName);
bean.setLastName(lastName);
bean.setSalary(sal);
bean.setHireDate(hireDate);
int column = dao.updateEmp(bean);
System.out.println(column);
if(column!=0){
response.sendRedirect("ShowMsgByPage");
}
}
删除员工信息
那就相对简单了,点击删除之后,带上员工id,跳转到DeleteEmpServlet页面.然后request.getParameter获取传过来的id,dao中的delete方法输进去id,赋给int类型,若为1则删除成功,跳转全部员工页面.
/ /删除按钮
"<td><a href=\"DeleteEmp?EmpID="+bean.getEmpID()+"\">删除</a></td>
public class DeleteEmp extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EmpDao dao = new EmpDao();
String id = request.getParameter("EmpID");
System.out.println(id);
int column = dao.deleteEmp(id);
if(column != 0){
response.sendRedirect("ShowMsgByPage");
}
}
添加员工信息
单独设置一个a标签,点击添加之后,跳转到一个单行文本框的html页面,其中id不可以添加,序列自动生成,点击按钮之后跳转addEmpServlet界面,通过request.getParmeter获取添加的信息,然后在bean中set,然后调用dao添加方法,
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EmpBean bean = new EmpBean();
EmpDao dao = new EmpDao();
request.getParameter("submit");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String salary = request.getParameter("salary");
String hireDate = request.getParameter("hireDate");
bean.setFirstName(firstName);
bean.setLastName(lastName);
bean.setHireDate(hireDate);
bean.setSalary(Double.parseDouble(salary));
int column = dao.addEmp(bean);
//System.out.println(column);
if(column!=0){
response.sendRedirect("ShowMsgByPage");
}
}