作業1:小型考勤登記表
阿新 • • 發佈:2017-07-10
btn ctype parameter edate 加油! tab wid delet basepath
這次在廣州實習了20天,收獲還比較大。不過仍需要繼續努力。這次總共布置了兩個作業,我總結一下:
登記考勤信息,查看信息——主要就是往數據庫增加數據,然後再從數據庫中讀取出來。
代碼演示:
從數據庫裏面寫入數據:
<[email protected] import="com.Seraphjin.Attence.model.attence"%> <[email protected] import="com.Attence.BizImpl.AttenceBizImpl"%> <[email protected] import="com.Attence.biz.AttenceBiz"%> <[email protected] import="java.text.SimpleDateFormat"%> <[email protected] 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:
<[email protected] import="com.Seraphjin.Attence.model.attence"%> <[email protected] import="com.Attence.BizImpl.AttenceBizImpl"%> <[email protected] 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:小型考勤登記表