作业1:小型考勤登记表
Posted Seraphjin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业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上~加油!
以上是关于作业1:小型考勤登记表的主要内容,如果未能解决你的问题,请参考以下文章