亲宝软件园·资讯

展开

jdbc员工管理系统 jdbc+jsp实现简单员工管理系统

lxh5431 人气:0

简单的页面分析

在上一个文章简单的数据库连接测试,已经测试和数据库做简单的交互,也就是dao层的实现,接下来要说的却是action的简单实现,在ssh中有struts作为表示层和server的交换,而这里我不是说的是关于struts这里只是简单的运用jsp的代码书写来实现数据的传输,这也是最繁琐的步骤,但是这却让我们对底层的调用有一个简单的了解,这里是直接调用封装好的数据,交换和使用,首先要书写的是action的使用,用的最多的就是getParameter表单的提交了,这里在网络上提交一个表单吗,然后通过getParameter进行获取,然后通过enployeeDao中的方法进行增删改查,就能够进行基本的逻辑操作了。

代码实现

<%@page import="dao.EmployeeDao"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="entity.Employee"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

 <%
  //1、接收表单提交的参数
  String action = request.getParameter("action");

  //3.调用EmployeeDao中addEmployee(Employee employee)完成员工添加
  EmployeeDao employeeDao = new EmployeeDao();
  if (action.equals("0") || action.equals("1")) {
   //添加 更新
   String empno = request.getParameter("empno");
   String ename = request.getParameter("ename");
   String sal = request.getParameter("sal");
   String hiredate = request.getParameter("hiredate");

   //2.将数据封装至Employee对象中
   Employee employee = new Employee();
   employee.setEmpno(Integer.parseInt(empno));
   employee.setEname(ename);
   employee.setSal(Double.parseDouble(sal));
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   employee.setHiredate(sdf.parse(hiredate));

   if (action.equals("0")) {
    //添加
    employeeDao.addEmployee(employee);
   } else {
    //更新
    employeeDao.updateEmployee(employee);
   }

  } else if (action.equals("2")) {
   //删除
   String empno = request.getParameter("empno");
   employeeDao.deleteEmployee(empno);
  } else if (action.equals("3")) {
   //批量删除
   System.out.println("action="+action);

   String[] chks=request.getParameterValues("chks");
   for(String chk:chks){
    System.out.println("chk="+chk);
   }


  }

  //4.画面跳转至employeeList.jsp 重定向
  response.sendRedirect("employeeList.jsp");
 %>
</body>
</html>

然后我们进行简单的页面设计,运用了表格的形式进行设计,代码如下

<%@page import="java.util.List"%>
<%@page import="entity.Employee"%>
<%@page import="dao.EmployeeDao"%>
<%@page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script>
 function checkAll() {

  //1.获取chkAll的选中状态checked
  var chkAll = document.getElementById("chkAll");
  //alert(chkAll.checked);

  //2、获取其他所有的checkbox
  var chks = document.getElementsByName("chks");
  for (var i = 0; i < chks.length; i++) {
   //3、将chkAll的选中状态于其他checkbox的选中状态同步
   chks[i].checked = chkAll.checked;
  }
 }

 function onDelete(empno){
  var result=confirm("Delete Employee?");
  if(result){
   window.location.href="employeeManageAction.jsp?action=2&empno=" rel="external nofollow" +empno;
  }
 }

 //function onDeleteBatch(){
  //判断是否有选中的checkbox

  //有选中的场合
  //提示是否删除确认的信息
  //确认删除
  //提交表单
  //var action=document.getElementById("action");
  //action.value="3";

 // document.frmlist.action="employeeManageAction.jsp"
  // document.frmlist.submit();
  //取消删除
  //不用处理



  //无选中的场合
  //提示选中记录的信息

 //}


