作业1:小型考勤登记表

这次在广州实习了20天,收获还比较大。不过仍需要继续努力。这次总共布置了两个作业,我总结一下:

登记考勤信息,查看信息——主要就是往数据库增加数据,然后再从数据库中读取出来。

代码演示:

从数据库里面写入数据:

<%@page import="com.Seraphjin.Attence.model.attence"%>
<%@page import="com.Attence.BizImpl.AttenceBizImpl"%>
<%@page import="com.Attence.biz.AttenceBiz"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("name");
    String dept = request.getParameter("dept");
    String datetime = request.getParameter("datetime");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
    Date date = sdf.parse(datetime);
    int status =Integer.valueOf(request.getParameter("status")).intValue();
    attence a = new attence();
    a.setEmpName(name);
    a.setDept(dept);
    a.setDatetime(date);
    a.setStatus(status);

    AttenceBiz attenceBiz = new AttenceBizImpl();
    boolean result = attenceBiz.addAttence(a);
    if(result){
        //response.sendRedirect("index.jsp");
        out.print("成功");
    }else{
        //request.getRequestDispatcher("add.jsp").forward(request, response);
        out.print("失败");
    }

 %>

这个是在jsp里面写的,不是在servlet里面写的。因为好理解,不过我现在已经习惯了写在servlet里面了。

首页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>
<html>
    <head>
        <meta charset="utf-8" />
        <title>考勤记录信息统计</title>
        <link rel="stylesheet" type="text/css" href="CSS/index.css"/>
    </head>
    <body>
        <div class="all">
        <form id="writeDown" action="writeDown.jsp" method="post">
            <table border="2px">
                <tr>
                    <th class="zerot" colspan="2">考勤记录信息统计表</th>
                </tr>
                <tr>
                    <th class="onet">姓名</th>
                    <th class="twot">
                        <input type="text" class="name" name="name"/>
                    </th>
                </tr>
                <tr>
                    <th class="onet">所属部门</th>
                    <th class="twot">
                        <input type="text" class="dept" name="dept"/>
                    </th>
                </tr>
                <tr>
                    <th class="onet">考勤日期</th>
                    <th class="twot">
                        <input type="date" class="atime" name="datetime" value="2017-06-26"/>
                    </th>
                </tr>
                <tr>
                    <th class="onet">考勤状态</th>
                    <th class="twot">
                        <select name="status" id="status">
                            <option value="1">正常 </option>
                            <option value="2">迟到 </option>
                            <option value="3">早退 </option>
                            <option value="4">休假 </option>
                            <option value="5">外出 </option>
                          </select>
                    </th>
                </tr>
            </table>
            <input id="btnSubmit" type="submit" name="btnSubmit" value="录入" />
            <input id="btnReset" type="reset" name="btnReset" value="重置" />
            <a href="Info.jsp">查看所有考勤信息</a>
            </form>
        </div>

    </body>
</html>

获取所有数据的jsp:

<%@page import="com.Seraphjin.Attence.model.attence"%>
<%@page import="com.Attence.BizImpl.AttenceBizImpl"%>
<%@page import="com.Attence.biz.AttenceBiz"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%

    AttenceBiz aBiz =new AttenceBizImpl();
    List<attence> attences = aBiz.getAll();
    request.setAttribute("attences", attences);
 %>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>考勤记录信息统计</title>
        <link rel="stylesheet" type="text/css" href="CSS/index.css"/>
    </head>
    <body>
        <div class="all1">

            <table border="2px">
                <tr>
                    <th class="zerot" colspan="4">考勤记录信息统计表</th>
                </tr>
                <tr>
                    <th class="">员工姓名</th>
                    <th class="">所属部门</th>
                    <th class="">考勤日期</th>
                    <th class="">考勤状态</th>
                </tr>
                <c:forEach var="attence" items="${attences}">
                    <tr>
                        <td>${attence.empName }</td>
                        <td>${attence.dept }</td>
                        <td>${attence.datetime }</td>
                        <td>${attence.status }</td>
                    </tr>                    

                </c:forEach>
            </table>

        </div>
    </body>
</html>

这个是把数据库里面的数据全部读取出来。

主要的方法实现:

