码迷,mamicode.com
首页 > 其他好文 > 详细

作业1:小型考勤登记表

时间:2017-07-10 22:26:56      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:exe   parse   mysq   状态   throws   doc   upd   eth   except   

这次在广州实习了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上~加油!

作业1:小型考勤登记表

标签:exe   parse   mysq   状态   throws   doc   upd   eth   except   

原文地址:http://www.cnblogs.com/zxcjj/p/7147419.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!