</script>
</head>
<body>
 <h3>Employee List Page</h3>

 <form name="frmSearch" action="xxxxAction.jsp" method="post">
  ename:<input type="text" /> <input type="submit" name="btnSubmit"
   value="submit" />
 </form>
 <P>
 <hr />

 <a href="employeeManage.jsp?action=0" rel="external nofollow" >Add Employee</a> 
 <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="onDeleteBatch();">Delete
  Employee Batch</a>
 <P>
  <%
   EmployeeDao emplyeeDao = new EmployeeDao();
   List<Employee> employees = emplyeeDao.getEmployees();
  %>

  <%
   if (employees != null && employees.size() > 0) {
  %>

 <form name="frmlist" action="" method="post">
  <input type="hidden" name="action" id="action" value="" />
  <table width="600px" border="1px">

   <tr bgcolor="#009966">
    <td><input type="checkbox" id="chkAll" onclick="checkAll()" /></td>
    <td>empno</td>
    <td>ename</td>
    <td>sal</td>
    <td>hiredate</td>
    <td>action</td>
   </tr>

   <%
    Employee employee = null;
     for (int i = 0; i < employees.size(); i++) {
      employee = employees.get(i);
   %>
   <tr>
    <td><input type="checkbox" id="chk<%=i + 1%>" name="chks"
     value="<%=employee.getEmpno()%>" /></td>
    <td><%=employee.getEmpno()%></td>
    <td><%=employee.getEname()%></td>
    <td><%=employee.getSal()%></td>
    <td><%=employee.getHiredate()%></td>
    <td><a
     href="employeeManage.jsp?action=1&empno=<%=employee.getEmpno()%>" rel="external nofollow" >update</a>
     <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" 
     onclick="onDelete(<%=employee.getEmpno()%>);">delete</a></td>
   </tr>
   <%
    }
   %>
  </table>

 </form>

 <%
  }
 %>
</body>
</html>

接下来要说的是更新操作

<%@page import="entity.Employee"%>
<%@page import="dao.EmployeeDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script>
 function checkForm() {

  var empno = document.getElementById("empno");
  if (empno && empno.value == "") {
   empno.focus();
   alert("please input empno");
   return;
  }

  var ename = document.getElementById("ename");
  if (ename && ename.value == "") {
   ename.focus();
   alert("please input ename");
   return;
  }

  var sal = document.getElementById("sal");
  if (sal && sal.value == "") {
   sal.focus();
   alert("please input sal");
   return;
  }

  var hiredate = document.getElementById("hiredate");
  if (hiredate && hiredate.value == "") {
   hiredate.focus();
   alert("please input hiredate");
   return;
  }

  var frm = document.getElementById("frm");
  frm.submit();

 }
</script>


</head>
<body>

 <%
  String action=request.getParameter("action");

  Employee employee=null;
  if(action.equals("1")){
   //更新操作
   String empno = request.getParameter("empno");

   EmployeeDao employeeDao = new EmployeeDao();
   employee=employeeDao.getEmployeeByEmpno(empno);
  }

 %>

 <h3>Employee <%=action.equals("1")?"Update":"Regist" %> Page</h3>

 <form id="frm" action="employeeManageAction.jsp" method="post"
  onsubmit="return checkForm();">
  <input type="hidden" name="action" value="<%=action%>"/>
  <table>
   <tr>
    <td>empno</td>
    <td><input type="text" id="empno" name="empno" <%=action.equals("1")?"readOnly":"" %> value="<%=employee==null?"":employee.getEmpno()%>"/></td>
   </tr>

   <tr>
    <td>ename</td>
    <td><input type="text" id="ename" name="ename" value="<%=employee==null?"":employee.getEname()%>"/></td>
   </tr>

   <tr>
    <td>sal</td>
    <td><input type="text" id="sal" name="sal" value="<%=employee==null?"":employee.getSal()%>"/></td>
   </tr>

   <tr>
    <td>hiredate</td>
    <td><input type="text" id="hiredate" name="hiredate" value="<%=employee==null?"":employee.getHiredate()%>"/></td>
   </tr>

   <tr>
    <td><input type="button" name="btnSubmit" value="submit"
     onclick="checkForm();" /></td>
    <td><input type="reset" name="btnReset" value="reset" /></td>
   </tr>
  </table>
 </form>
</body>
</html>

在这个过程中刚好用到了简单的js对数据进行非空和数据类型判断,这就是我们需要学会的逻辑,业务层,然后进行其他操作,当然我注释的是还没完成的多个删除和模糊搜索,这个下一个博客再去完善。

总结

在学习这个过程中,学会了更多关于数据调用的知识,而不是像hibernate那样在配置文件配置,然后直接调用就可以了,再用jsp中也发现jsp嵌入代码,更加笨重,这也是我学习的另一个方面吧。

更多学习资料请关注专题《管理系统开发》。

加载全部内容

相关教程
猜你喜欢
用户评论