jsp+servlet实现模糊查询和分页效果

时间:2022-04-03 14:58:56

---恢复内容开始---

1.DAO+MVC包

jsp+servlet实现模糊查询和分页效果

jsp+servlet实现模糊查询和分页效果

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>&nbsp;&nbsp;&nbsp;&nbsp;" +
       "<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);
 }

}

6.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>

---恢复内容结束---

1.DAO+MVC包

jsp+servlet实现模糊查询和分页效果

jsp+servlet实现模糊查询和分页效果

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>&nbsp;&nbsp;&nbsp;&nbsp;" +
       "<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);
 }

}

6.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>

---恢复内容开始---

1.DAO+MVC包

jsp+servlet实现模糊查询和分页效果

jsp+servlet实现模糊查询和分页效果

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>&nbsp;&nbsp;&nbsp;&nbsp;" +
       "<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);
 }

}

6.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>

---恢复内容结束---

1.DAO+MVC包

jsp+servlet实现模糊查询和分页效果

jsp+servlet实现模糊查询和分页效果

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>&nbsp;&nbsp;&nbsp;&nbsp;" +
       "<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.页面实现效果展示

jsp+servlet实现模糊查询和分页效果

  总结一下吧,mvc+dao设计模式的好处就是实现了java面向对象的思想,接口和方法的实现分开,便于后期的开发和维护,以及功能的增加,通过接口实现类去实现接口中的方法,通过代理类去取得数据库连接池文件及调用方法。