<java EE 项目:Attendance> 从一个简单项目看java web 开发的整体布局

来源:互联网 发布:下载淘宝网天猫商城 编辑:程序博客网 时间:2024/05/16 17:56

需求说明:

这里写图片描述

效果图:

1:数据库:
这里写图片描述

这里写图片描述

2:导入信息页面:
这里写图片描述

3:导入成功后的数据库:
这里写图片描述

4:显示考情信息(3条):
这里写图片描述

项目整体格局:
注意:不用管最后一个servlet的包,这个是为了测试,将attenceAction.jsp用servlet来实现

这里写图片描述

整体的布局,还是标准的三层结构:数据层Dao,业务层Biz,表示层Jsp

源代码:

实体:
这里写图片描述

package com.attendance.entity;import java.util.Date;public class Attence {    private int id;    private String empName;    private String dept;    private Date chkDate;    private int status;    public Attence(){}    public Attence(String empName, String dept, Date chkDate, int status) {        super();        this.empName = empName;        this.dept = dept;        this.chkDate = chkDate;        this.status = status;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getEmpName() {        return empName;    }    public void setEmpName(String empName) {        this.empName = empName;    }    public String getDept() {        return dept;    }    public void setDept(String dept) {        this.dept = dept;    }    public Date getChkDate() {        return chkDate;    }    public void setChkDate(Date chkDate) {        this.chkDate = chkDate;    }    public int getStatus() {        return status;    }    public void setStatus(int status) {        this.status = status;    }}

数据层:
(1)接口:
这里写图片描述

package com.attendance.dao;import java.util.List;import com.attendance.entity.Attence;public interface AttenceDao {    int addAttence(Attence attence);    List<Attence> getAttencesByNum(int num);}

(2)实现:
这里写图片描述
AttenceDaoImpl:

package com.attendance.dao.impl;import java.sql.ResultSet;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.attendance.dao.AttenceDao;import com.attendance.entity.Attence;public class AttenceDaoImpl extends BaseDao implements AttenceDao{    public int addAttence(Attence attence) {        int count = 0;        try {            openConnection();            String sql = "insert into attence(EmpName,Dept,ChkDate,Status)values(?,?,?,?)";            Object[] params = new Object[] {                attence.getEmpName(),                attence.getDept(),                attence.getChkDate(),                attence.getStatus()            };            count = executeUpdate(sql, params);            sql = "select last_insert_id()";            ResultSet rs = executeQuery(sql, null);            if(rs.next())            {                attence.setId(rs.getInt(1));            }        } catch (Exception e) {            e.printStackTrace();        }        finally        {            closeResource();        }        return count;    }    public List<Attence> getAttencesByNum(int num) {        List<Attence> attences = new ArrayList<Attence>();        try {            openConnection();            String sql = "select * from attence order by id desc limit ?";            ResultSet rs = executeQuery(sql, new Object[] {num});            while(rs.next())            {                Attence attence = new Attence();                attence.setId(rs.getInt("id"));                attence.setEmpName(rs.getString("empName"));                attence.setDept(rs.getString("dept"));                attence.setChkDate(rs.getDate("chkDate"));                attence.setStatus(rs.getInt("status"));                attences.add(attence);            }        } catch (Exception e) {            e.printStackTrace();        }        finally        {            closeResource();        }        return attences;    }    public static void main(String[] args) {        AttenceDao attenceDao = new AttenceDaoImpl();        Attence attence = new Attence();        attence.setEmpName("xjy");        attence.setDept("asd");        attence.setStatus(1);        //        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        try {            Date date = sdf.parse("2017-11-30");            attence.setChkDate(date);        } catch (Exception e) {            e.printStackTrace();        }        int count = attenceDao.addAttence(attence);        System.out.println(count);    }}

数据库访问函数封装:

package com.attendance.dao.impl;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class BaseDao {    String className = "com.mysql.jdbc.Driver";    String dbUrl = "jdbc:mysql://localhost:3306/musicdb";    Connection connection;  //连接    PreparedStatement statement; //命令    ResultSet resultSet; //结果集    //打开连接    public void openConnection() throws ClassNotFoundException, SQLException     {        //加载驱动类        Class.forName(className);        connection = DriverManager.getConnection(dbUrl,"root","root");    }    //增    public int executeUpdate(String sql, Object[] params) throws SQLException    {        statement = connection.prepareStatement(sql);        if(params != null) //追加参数        {            int i = 1;            for(Object object : params){                statement.setObject(i, object);                i++;            }        }        //执行sql        int count = statement.executeUpdate();        return count;    }    //查询    public ResultSet executeQuery(String sql,Object[] params) throws SQLException    {        statement = connection.prepareStatement(sql);        if(params != null) //追加参数        {            int i = 1;            for(Object object : params){                statement.setObject(i, object);                i++;            }        }        resultSet  = statement.executeQuery();        return resultSet;    }    //释放资源    public void closeResource()    {        try {            if(resultSet != null)            {                resultSet.close();            }            if(statement != null)            {                statement.close();            }            if(connection != null)            {                connection.close();            }        } catch (Exception e) {            e.printStackTrace();        }    }}

业务层:
(1)接口:
这里写图片描述

package com.attendance.biz;import java.util.List;import com.attendance.entity.Attence;public interface AttenceBiz {    List<Attence> getAttencesByNum(int num);    int addAttence(Attence attence);}

(2)实现:

package com.attendance.biz.impl;import java.util.List;import com.attendance.biz.AttenceBiz;import com.attendance.dao.AttenceDao;import com.attendance.dao.impl.AttenceDaoImpl;import com.attendance.entity.Attence;public class AttenceBizImpl implements AttenceBiz{    AttenceDao attenceDao = new AttenceDaoImpl();    //业务层调用数据层    public List<Attence> getAttencesByNum(int num) {        return attenceDao.getAttencesByNum(num);    }    public int addAttence(Attence attence) {        return attenceDao.addAttence(attence);    }}

表示层:
这里写图片描述

(1)attence.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'attence.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body >        <form id="考勤信息记录表" action="attenceAction.jsp" method="post">                <table border = "1">                    <tr>                        <td colspan = "2" align = "center"> 考情信息记录表  </td>                    </tr>                    <tr>                        <td>姓名:</td>                        <td><input class="txt" type="text" id="empname" name="empname" style = "width:380px;"/> <label class="error"></label></td>                    </tr>                    <tr>                        <td>所属部门:</td>                        <td><input class="txt" type="dept" id="dept" name="dept" style = "width:380px;" /> <label class="error"></label></td>                    </tr>                    <tr>                        <td>考勤日期:</td>                        <td><input class="txt" type="text" id="chkdate" name="chkdate" style = "width:200px;"/> 日期格式:yyyy-mm-dd  <label class="error"></label></td>                    </tr>                    <tr>                        <td>考勤状态:</td>                        <td>                             <select style = "width:120px;" name = "status" id = "status">                            <option value = "1"> 正常 </option>                            <option value = "2"> 迟到 </option>                            <option value = "3"> 早退 </option>                            <option value = "4"> 休假 </option>                            <option value = "5"> 外出 </option>                        </td>                    </tr>                    <tr>                        <td></td>                        <td>                            <input id="btnSubmit" type="submit" name="btnSubmit" value="登记" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                            <input type="reset" name="reset" id="reset" value="重置" />                        </td>                    </tr>                    <tr>                        <td colspan="2" align="center">                        <label class="error">${error}</label>                        </td>                    </tr>                </table>                </form>  </body></html>

(2)attenceAction

<%@page import="com.attendance.entity.Attence"%><%@page import="com.attendance.biz.impl.AttenceBizImpl"%><%@page import="com.attendance.biz.AttenceBiz"%><%@page import="java.text.SimpleDateFormat"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    request.setCharacterEncoding("utf-8");    String empname = request.getParameter("empname");    String dept = request.getParameter("dept");    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    Date date = sdf.parse(request.getParameter("chkdate"));    int status = Integer.parseInt(request.getParameter("status"));    AttenceBiz attenceBiz = new AttenceBizImpl();          //表示层调用业务层!!!    Attence attence = new Attence(empname,dept,date,status);      int count = attenceBiz.addAttence(attence);    if(count != 0)    {           request.setAttribute("error", "已成功导入!");        request.getRequestDispatcher("attence.jsp").forward(request, response);    }    else     {        request.setAttribute("error", "用户名或密码错误!");        request.getRequestDispatcher("attence.jsp").forward(request, response);    }%>

(3)showAttence

<%@page import="com.attendance.biz.impl.AttenceBizImpl"%><%@page import="com.attendance.entity.Attence"%><%@page import="com.attendance.dao.impl.AttenceDaoImpl"%><%@page import="com.attendance.biz.AttenceBiz"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'showAttence.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <%    AttenceBiz attenceBiz = new AttenceBizImpl();    List<Attence> attences = new ArrayList<Attence>();    attences = attenceBiz.getAttencesByNum(3);    request.setAttribute("attences", attences);   %>    <body>    <div id = "attences">                <table >                <tr>                    <td ><strong>员工姓名 </strong></td>                    <td ><strong>所属部门</strong></td>                    <td ><strong>考勤日期</strong></td>                    <td ><strong>考勤状态 </strong></td>                </tr>                <c:forEach var="attence" items="${attences}">                    <tr>                        <td ><strong>${attence.empName}</strong></td>                        <td ><strong>${attence.dept}</strong></td>                        <td ><strong>${attence.chkDate}</strong></td>                        <td ><strong>&nbsp;&nbsp;${attence.status}</strong></td>                    </tr>                </c:forEach>                </table>        </div>  </body></html>
阅读全文
1 0
原创粉丝点击