詳解DAO模式(下)
在上一節我們已經把DAO的開發流程完整講解一遍了,對於剛接觸DAO模式的同學來說可能會比較難懂。不過沒關係,想當初我接觸DAO模式的時候也是一頭霧水,根本不知道整個流程怎麼走的。但好學的我一邊閱讀程序一邊理順程序的脈絡,晚上畫出了第一節那裏的關係圖。雖然還是有點模糊,但是效果確實不錯!而現在再看那幅圖,感覺有一種力量驅使我的腳步,在光滑的地上,摩擦~摩擦~ -_-|||
好,話歸正題!
我們這節主要講的是在JSP中調用DAOFactory類返回的對象進行員工信息的插入和檢索。相信大家對於HTML和JSP都有一定的基礎了吧,所以老夫就簡單描述一下思路,然後貼上代碼就完事了哦!因爲一晚上都在對着電腦寫文章,感覺好激動哦!晚上不能太激動,會睡不着的!
(一)員工信息簡單錄入
先在項目WebRoot下新建一個HTML網頁,主要功能是使用控件獲取用戶錄入的信息。在標籤<form>裏面的action填寫提交的目標網頁,以及員工屬性控件的名稱最好與Emp表裏的屬性一致。HTML代碼如下:
<!DOCTYPE html>
<html>
<head>
<title>empInsert.html</title>
</head>
<body>
<h1>僱員信息添加表</h1>
<form action="insert.jsp" method = "post">
員工編號:<input type = "text" name = "empID"><br>
員工姓名:<input type = "text" name = "empName"><br>
工作類型:<input type = "text" name = "job"><br>
入職日期:<input type = "text" name = "hireDate"><br>
員工工資:<input type = "text" name = "salary"><br>
<input type = "reset" value = "重置"><input type = "submit" value = "提交">
</form>
</body>
</html>
然後再新建一個JSP頁面,用以處理數據庫的信息插入以及顯示錄入的成功與否。要在JSP中使用DAOFactory等等類,同樣也需要導入類文件。導入的位置在JSP的指令處。指令如下:
<%@ page language="java" import="java.util.*" import="java.text.*"
contentType="text/html" pageEncoding="GBK" import="factory.*"
import="vo.*"
%>
JSP網頁代碼如下:
<span style="font-size:18px;"> <%@ page language="java" import="java.util.*" import="java.text.*"
contentType="text/html" pageEncoding="GBK" import="factory.*"
import="vo.*"%>
<%
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>
<title>insert page !</title>
</head>
<%
request.setCharacterEncoding("GBK");
%>
<body>
<%
Emp emp = new Emp();
emp.setEmpID(Integer.parseInt(request.getParameter("empID")));
emp.setEmpName(request.getParameter("empName"));
emp.setJob(request.getParameter("job"));
emp.setHireDate(new SimpleDateFormat("yyyy-mm-dd").parse(request<span style="white-space:pre"> </span>//注意:由於頁面沒有設置格式驗證
.getParameter("hireDate"))); <span style="white-space:pre"> </span> //所以要按照規定的日期格式來填寫
emp.setSalary(Float.parseFloat(request.getParameter("salary")));
%>
<%
try {
if (!DAOFactory.getIEmpDAOInstance().doCreat(emp)) {
%>
<h1>僱員信息添加成功!</h1>
<%
} else {
%>
<h1>僱員信息添加失敗!</h1>
<%
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html></span>
效果圖:
(二)員工信息簡單檢索
該JSP同樣需要導入相關的類文件,爾後可以模糊檢索用戶的姓名和工作,結果以表格顯示。代碼如下:<%@page import="factory.DAOFactory"%>
<%@page import="java.util.List"%>
<%@ page language="java" import="java.util.*" import="vo.*"
import="DAOFactory.*" contentType="text/html" pageEncoding="GBK"%>
<%
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>
<title>Select page !</title>
</head>
<%
request.setCharacterEncoding("GBK");
%>
<body>
<form action="select.jsp" method="post">
<input type="text" name="keyWord">
<input type="submit" value="檢索">
</form>
<%
String keyWord = request.getParameter("keyWord");
try {
List<Emp> all = DAOFactory.getIEmpDAOInstance()
.findAll(keyWord);
Iterator<Emp> iter = all.iterator();
%>
<table border="2">
<tr>
<td>員工編號</td>
<td>員工姓名</td>
<td>工作類型</td>
<td>入職日期</td>
<td>員工工資</td>
</tr>
<%
while (iter.hasNext()) {
Emp emp = iter.next();
%>
<tr>
<td><%=emp.getEmpID()%></td>
<td><%=emp.getEmpName()%></td>
<td><%=emp.getJob()%></td>
<td><%=emp.getHireDate()%></td>
<td><%=emp.getSalary()%></td>
</tr>
<%
}
%>
</table>
<%
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
效果圖: