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

 

1.DAO+MVC包

jsp+servlet实现模糊查询和分页效果_第1张图片

jsp+servlet实现模糊查询和分页效果_第2张图片

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 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 findByNameLevelState(String sName,
   int cLevel, int cState, int pageSize, int pageCurrent) {
  List list = new ArrayList();
  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

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 findByNameLevelState(String sName,
   int cLevel, int cState, int pageSize, int pageCurrent) {
  List 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 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("");
  sb.append("");
  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("" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "" +
     "");
  }
  sb.append("
学校名称学校地址联系人联系人电话客户等级合作状态院校领导领导电话操作
"+pojo.getSname()+""+pojo.getSadd()+""+pojo.getSlinkMan()+""+pojo.getSlinkTel()+""+cLevelCode+""+cStateCode+""+pojo.getSleader()+""+pojo.getSleaderTel()+"修改    " + "删除
"); 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(); %>
 
 
 分页操作
 
 
 
查询条件 学校名称: 合作等级: 合作状态:

(本人亲测可以)

 

 

 

8.页面实现效果展示

jsp+servlet实现模糊查询和分页效果_第3张图片

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

你可能感兴趣的:(java基础)