<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="登记" /> <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> ${attence.status}</strong></td> </tr> </c:forEach> </table> </div> </body></html>
阅读全文
1 0
- <java EE 项目:Attendance> 从一个简单项目看java web 开发的整体布局
- <java EE 项目:petstore> 从一个简单项目看 java web 如何在本jsp页面上对用户输入的格式进行限制与验证
- Spring boot 搭建一个简单的 Java EE 项目
- 一个简单的java web 项目
- 一个简单的Java Web项目
- 【Java.Web】创建一个简单的Java Web项目
- Java Web项目开发:Eclipse EE+Tomcat+MySQL
- java(Web,EE)项目开发规范参考
- centos中安装并配置Java EE开发环境(可以开发java项目和web项目的环境)
- 使用MyEclipse开发Java EE应用:如何配置一个独立的Java项目
- Java EE开发第十章:tomcat的搭建以及web项目的创建
- java ee项目的小结
- Java EE 中的 Web项目结构
- web项目java EE Module Dependencies
- Java EE中的Web项目的目录文件结构
- java EE IDE web项目使用 tomcat 调试的过程
- 我看java web项目的发布
- java web项目整体异常处理机制
- 关于eclipse中@override报错原因及解决办法
- Anagrams问题(颠倒字母形成的新单词)
- hive中UDF和UDAF使用说明
- 解决caffe debug 1-Pycharm import caffe 报错 no module named caffe
- js 把一个数组分割成 n 个一组
- <java EE 项目:Attendance> 从一个简单项目看java web 开发的整体布局
- gulp--gulp-autoprefixer
- 总结“生成账单”功能的要点
- AngularJS注意事项
- 插件APK中的activity显示的视图,其实是附着在宿主Activity上的
- Ubuntu 16.04下搭建Web服务器
- surface Shader——主贴图、法线贴图、边缘颜色
- 我自己的android studio代码混淆打包参考
- mvc5新特性RouteAttribute特征路由