jsp+servlet实现模糊查询和分页效果
---恢复内容开始---
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
//查询数据笔数
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 ListfindByNameLevelState(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 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 ListfindByNameLevelState(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")); Listlist = 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(" "); out.print(sb.toString()); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
"); 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()+" " + "修改 " + "删除 " + "
7.web页面
<%@page contentType="text/html; charset=utf-8" %> <% String path=request.getContextPath(); %>分页操作