從一個簡單專案看java web 開發的整體佈局
阿新 • • 發佈:2019-02-15
需求說明:
效果圖:
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>