day03員工的添加,修改,刪除 修改功能 思路 : 點擊修改員工數據之後,跳轉到單行文本,查詢要修改的員工id的全部信息,主鍵id設置為readonly,其餘的都可以修改, 修改之後,提交按鈕,提交之後跳轉到全部員工界面 修改界面 : 部分代碼 : 刪除員工信息 那就相對簡單了,點擊刪除之後,帶上 ...
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");
}
}