package com.Attence.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.Attence.dao.AttenceDao;
import com.Seraphjin.Attence.model.attence;

public class AttenceDaoImpl extends BaseDao implements AttenceDao {

    ArrayList<attence> attences = new ArrayList<attence>();
    @Override
    public List<attence> getAll() {
        try {
            openConnection();
            String sql= "select * from attence";
            ResultSet resultSet = executeQuery(sql, null);
            while (resultSet.next()) {
                attence a = new attence();
                a.setId(resultSet.getInt("id"));
                a.setEmpName(resultSet.getString("empName"));
                a.setDept(resultSet.getString("dept"));
                a.setDatetime(resultSet.getDate("datetime"));
                a.setStatus(resultSet.getInt("status"));
                attences.add(a);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeResourse();
        }
        return attences;
    }

    @Override
    public boolean addAttence(attence a) {
        boolean result = false;
        try {
            openConnection();
            String sql ="insert into attence value(?,?,?,?,?)";
            result =excute(sql, new Object[]{
                a.getId(),
                a.getEmpName(),
                a.getDept(),
                a.getDatetime(),
                a.getStatus()
            });
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {

            e.printStackTrace();
        }finally{
            closeResourse();
        }
        return result;
    }

    @Override
    public boolean deleteAttence(int id) {
        boolean result = false;
        try {
            openConnection();
            String sql ="delete from attence where id = ?";
            result = excute(sql, new Object[]{id});
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {

            e.printStackTrace();
        }finally{
            closeResourse();
        }
        return result;

    }

    @Override
    public boolean updateAttence(attence a) {
        boolean result = false;
        try {
            openConnection();
            String sql = "update attence set empName = ?, dept =?, datetime=?,status=? where id=?";
            result = excute(sql, new Object[]{
                    a.getId()
            });
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeResourse();
        }
        return result;
    }

}

连接数据库的基本操作:

package com.Attence.daoImpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    //连接数据库
    private String className = "com.mysql.jdbc.Driver";
    private String dburl = "jdbc:mysql://localhost/ZJJexe";
    private String user = "root";
    private String password = "root";
    private Connection connection;
    private PreparedStatement statement;
    private ResultSet resultSet;

    public void openConnection() throws ClassNotFoundException, SQLException{
        //加载驱动
        Class.forName(className);
        //创建连接
        connection = DriverManager.getConnection(dburl,user,password);
    }

    //查询方法
    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 boolean excute(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++;
            }
        }
        int updateCount = statement.executeUpdate();

        if (updateCount != 0) {
            return true;
        }
        else{
            return false;
        }

    }
    //释放资源
    public void closeResourse(){
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(statement != null){
                statement.close();
            }
            if(connection != null){
                connection.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

其他就是方法的声明与调用。还有一个实体类,是员工信息:

package com.Seraphjin.Attence.model;

import java.util.Date;

public class attence {
    private int id;
    private String empName;
    private String dept;
    private Date datetime;
    private int 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 getDatetime() {
        return datetime;
    }
    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }

}

OK~

近期我要学会用小乌龟,然后将自己的小项目部署到Github上~加油!

时间: 2024-08-08 16:50:05

作业1:小型考勤登记表的相关文章

SSR———团队作业:小型论坛的设计与实践

小型论坛的设计与实践 这次团队作业,我们设计的是小型论坛的设计与实现,作为团队中的一员,我主要负责对用户进行需求调研与对用户体验的过程拍摄总结,力求我们的项目在力所能及的范围内更加完美 视频:http://202.199.28.1/files/A2010000004208DC/files.cnblogs.com/files/mxxl/%E8%AE%BA%E5%9D%9B_x264.zip(因为视频一开始比较大,所以压缩之后像素有点低,见谅) 需求调研: 首先,因为在我们的校园生活中学生和老师缺少

容易使用的读取文本播放器 Text to Speech Maker 2.5

FilmConvert Stand Alone 1.216 MacOSXAutodesk.Smoke.v2015.SP1.MacOSX 1DVDAutodesk Smoke 2015提供更快的效能和更平易近人的价格专 为以Mac计算机作业的小型工作室设计,Smoke 2015专业影音特效和剪辑工具现在具备了 3D追踪.新的Timeline FX工作流程.针对搭载OS X Mavericks操作系统的新版Mac Pro新增硬件支持和系统运作的最佳化,并与Final Cut Pro X提供更佳的互通

上海交通大学继续教育学院(网络教育)上课时间表

上海交通大学继续教育学院(网络教育)上课时间表专业: 2017 秋业余制专升本计算机科学与技术一. 上课时间表: 9 月 4 日正式开学(第一周) 时间 周次 课程 考核形式 上课教室 每周三上传 1-15 周 程序设计(C) 开卷 请在网上点播.下载学习,并完成相关作业 周一 18:30-21:30 1-15 周 大学英语(一) 闭卷 南楼 100. 103 每周二上传 1-15 周 计算机网络 开卷 请在网上点播.下载学习,并完成相关作业 周四 18:30-21:30 1-11 周 计算机应

商学院

前 言 中南大学以“建设特色鲜明的世界一流大学”为办学目标,主动对接国家重大发展战略需求,为行业和地方社会经济发展服务,坚持将“质量提升”作为构建一流本科教育的核心任务.为进一步深化本科教育教学改革,完善本科人才培养体系,全面提升人才培养质量,学校从2014年3月开始,启动2016版本科人才培养方案的修订工作,同时组织了各教学单位对其开设课程的教学大纲进行修订.教学大纲是实施专业培养方案,实现高校培养目标及要求的教学指导文件,是组织教学过程.进行教学质量评估和实施教学管理的主要依据.制订与本科教

编译原理大作业(用java编写小型GCC 编译器)

以前只用编译器编译程序,现在学完编译原理这门课以后,通过编译大作业,我对编译器的工作原理有了比较清晰的认识 编译器的工作原理 编译器 (Compiler) 是一种将由一种语言编写的程序转换为另一种编程语言的可执行程序. 现代软件对于编译器的需求远甚从前, 究其原因很简单: 作为中间层, 编译器是构建更高层抽象的基础设施. 编译器意欲将人类可阅读的高阶代码, 翻译为机器能运行的低阶代码. 现代编译器的主要工作流程为: 源代码(source code)→ 预处理器(preprocessor)→ 编译

附加作业

1. 你认为本门课程需要在哪里进行改进,具体措施有哪些,包括:时间进度安排,项目难度等均可: 我认为本门课程没有真正发掘出一群真正对编程有强烈兴趣或编程能力特别强的人,我觉得可以将这些人组合真正做出一个大型项目,这样几乎不会出现有人偷懒不做.贡献较少等情况: 解决方法:每组推优,推出贡献较多编程能力强的人,因为毕竟每个人的能力不同,我想如果强强联合的话,或许能发挥出更大的优势:我并不是说编程能力不强就没有其他的优势了,反之,一个团队中不可能只有1~2个开发人员,这样也只能做出小型项目,学习软件工

【个人阅读作业】软件工程M1/M2总结

链接:”看<快速软件开发>的五个问题“ http://www.cnblogs.com/leiyy/p/4027759.html 一.较为明白的问题 1. 在文章的第一个关于Square_Tech的案例中,代码测试和优化都是在所有程序完成以后才进行的,这应该也不符合快速软件开发的要求吧.如果测试工程师在最开始的时候就加入到软件开发中的话,软件开发进程会不会更快呢? 在团队项目之前,虽然并不是特别了解测试工程师的工作内容,但想到既然是软件开发项目中的一个单独列出来的角色,那就肯定大有用处.当初为什

软工第零次作业[补交]

目录 第一部分:结缘计算机    2 i.    你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答)    2 ii.    计算机是你喜欢的领域吗?是你擅长的领域吗?    3 iii.    你热爱这一专业吗?你对计算机的热爱是怎样的?仅仅是口头的吗?    3 第二部分:在计算机系里学习    4 i.    你对你的大学生活有什么想要吐槽的地方吗?你理想的大学教育应该是什么样子的?跟学校给你的有什么区别?比较你在中国大学的经历,你的老师和学校能做到和国外那样吗?如果不能

第1次作业:阅读优秀博文有感

第一部分:结缘计算机 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?                                                                                                                                                                            答:高考结束后因为成绩的不理想所以选择的专业类型很少,加之学的是理科,所以