---恢復內容開始---
1.DAO+MVC包


2.DAO接口方法定義
package com.wanczy.dao;
import java.math.BigDecimal;
import java.util.List;
import com.wanczy.pojo.CustomerResourcePOJO;
public interface CustomerResourceDAO {
/**
*
* @param sName學校名稱
* @param cLevel合作等級
* @param cState合作狀態
* @param pageSize一頁顯示數據的筆數
* @param pageCurrent顯示的頁數
* @return
*/
//根據名字水平狀態來查詢數據,傳入頁數及當前頁數
public List<CustomerResourcePOJO> findByNameLevelState (String sName,int cLevel,int cState,int pageSize,int pageCurrent);
//查詢數據筆數
public int findCountByNameLevelState(String sName,int cLevel,int cState);
//查詢單筆數據
public CustomerResourcePOJO findByCId(BigDecimal cID);
//修改
public boolean doUpd(CustomerResourcePOJO pojo);
//新增
public boolean doIns(CustomerResourcePOJO pojo);
//刪除
public boolean doDel(BigDecimal cID);
}
3.DAO接口方法實現方法
package com.wanczy.dao.impl; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.wanczy.dao.CustomerResourceDAO; import com.wanczy.pojo.CustomerResourcePOJO; public class CustomerResourceDAOImpl implements CustomerResourceDAO { Connection conn ; public CustomerResourceDAOImpl(Connection conn){ this.conn = conn; } public boolean doDel(BigDecimal cID) { boolean flag = false; PreparedStatement pstate = null; try { this.conn.setAutoCommit(false); String sql = "delete from customer_resource where c_id = ?"; pstate = this.conn.prepareStatement(sql); pstate.setBigDecimal(1, cID); pstate.execute();//執行 this.conn.commit(); flag = true; } catch (Exception e) { e.printStackTrace(); try { this.conn.rollback(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } // TODO: handle exception } finally{ try { pstate.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } return flag; } public boolean doIns(CustomerResourcePOJO pojo) { boolean flag = false; PreparedStatement pstate = null; try { this.conn.setAutoCommit(false); String sql = "insert into customer_resource (c_id, s_name, s_add, " + "s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel)" + "values(scott_squence.nextval,?,?,?,?,?,?,?,?)"; pstate = this.conn.prepareStatement(sql); pstate.setString(1,pojo.getSname()); pstate.setString(2,pojo.getSadd()); pstate.setString(3,pojo.getSlinkMan()); pstate.setString(4,pojo.getSlinkTel()); pstate.setInt(5,pojo.getClevel()); pstate.setInt(6,pojo.getCstate()); pstate.setString(7,pojo.getSleader()); pstate.setString(8,pojo.getSleaderTel()); pstate.execute();//執行 this.conn.commit(); flag = true; } catch (Exception e) { e.printStackTrace(); try { this.conn.rollback(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } // TODO: handle exception } finally{ try { pstate.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } return flag; } public boolean doUpd(CustomerResourcePOJO pojo) { boolean flag = false; PreparedStatement pstate = null; try { this.conn.setAutoCommit(false); String sql = "update customer_resource set s_name=?, s_add=?, " + " s_link_man=?, s_link_tel=?, c_level=?, c_state=? ,s_leader=?, s_leader_tel=? where" + " c_id = ?"; pstate = this.conn.prepareStatement(sql); pstate.setString(1,pojo.getSname()); pstate.setString(2,pojo.getSadd()); pstate.setString(3,pojo.getSlinkMan()); pstate.setString(4,pojo.getSlinkTel()); pstate.setInt(5,pojo.getClevel()); pstate.setInt(6,pojo.getCstate()); pstate.setString(7,pojo.getSleader()); pstate.setString(8,pojo.getSleaderTel()); pstate.setBigDecimal(9, pojo.getCid()); pstate.execute();//執行 this.conn.commit(); flag = true; } catch (Exception e) { e.printStackTrace(); try { this.conn.rollback(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } // TODO: handle exception } finally{ try { pstate.close(); } catch (Exception e2) { e2.printStackTrace(); // TODO: handle exception } } return flag; } public CustomerResourcePOJO findByCId(BigDecimal cID) { CustomerResourcePOJO pojo = null; PreparedStatement pstate = null; ResultSet res = null; try { String sql = "select s_name, s_add, " + "s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel from customer_resource where c_id = ?"; pstate = this.conn.prepareStatement(sql); pstate.setBigDecimal(1, cID); res = pstate.executeQuery(); while(res.next()){ pojo = new CustomerResourcePOJO(cID,res.getString(1),res.getString(2), res.getString(3),res.getString(4),res.getInt(5),res.getInt(6), res.getString(7),res.getString(8)); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally{ try { res.close(); pstate.close(); } catch (Exception e2) { e2.printStackTrace(); } } return pojo; } public List<CustomerResourcePOJO> findByNameLevelState(String sName, int cLevel, int cState, int pageSize, int pageCurrent) { List<CustomerResourcePOJO> list = new ArrayList<CustomerResourcePOJO>(); PreparedStatement pstate = null; ResultSet res = null; try { StringBuffer sql = new StringBuffer(); sql.append("select c_id,s_name, s_add, s_link_man, "+ " s_link_tel, c_level, c_state ,s_leader, "+ " s_leader_tel from (select c_id,s_name, s_add, s_link_man, "+ " s_link_tel, c_level, c_state ,s_leader, "+ " s_leader_tel ,rownum abc "+ " from customer_resource where s_name like ? "); if(cLevel != 0){ sql.append(" and c_level = "+cLevel); } if(cState != 0){ sql.append(" and c_state = "+cState); } sql.append(" ) where abc>? and abc<=? order by c_level,c_state"); pstate = this.conn.prepareStatement(sql.toString()); pstate.setString(1, "%"+sName+"%"); pstate.setInt(2, (pageCurrent-1)*pageSize); pstate.setInt(3, pageCurrent*pageSize); res = pstate.executeQuery(); while(res.next()){ CustomerResourcePOJO pojo = new CustomerResourcePOJO(res.getBigDecimal(1),res.getString(2),res.getString(3), res.getString(4),res.getString(5),res.getInt(6),res.getInt(7), res.getString(8),res.getString(9)); list.add(pojo); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally{ try { res.close(); pstate.close(); } catch (Exception e2) { e2.printStackTrace(); } } return list; } //查詢單筆數據 public int findCountByNameLevelState(String sName, int cLevel, int cState) { int count = 0; PreparedStatement pstate = null; ResultSet res = null; try { StringBuffer sql = new StringBuffer(); sql.append("select count(c_id) from customer_resource where s_name like ? "); if(cLevel != 0){ sql.append(" and c_level = "+cLevel); } if(cState != 0){ sql.append(" and c_state = "+cState); } pstate = this.conn.prepareStatement(sql.toString()); pstate.setString(1, "%"+sName+"%"); res = pstate.executeQuery(); while(res.next()){ count = res.getInt(1); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally{ try { res.close(); pstate.close(); } catch (Exception e2) { e2.printStackTrace(); } } return count; } }
4.Pojo實體類(數據庫里的字段)
package com.wanczy.pojo; import java.io.Serializable; import java.math.BigDecimal; public class CustomerResourcePOJO implements Serializable { private BigDecimal cid; private String sname; private String sadd; private String slinkMan; private String slinkTel; private int clevel; private int cstate; private String sleader; private String sleaderTel; public BigDecimal getCid() { return cid; } public void setCid(BigDecimal cid) { this.cid = cid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSadd() { return sadd; } public void setSadd(String sadd) { this.sadd = sadd; } public String getSlinkMan() { return slinkMan; } public void setSlinkMan(String slinkMan) { this.slinkMan = slinkMan; } public String getSlinkTel() { return slinkTel; } public void setSlinkTel(String slinkTel) { this.slinkTel = slinkTel; } public int getClevel() { return clevel; } public void setClevel(int clevel) { this.clevel = clevel; } public int getCstate() { return cstate; } public void setCstate(int cstate) { this.cstate = cstate; } public String getSleader() { return sleader; } public void setSleader(String sleader) { this.sleader = sleader; } public String getSleaderTel() { return sleaderTel; } public void setSleaderTel(String sleaderTel) { this.sleaderTel = sleaderTel; } //一般構造方法都要寫一個帶id和一個不帶id的,還有一個無參的,方便后面的增刪改查以及方法的調用 public CustomerResourcePOJO(BigDecimal cid, String sname, String sadd, String slinkMan, String slinkTel, int clevel, int cstate, String sleader, String sleaderTel) { super(); this.cid = cid; this.sname = sname; this.sadd = sadd; this.slinkMan = slinkMan; this.slinkTel = slinkTel; this.clevel = clevel; this.cstate = cstate; this.sleader = sleader; this.sleaderTel = sleaderTel; } public CustomerResourcePOJO( String sname, String sadd, String slinkMan, String slinkTel, int clevel, int cstate, String sleader, String sleaderTel) { super(); this.sname = sname; this.sadd = sadd; this.slinkMan = slinkMan; this.slinkTel = slinkTel; this.clevel = clevel; this.cstate = cstate; this.sleader = sleader; this.sleaderTel = sleaderTel; } public CustomerResourcePOJO() { super(); } }
5.代理類以及工廠類
package com.wanczy.dao.proxy; import java.math.BigDecimal; import java.sql.Connection; import java.util.List; import com.wanczy.dao.CustomerResourceDAO; import com.wanczy.dao.impl.CustomerResourceDAOImpl; import com.wanczy.pojo.CustomerResourcePOJO; import com.wanczy.pub.GetConnection; public class CustomerResourceDAOProxy implements CustomerResourceDAO { Connection conn = null; CustomerResourceDAOImpl impl = null; public CustomerResourceDAOProxy(){ try { this.conn = GetConnection.getConn(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } this.impl = new CustomerResourceDAOImpl(this.conn); } public boolean doDel(BigDecimal cID) { boolean flag = this.impl.doDel(cID); this.close(); return flag; } public boolean doIns(CustomerResourcePOJO pojo) { boolean flag = this.impl.doIns(pojo); this.close(); return flag; } public boolean doUpd(CustomerResourcePOJO pojo) { boolean flag = this.impl.doUpd(pojo); this.close(); return flag; } public CustomerResourcePOJO findByCId(BigDecimal cID) { CustomerResourcePOJO pojo = this.impl.findByCId(cID); this.close(); return pojo; } public List<CustomerResourcePOJO> findByNameLevelState(String sName, int cLevel, int cState, int pageSize, int pageCurrent) { List<CustomerResourcePOJO> list = this.impl.findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent); this.close(); return list; } public int findCountByNameLevelState(String sName, int cLevel, int cState) { int count = this.impl.findCountByNameLevelState(sName, cLevel, cState); this.close(); return count; } public void close(){ try { this.conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
6.servlet package com.wanczy.servlet.customerResource; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wanczy.dao.factory.CustomerResourceDAOFactory; import com.wanczy.pojo.CustomerResourcePOJO; public class CustomerResourceQuery extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String sName = request.getParameter("sName"); int cLevel = Integer.parseInt(request.getParameter("cLevel")); int cState = Integer.parseInt(request.getParameter("cState")); int pageSize = Integer.parseInt(request.getParameter("pageSize")); int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent")); List<CustomerResourcePOJO> list = CustomerResourceDAOFactory.getDAOInstance().findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent); int count = CustomerResourceDAOFactory.getDAOInstance().findCountByNameLevelState(sName, cLevel, cState); PrintWriter out = response.getWriter(); StringBuffer sb = new StringBuffer(); sb.append("<input type='hidden' id='count' value='"+count+"'/>"); sb.append("<table id='sample_1' class='table table-striped table-bordered table-hover table-checkable order-column'><tr><th>學校名稱</th><th>學校地址</th><th>聯系人</th><th>聯系人電話</th><th>客戶等級</th><th>合作狀態</th><th>院校領導</th><th>領導電話</th><th>操作</th></tr>"); for(CustomerResourcePOJO pojo : list){ String cLevelCode = ""; if(pojo.getClevel() == 1){ cLevelCode = "高"; }else if(pojo.getClevel() == 2){ cLevelCode = "中"; }else{ cLevelCode = "低"; } String cStateCode = ""; if(pojo.getCstate() == 1){ cStateCode = "常年合作"; }else if(pojo.getCstate() == 2){ cStateCode = "合作少"; }else{ cStateCode = "近年無合作"; } sb.append("<tr>" + "<td>"+pojo.getSname()+"</td>" + "<td>"+pojo.getSadd()+"</td>" + "<td>"+pojo.getSlinkMan()+"</td>" + "<td>"+pojo.getSlinkTel()+"</td>" + "<td>"+cLevelCode+"</td>" + "<td>"+cStateCode+"</td>" + "<td>"+pojo.getSleader()+"</td>" + "<td>"+pojo.getSleaderTel()+"</td>" + "<td><a href='#' onclick='goUpdate("+pojo.getCid()+")'>修改</a> " + "<a href='#' onclick='goDelete("+pojo.getCid()+")'>刪除</a></td>" + "</tr>"); } sb.append("</table>"); out.print(sb.toString()); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
7.web頁面
<%@page contentType="text/html; charset=utf-8" %>
<% String path=request.getContextPath(); %>
<html>
<head>
<title>分頁操作</title>
</head>
<body>
<form name = "f">
<fieldset title="查詢">
<legend>
<span width="12%" height="25" class="STYLE1"
style="color: black;">查詢條件</span>
</legend>
學校名稱:<input type="text" name="sName"/>
合作等級:<select name="cLevel">
<option value="0" selected="selected">全部</option>
<option value="1">高</option>
<option value="2">中</option>
<option value="3">低</option>
</select>
合作狀態:<select name="cState">
<option value="0" selected="selected">全部</option>
<option value="1">常年合作</option>
<option value="2">合作少</option>
<option value="3">近年無合作</option>
</select>
<input type="button" value="查詢" onclick="query(0)"/>
<input type="button" value="新增" onclick="goAdd()"/>
</fieldset>
</form>
<hr/>
<div id="showTable"></div>
<div align="right">
<input type="button" id="first" value="|<" onclick="query(1)"/>
<input type="button" id="up" value="<" onclick="query(2)"/>
<input type="button" id="next" value=">" onclick="query(3)"/>
<input type="button" id="end" value=">|" onclick="query(4)"/>
<select id="selectPageCurrent" onchange="query(5)">
<option value="3" selected="selected">顯示3筆</option>
<option value="5">顯示5筆</option>
<option value="10">顯示10筆</option>
</select>
<span id="showPageMessage"></span>
</div>
</body>
<script type="text/javascript">
var pageSize = 3;//一頁顯示的數據筆數
var pageCurrent = 1;//顯示的頁數
var allCount = 0;//總共的數據筆數
var allPage = 0;//總共數據頁數
query(0);
function query(num){
var sName = f.sName.value;
var cLevel = f.cLevel.value;
var cState = f.cState.value;
if(num == 1){
pageCurrent = 1;
}else if(num == 2){
pageCurrent = pageCurrent -1;
}else if(num == 3){
pageCurrent = pageCurrent + 1;
}else if(num == 4){
pageCurrent = allPage;
}else if(num == 5){
pageCurrent = 1;
pageSize = $("#selectPageCurrent").val();//取得每頁顯示的數據筆數
}
$(document).ready(function(){
//設置提交的路徑,和參數
$.post("<%=path %>/CustomerResourceQuery",{"sName":sName,"cLevel":cLevel,"cState":cState,"pageSize":pageSize,"pageCurrent":pageCurrent},
function(data){//Servlet執行完之后執行方法,data表示的servlet返回數據內容
$("#showTable").html(data);//顯示Servlet返回的內容
controlButton();
});
});
}
function controlButton(){
allCount = $("#count").val();
if(allCount%pageSize == 0){
allPage = allCount/pageSize
}else{
allPage = Math.floor(allCount/pageSize) +1;
}
document.getElementById("first").disabled = false;
document.getElementById("up").disabled = false;
document.getElementById("next").disabled = false;
document.getElementById("end").disabled = false;
if(allPage == 1){
document.getElementById("first").disabled = true;
document.getElementById("up").disabled = true;
document.getElementById("next").disabled = true;
document.getElementById("end").disabled = true;
}else if(pageCurrent == 1){
document.getElementById("first").disabled = true;
document.getElementById("up").disabled = true;
}else if(pageCurrent == allPage){
document.getElementById("next").disabled = true;
document.getElementById("end").disabled = true;
}
$("#showPageMessage").html("總共"+allCount+"筆數據,當前顯示"+pageCurrent+"頁,共"+ allPage+"頁");
}
function goAdd(){
var width = window.screen.width ;
var height = window.screen.height ;
window.open("add.jsp","新增客戶",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}
function goUpdate(cID){
var width = window.screen.width ;
var height = window.screen.height ;
window.open("<%=path%>/CustomerResourceFindByCID?cID="+cID,"修改客戶",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}
function goDelete(cID){
if(confirm("確認刪除?")){
$(document).ready(function(){
//設置提交的路徑,和參數
$.post("<%=path %>/CustomerResourceDel",{"cId":cID},
function(data){//Servlet執行完之后執行方法,data表示的servlet返回數據內容
if(data == "true"){
alert("刪除成功");
query(0);
}else{
alert("刪除失敗,請聯系系統管理員");
}
});
});
}
}
</script>
</body>
</html>
8.頁面實現效果展示

總結一下吧,mvc+dao設計模式的好處就是實現了java面向對象的思想,接口和方法的實現分開,便於后期的開發和維護,以及功能的增加,通過接口實現類去實現接口中的方法,通過代理類去取得數據庫連接池文件及調用方法。
