首页 > Web开发 > 详细

使用jsp实现员工信息表的增删改查

时间:2015-10-19 10:45:28      阅读:1443      评论:0      收藏:0      [点我收藏+]

 

更新操作:实现的方式,其中先编写DAO文件和数据库的连接

 

 

<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>
<%@page import="com.shaoye.Employee.Employ"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>修改员工信息</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
  </head>

  <body>
    <div id="wrap">
      <div id="top_content">
        <div id="header">
          <div id="rightheader">
            <p>
                 <%
                  Date date = new Date();
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                  String str = sdf.format(date);
              %>
              <%=str%>
              <br />
            </p>
          </div>
          <div id="topheader">
            <h1 id="title">
              <a href="#">Main</a>
            </h1>
          </div>
          <div id="navigation">
          </div>
        </div>
        <div id="content">
          <p id="whereami">
          </p>
          <h1>
            修改员工信息:
          </h1>
          <form action="updateEmp_ok.jsp" method="post">
          <%
              int id = Integer.parseInt(request.getParameter("id"));
              EmployeeDAO dao =new EmployeeDAO();
              Employ emp=dao.findById(id);
              
               %>
            <table cellpadding="0" cellspacing="0" border="0"
              class="form_table">
              
              <tr>
                <td valign="middle" align="right">
                  id:
                </td>
                <td valign="middle" align="left">
                  <%=id %>
                  <input  type="hidden" value="<%=id %>" name="id">
                </td>
              </tr>
              <tr>
                <td valign="middle" align="right">
                  姓名:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="name"
                    value="<%=emp.getName() %>" />
                </td>
              </tr>
              <tr>
                <td valign="middle" align="right">
                  薪水:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="salary"
                    value="<%=emp.getSalary() %>" />
                </td>
              </tr>
              <tr>
                <td valign="middle" align="right">
                年龄:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="age"
                    value="<%=emp.getAge() %>" />
                </td>
              </tr>
            </table>
            <p>
              <input type="submit" class="button" value="确认更新" />
            </p>
          </form>
        </div>
      </div>
      <div id="footer">
        <div id="footer_bg">
          ABC@126.com
        </div>
      </div>
    </div>
  </body>
</html>
更新的逻辑页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.shaoye.Employee.Employ"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setContentType("utf-8");

    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    double salary = Double.parseDouble(request.getParameter("salary"));
    int age = Integer.parseInt(request.getParameter("age"));

    Employ emp = new Employ();
    emp.setAge(age);
    emp.setId(id);
    emp.setName(name);
    emp.setSalary(salary);

    EmployeeDAO dao = new EmployeeDAO();
    dao.modify(emp);

    response.sendRedirect("listEmp.jsp");
%>

 

增加页面:

<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>添加员工信息</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
  </head>

  <body>
    <div id="wrap">
      <div id="top_content">
        <div id="header">
          <div id="rightheader">
            <p>
              <%
                  Date date = new Date();
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                  String str = sdf.format(date);
              %>
              <%=str%>
              <br />
            </p>
          </div>
          <div id="topheader">
            <h1 id="title">
              <a href="#">Main</a>
            </h1>
          </div>
          <div id="navigation">
          </div>
        </div>
        <div id="content">
          <p id="whereami">
          </p>
          <h1>
           添加员工信息:
          </h1>
          <form action="addEmp_ok.jsp" method="post">
            <table cellpadding="0" cellspacing="0" border="0"
              class="form_table">
            
              <tr>
                <td valign="middle" align="right">
                  姓名:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="name" />
                </td>
              </tr>
              <tr>
                <td valign="middle" align="right">
                 薪水:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="salary" />
                </td>
              </tr>
              <tr>
                <td valign="middle" align="right">
                  年龄:
                </td>
                <td valign="middle" align="left">
                  <input type="text" class="inputgri" name="age" />
                </td>
              </tr>
            </table>
            <p>
              <input type="submit" class="button" value="提交" />
            </p>
          </form>
        </div>
      </div>
      <div id="footer">
        <div id="footer_bg">
          ABC@126.com
        </div>
      </div>
    </div>
  </body>
</html>

增加的逻辑页面:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.shaoye.Employee.Employ"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
String name = request.getParameter("name");
double salary = Double.parseDouble(request.getParameter("salary"));
int age = Integer.parseInt(request.getParameter("age"));

Employ emp = new Employ();
emp.setAge(age);
emp.setName(name);
emp.setSalary(salary);
EmployeeDAO dao= new EmployeeDAO();
dao.save(emp);
response.sendRedirect("listEmp.jsp");
%>

删除页面存在逻辑页面:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");


int id =Integer.parseInt(request.getParameter("id"));

EmployeeDAO dao = new EmployeeDAO();
dao.delete(id);

response.sendRedirect("listEmp.jsp");

%>

 

 

查询页面的代码:

 

<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.shaoye.Employee.EmployeeDAO"%>
<%@page import="java.util.List"%>
<%@page import="com.shaoye.Employee.Employ"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <title>员工管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script type="text/javascript">
    function showtime(){
    var d = new Date();
  // 将系统时间的每一个值提取出来
  var year = d.getYear();//年
  var month = d.getMonth();//月
  var day = d.getDate();//日
  var hour = d.getHours()>=10 ? d.getHours() : ("0"+d.getHours());  //时
  var mi = d.getMinutes()>=10 ? d.getMinutes() : ("0"+d.getMinutes()); //分
  var ss = d.getSeconds()>=10 ? d.getSeconds() : ("0"+d.getSeconds());//秒
  var t = year+"年"+(month+1)+"月"+day+"日"+" "+hour+":"+mi+":"+ss;
  document.getElementById("showTimes").innerHTML=t;
  setTimeout(showTime, 1000);
    }
    
    </script>
  </head>
  <body  onload="showtime()">
    <div id="wrap">
      <div id="top_content">
        <div id="header">
          <div id="rightheader">
            <p>
                 <span id="showTimes"></span>
              <br />
            </p>
          </div>
          <div id="topheader">
            <h1 id="title">
              <a href="#">main</a>
            </h1>
          </div>
          <div id="navigation">
          </div>
        </div>
        <div id="content">
          <p id="whereami">
          </p>
          <h1>
            Welcome!
          </h1>
          <table class="table">
            <tr class="table_header">
              <td>
                ID
              </td>
              <td>
                姓名
              </td>
              <td>
                薪水
              </td>
              <td>
                年龄
              </td>
              <td>
                操作
              </td>
            </tr>
            <%
            EmployeeDAO dao = new EmployeeDAO();
            List<Employ> employs = dao.findAll();
            for(int i =0;i<employs.size();i++){
            Employ emp = employs.get(i);
           
             %>
            <tr class="row<%=(i%2+1)%>">
              <td>
             <span> <%=emp.getId() %></span>
              </td>
              <td>
                <%=emp.getName() %>
              </td>
              <td>
                <%=emp.getSalary() %>
              </td>
              <td>
                <%=emp.getAge() %>
              </td>
              <td>
                <a href="delete_ok.jsp?id=<%=emp.getId()%>">删除</a>&nbsp;
                <a href="updateEmp.jsp?id=<%=emp.getId()%>">修改</a>
              </td>
            </tr>
            <%} %>
          </table>
          <p>
            <input type="button" class="button" value="添加员工"
              onclick="location=‘addEmp.jsp‘" />
          </p>
        </div>
      </div>
      <div id="footer">
        <div id="footer_bg">
          ABC@126.com
        </div>
      </div>
    </div>
  </body>
</html>

使用jsp实现员工信息表的增删改查

原文:http://www.cnblogs.com/shaoyefeng/p/4890952.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!