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<CustomerResourcePOJO> findByNameLevelState (String sName,int cLevel,int cState,int pageSize,int pageCurrent);
//查询数据笔数
public int findCountByNameLevelState(String sName,int cLevel,int cState);
//查询单笔数据
public CustomerResourcePOJO findByCId(BigDecimal cID);
//修改
public boolean doUpd(CustomerResourcePOJO pojo);
//新增
public boolean doIns(CustomerResourcePOJO pojo);
//删除
public boolean doDel(BigDecimal cID);
}
3.DAO接口方法实现方法
package com.wanczy.dao.impl;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceDAOImpl implements CustomerResourceDAO {
Connection conn ;
public CustomerResourceDAOImpl(Connection conn){
this.conn = conn;
}
public boolean doDel(BigDecimal cID) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "delete from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "insert into customer_resource (c_id, s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel)" +
"values(scott_squence.nextval,?,?,?,?,?,?,?,?)";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "update customer_resource set s_name=?, s_add=?, " +
" s_link_man=?, s_link_tel=?, c_level=?, c_state=? ,s_leader=?, s_leader_tel=? where" +
" c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.setBigDecimal(9, pojo.getCid());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = null;
PreparedStatement pstate = null;
ResultSet res = null;
try {
String sql = "select s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
res = pstate.executeQuery();
while(res.next()){
pojo = new CustomerResourcePOJO(cID,res.getString(1),res.getString(2),
res.getString(3),res.getString(4),res.getInt(5),res.getInt(6),
res.getString(7),res.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = new ArrayList<CustomerResourcePOJO>();
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel from (select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel ,rownum abc "+
" from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
sql.append(" ) where abc>? and abc<=? order by c_level,c_state");
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
pstate.setInt(2, (pageCurrent-1)*pageSize);
pstate.setInt(3, pageCurrent*pageSize);
res = pstate.executeQuery();
while(res.next()){
CustomerResourcePOJO pojo = new CustomerResourcePOJO(res.getBigDecimal(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getInt(6),res.getInt(7),
res.getString(8),res.getString(9));
list.add(pojo);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
//查询单笔数据
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = 0;
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select count(c_id) from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
res = pstate.executeQuery();
while(res.next()){
count = res.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return count;
}
}
4.Pojo实体类(数据库里的字段)
package com.wanczy.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
public class CustomerResourcePOJO implements Serializable {
private BigDecimal cid;
private String sname;
private String sadd;
private String slinkMan;
private String slinkTel;
private int clevel;
private int cstate;
private String sleader;
private String sleaderTel;
public BigDecimal getCid() {
return cid;
}
public void setCid(BigDecimal cid) {
this.cid = cid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSadd() {
return sadd;
}
public void setSadd(String sadd) {
this.sadd = sadd;
}
public String getSlinkMan() {
return slinkMan;
}
public void setSlinkMan(String slinkMan) {
this.slinkMan = slinkMan;
}
public String getSlinkTel() {
return slinkTel;
}
public void setSlinkTel(String slinkTel) {
this.slinkTel = slinkTel;
}
public int getClevel() {
return clevel;
}
public void setClevel(int clevel) {
this.clevel = clevel;
}
public int getCstate() {
return cstate;
}
public void setCstate(int cstate) {
this.cstate = cstate;
}
public String getSleader() {
return sleader;
}
public void setSleader(String sleader) {
this.sleader = sleader;
}
public String getSleaderTel() {
return sleaderTel;
}
public void setSleaderTel(String sleaderTel) {
this.sleaderTel = sleaderTel;
}
//一般构造方法都要写一个带id和一个不带id的,还有一个无参的,方便后面的增删改查以及方法的调用
public CustomerResourcePOJO(BigDecimal cid, String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.cid = cid;
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO( String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO() {
super();
}
}
5.代理类以及工厂类
package com.wanczy.dao.proxy;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.dao.impl.CustomerResourceDAOImpl;
import com.wanczy.pojo.CustomerResourcePOJO;
import com.wanczy.pub.GetConnection;
public class CustomerResourceDAOProxy implements CustomerResourceDAO {
Connection conn = null;
CustomerResourceDAOImpl impl = null;
public CustomerResourceDAOProxy(){
try {
this.conn = GetConnection.getConn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.impl = new CustomerResourceDAOImpl(this.conn);
}
public boolean doDel(BigDecimal cID) {
boolean flag = this.impl.doDel(cID);
this.close();
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doIns(pojo);
this.close();
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doUpd(pojo);
this.close();
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = this.impl.findByCId(cID);
this.close();
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = this.impl.findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
this.close();
return list;
}
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = this.impl.findCountByNameLevelState(sName, cLevel, cState);
this.close();
return count;
}
public void close(){
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.servlet
package com.wanczy.servlet.customerResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wanczy.dao.factory.CustomerResourceDAOFactory;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceQuery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String sName = request.getParameter("sName");
int cLevel = Integer.parseInt(request.getParameter("cLevel"));
int cState = Integer.parseInt(request.getParameter("cState"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
List<CustomerResourcePOJO> list = CustomerResourceDAOFactory.getDAOInstance().findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
int count = CustomerResourceDAOFactory.getDAOInstance().findCountByNameLevelState(sName, cLevel, cState);
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<input type='hidden' id='count' value='"+count+"'/>");
sb.append("<table id='sample_1' class='table table-striped table-bordered table-hover table-checkable order-column'><tr><th>学校名称</th><th>学校地址</th><th>联系人</th><th>联系人电话</th><th>客户等级</th><th>合作状态</th><th>院校领导</th><th>领导电话</th><th>操作</th></tr>");
for(CustomerResourcePOJO pojo : list){
String cLevelCode = "";
if(pojo.getClevel() == 1){
cLevelCode = "高";
}else if(pojo.getClevel() == 2){
cLevelCode = "中";
}else{
cLevelCode = "低";
}
String cStateCode = "";
if(pojo.getCstate() == 1){
cStateCode = "常年合作";
}else if(pojo.getCstate() == 2){
cStateCode = "合作少";
}else{
cStateCode = "近年无合作";
}
sb.append("<tr>" +
"<td>"+pojo.getSname()+"</td>" +
"<td>"+pojo.getSadd()+"</td>" +
"<td>"+pojo.getSlinkMan()+"</td>" +
"<td>"+pojo.getSlinkTel()+"</td>" +
"<td>"+cLevelCode+"</td>" +
"<td>"+cStateCode+"</td>" +
"<td>"+pojo.getSleader()+"</td>" +
"<td>"+pojo.getSleaderTel()+"</td>" +
"<td><a href='#' onclick='goUpdate("+pojo.getCid()+")'>修改</a> " +
"<a href='#' onclick='goDelete("+pojo.getCid()+")'>删除</a></td>" +
"</tr>");
}
sb.append("</table>");
out.print(sb.toString());
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
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包
2.DAO接口方法定义
package com.wanczy.dao;
import java.math.BigDecimal;
import java.util.List;
import com.wanczy.pojo.CustomerResourcePOJO;
public interface CustomerResourceDAO {
/**
*
* @param sName学校名称
* @param cLevel合作等级
* @param cState合作状态
* @param pageSize一页显示数据的笔数
* @param pageCurrent显示的页数
* @return
*/
//根据名字水平状态来查询数据,传入页数及当前页数
public List<CustomerResourcePOJO> findByNameLevelState (String sName,int cLevel,int cState,int pageSize,int pageCurrent);
//查询数据笔数
public int findCountByNameLevelState(String sName,int cLevel,int cState);
//查询单笔数据
public CustomerResourcePOJO findByCId(BigDecimal cID);
//修改
public boolean doUpd(CustomerResourcePOJO pojo);
//新增
public boolean doIns(CustomerResourcePOJO pojo);
//删除
public boolean doDel(BigDecimal cID);
}
3.DAO接口方法实现方法
package com.wanczy.dao.impl;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceDAOImpl implements CustomerResourceDAO {
Connection conn ;
public CustomerResourceDAOImpl(Connection conn){
this.conn = conn;
}
public boolean doDel(BigDecimal cID) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "delete from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "insert into customer_resource (c_id, s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel)" +
"values(scott_squence.nextval,?,?,?,?,?,?,?,?)";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "update customer_resource set s_name=?, s_add=?, " +
" s_link_man=?, s_link_tel=?, c_level=?, c_state=? ,s_leader=?, s_leader_tel=? where" +
" c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.setBigDecimal(9, pojo.getCid());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = null;
PreparedStatement pstate = null;
ResultSet res = null;
try {
String sql = "select s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
res = pstate.executeQuery();
while(res.next()){
pojo = new CustomerResourcePOJO(cID,res.getString(1),res.getString(2),
res.getString(3),res.getString(4),res.getInt(5),res.getInt(6),
res.getString(7),res.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = new ArrayList<CustomerResourcePOJO>();
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel from (select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel ,rownum abc "+
" from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
sql.append(" ) where abc>? and abc<=? order by c_level,c_state");
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
pstate.setInt(2, (pageCurrent-1)*pageSize);
pstate.setInt(3, pageCurrent*pageSize);
res = pstate.executeQuery();
while(res.next()){
CustomerResourcePOJO pojo = new CustomerResourcePOJO(res.getBigDecimal(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getInt(6),res.getInt(7),
res.getString(8),res.getString(9));
list.add(pojo);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
//查询单笔数据
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = 0;
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select count(c_id) from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
res = pstate.executeQuery();
while(res.next()){
count = res.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return count;
}
}
4.Pojo实体类(数据库里的字段)
package com.wanczy.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
public class CustomerResourcePOJO implements Serializable {
private BigDecimal cid;
private String sname;
private String sadd;
private String slinkMan;
private String slinkTel;
private int clevel;
private int cstate;
private String sleader;
private String sleaderTel;
public BigDecimal getCid() {
return cid;
}
public void setCid(BigDecimal cid) {
this.cid = cid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSadd() {
return sadd;
}
public void setSadd(String sadd) {
this.sadd = sadd;
}
public String getSlinkMan() {
return slinkMan;
}
public void setSlinkMan(String slinkMan) {
this.slinkMan = slinkMan;
}
public String getSlinkTel() {
return slinkTel;
}
public void setSlinkTel(String slinkTel) {
this.slinkTel = slinkTel;
}
public int getClevel() {
return clevel;
}
public void setClevel(int clevel) {
this.clevel = clevel;
}
public int getCstate() {
return cstate;
}
public void setCstate(int cstate) {
this.cstate = cstate;
}
public String getSleader() {
return sleader;
}
public void setSleader(String sleader) {
this.sleader = sleader;
}
public String getSleaderTel() {
return sleaderTel;
}
public void setSleaderTel(String sleaderTel) {
this.sleaderTel = sleaderTel;
}
//一般构造方法都要写一个带id和一个不带id的,还有一个无参的,方便后面的增删改查以及方法的调用
public CustomerResourcePOJO(BigDecimal cid, String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.cid = cid;
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO( String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO() {
super();
}
}
5.代理类以及工厂类
package com.wanczy.dao.proxy;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.dao.impl.CustomerResourceDAOImpl;
import com.wanczy.pojo.CustomerResourcePOJO;
import com.wanczy.pub.GetConnection;
public class CustomerResourceDAOProxy implements CustomerResourceDAO {
Connection conn = null;
CustomerResourceDAOImpl impl = null;
public CustomerResourceDAOProxy(){
try {
this.conn = GetConnection.getConn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.impl = new CustomerResourceDAOImpl(this.conn);
}
public boolean doDel(BigDecimal cID) {
boolean flag = this.impl.doDel(cID);
this.close();
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doIns(pojo);
this.close();
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doUpd(pojo);
this.close();
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = this.impl.findByCId(cID);
this.close();
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = this.impl.findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
this.close();
return list;
}
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = this.impl.findCountByNameLevelState(sName, cLevel, cState);
this.close();
return count;
}
public void close(){
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.servlet
package com.wanczy.servlet.customerResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wanczy.dao.factory.CustomerResourceDAOFactory;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceQuery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String sName = request.getParameter("sName");
int cLevel = Integer.parseInt(request.getParameter("cLevel"));
int cState = Integer.parseInt(request.getParameter("cState"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
List<CustomerResourcePOJO> list = CustomerResourceDAOFactory.getDAOInstance().findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
int count = CustomerResourceDAOFactory.getDAOInstance().findCountByNameLevelState(sName, cLevel, cState);
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<input type='hidden' id='count' value='"+count+"'/>");
sb.append("<table id='sample_1' class='table table-striped table-bordered table-hover table-checkable order-column'><tr><th>学校名称</th><th>学校地址</th><th>联系人</th><th>联系人电话</th><th>客户等级</th><th>合作状态</th><th>院校领导</th><th>领导电话</th><th>操作</th></tr>");
for(CustomerResourcePOJO pojo : list){
String cLevelCode = "";
if(pojo.getClevel() == 1){
cLevelCode = "高";
}else if(pojo.getClevel() == 2){
cLevelCode = "中";
}else{
cLevelCode = "低";
}
String cStateCode = "";
if(pojo.getCstate() == 1){
cStateCode = "常年合作";
}else if(pojo.getCstate() == 2){
cStateCode = "合作少";
}else{
cStateCode = "近年无合作";
}
sb.append("<tr>" +
"<td>"+pojo.getSname()+"</td>" +
"<td>"+pojo.getSadd()+"</td>" +
"<td>"+pojo.getSlinkMan()+"</td>" +
"<td>"+pojo.getSlinkTel()+"</td>" +
"<td>"+cLevelCode+"</td>" +
"<td>"+cStateCode+"</td>" +
"<td>"+pojo.getSleader()+"</td>" +
"<td>"+pojo.getSleaderTel()+"</td>" +
"<td><a href='#' onclick='goUpdate("+pojo.getCid()+")'>修改</a> " +
"<a href='#' onclick='goDelete("+pojo.getCid()+")'>删除</a></td>" +
"</tr>");
}
sb.append("</table>");
out.print(sb.toString());
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
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包
2.DAO接口方法定义
package com.wanczy.dao;
import java.math.BigDecimal;
import java.util.List;
import com.wanczy.pojo.CustomerResourcePOJO;
public interface CustomerResourceDAO {
/**
*
* @param sName学校名称
* @param cLevel合作等级
* @param cState合作状态
* @param pageSize一页显示数据的笔数
* @param pageCurrent显示的页数
* @return
*/
//根据名字水平状态来查询数据,传入页数及当前页数
public List<CustomerResourcePOJO> findByNameLevelState (String sName,int cLevel,int cState,int pageSize,int pageCurrent);
//查询数据笔数
public int findCountByNameLevelState(String sName,int cLevel,int cState);
//查询单笔数据
public CustomerResourcePOJO findByCId(BigDecimal cID);
//修改
public boolean doUpd(CustomerResourcePOJO pojo);
//新增
public boolean doIns(CustomerResourcePOJO pojo);
//删除
public boolean doDel(BigDecimal cID);
}
3.DAO接口方法实现方法
package com.wanczy.dao.impl;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceDAOImpl implements CustomerResourceDAO {
Connection conn ;
public CustomerResourceDAOImpl(Connection conn){
this.conn = conn;
}
public boolean doDel(BigDecimal cID) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "delete from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "insert into customer_resource (c_id, s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel)" +
"values(scott_squence.nextval,?,?,?,?,?,?,?,?)";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "update customer_resource set s_name=?, s_add=?, " +
" s_link_man=?, s_link_tel=?, c_level=?, c_state=? ,s_leader=?, s_leader_tel=? where" +
" c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.setBigDecimal(9, pojo.getCid());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = null;
PreparedStatement pstate = null;
ResultSet res = null;
try {
String sql = "select s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
res = pstate.executeQuery();
while(res.next()){
pojo = new CustomerResourcePOJO(cID,res.getString(1),res.getString(2),
res.getString(3),res.getString(4),res.getInt(5),res.getInt(6),
res.getString(7),res.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = new ArrayList<CustomerResourcePOJO>();
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel from (select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel ,rownum abc "+
" from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
sql.append(" ) where abc>? and abc<=? order by c_level,c_state");
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
pstate.setInt(2, (pageCurrent-1)*pageSize);
pstate.setInt(3, pageCurrent*pageSize);
res = pstate.executeQuery();
while(res.next()){
CustomerResourcePOJO pojo = new CustomerResourcePOJO(res.getBigDecimal(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getInt(6),res.getInt(7),
res.getString(8),res.getString(9));
list.add(pojo);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
//查询单笔数据
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = 0;
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select count(c_id) from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
res = pstate.executeQuery();
while(res.next()){
count = res.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return count;
}
}
4.Pojo实体类(数据库里的字段)
package com.wanczy.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
public class CustomerResourcePOJO implements Serializable {
private BigDecimal cid;
private String sname;
private String sadd;
private String slinkMan;
private String slinkTel;
private int clevel;
private int cstate;
private String sleader;
private String sleaderTel;
public BigDecimal getCid() {
return cid;
}
public void setCid(BigDecimal cid) {
this.cid = cid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSadd() {
return sadd;
}
public void setSadd(String sadd) {
this.sadd = sadd;
}
public String getSlinkMan() {
return slinkMan;
}
public void setSlinkMan(String slinkMan) {
this.slinkMan = slinkMan;
}
public String getSlinkTel() {
return slinkTel;
}
public void setSlinkTel(String slinkTel) {
this.slinkTel = slinkTel;
}
public int getClevel() {
return clevel;
}
public void setClevel(int clevel) {
this.clevel = clevel;
}
public int getCstate() {
return cstate;
}
public void setCstate(int cstate) {
this.cstate = cstate;
}
public String getSleader() {
return sleader;
}
public void setSleader(String sleader) {
this.sleader = sleader;
}
public String getSleaderTel() {
return sleaderTel;
}
public void setSleaderTel(String sleaderTel) {
this.sleaderTel = sleaderTel;
}
//一般构造方法都要写一个带id和一个不带id的,还有一个无参的,方便后面的增删改查以及方法的调用
public CustomerResourcePOJO(BigDecimal cid, String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.cid = cid;
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO( String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO() {
super();
}
}
5.代理类以及工厂类
package com.wanczy.dao.proxy;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.dao.impl.CustomerResourceDAOImpl;
import com.wanczy.pojo.CustomerResourcePOJO;
import com.wanczy.pub.GetConnection;
public class CustomerResourceDAOProxy implements CustomerResourceDAO {
Connection conn = null;
CustomerResourceDAOImpl impl = null;
public CustomerResourceDAOProxy(){
try {
this.conn = GetConnection.getConn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.impl = new CustomerResourceDAOImpl(this.conn);
}
public boolean doDel(BigDecimal cID) {
boolean flag = this.impl.doDel(cID);
this.close();
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doIns(pojo);
this.close();
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doUpd(pojo);
this.close();
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = this.impl.findByCId(cID);
this.close();
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = this.impl.findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
this.close();
return list;
}
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = this.impl.findCountByNameLevelState(sName, cLevel, cState);
this.close();
return count;
}
public void close(){
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.servlet
package com.wanczy.servlet.customerResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wanczy.dao.factory.CustomerResourceDAOFactory;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceQuery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String sName = request.getParameter("sName");
int cLevel = Integer.parseInt(request.getParameter("cLevel"));
int cState = Integer.parseInt(request.getParameter("cState"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
List<CustomerResourcePOJO> list = CustomerResourceDAOFactory.getDAOInstance().findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
int count = CustomerResourceDAOFactory.getDAOInstance().findCountByNameLevelState(sName, cLevel, cState);
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<input type='hidden' id='count' value='"+count+"'/>");
sb.append("<table id='sample_1' class='table table-striped table-bordered table-hover table-checkable order-column'><tr><th>学校名称</th><th>学校地址</th><th>联系人</th><th>联系人电话</th><th>客户等级</th><th>合作状态</th><th>院校领导</th><th>领导电话</th><th>操作</th></tr>");
for(CustomerResourcePOJO pojo : list){
String cLevelCode = "";
if(pojo.getClevel() == 1){
cLevelCode = "高";
}else if(pojo.getClevel() == 2){
cLevelCode = "中";
}else{
cLevelCode = "低";
}
String cStateCode = "";
if(pojo.getCstate() == 1){
cStateCode = "常年合作";
}else if(pojo.getCstate() == 2){
cStateCode = "合作少";
}else{
cStateCode = "近年无合作";
}
sb.append("<tr>" +
"<td>"+pojo.getSname()+"</td>" +
"<td>"+pojo.getSadd()+"</td>" +
"<td>"+pojo.getSlinkMan()+"</td>" +
"<td>"+pojo.getSlinkTel()+"</td>" +
"<td>"+cLevelCode+"</td>" +
"<td>"+cStateCode+"</td>" +
"<td>"+pojo.getSleader()+"</td>" +
"<td>"+pojo.getSleaderTel()+"</td>" +
"<td><a href='#' onclick='goUpdate("+pojo.getCid()+")'>修改</a> " +
"<a href='#' onclick='goDelete("+pojo.getCid()+")'>删除</a></td>" +
"</tr>");
}
sb.append("</table>");
out.print(sb.toString());
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
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包
2.DAO接口方法定义
package com.wanczy.dao;
import java.math.BigDecimal;
import java.util.List;
import com.wanczy.pojo.CustomerResourcePOJO;
public interface CustomerResourceDAO {
/**
*
* @param sName学校名称
* @param cLevel合作等级
* @param cState合作状态
* @param pageSize一页显示数据的笔数
* @param pageCurrent显示的页数
* @return
*/
//根据名字水平状态来查询数据,传入页数及当前页数
public List<CustomerResourcePOJO> findByNameLevelState (String sName,int cLevel,int cState,int pageSize,int pageCurrent);
//查询数据笔数
public int findCountByNameLevelState(String sName,int cLevel,int cState);
//查询单笔数据
public CustomerResourcePOJO findByCId(BigDecimal cID);
//修改
public boolean doUpd(CustomerResourcePOJO pojo);
//新增
public boolean doIns(CustomerResourcePOJO pojo);
//删除
public boolean doDel(BigDecimal cID);
}
3.DAO接口方法实现方法
package com.wanczy.dao.impl;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceDAOImpl implements CustomerResourceDAO {
Connection conn ;
public CustomerResourceDAOImpl(Connection conn){
this.conn = conn;
}
public boolean doDel(BigDecimal cID) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "delete from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "insert into customer_resource (c_id, s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel)" +
"values(scott_squence.nextval,?,?,?,?,?,?,?,?)";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = false;
PreparedStatement pstate = null;
try {
this.conn.setAutoCommit(false);
String sql = "update customer_resource set s_name=?, s_add=?, " +
" s_link_man=?, s_link_tel=?, c_level=?, c_state=? ,s_leader=?, s_leader_tel=? where" +
" c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setString(1,pojo.getSname());
pstate.setString(2,pojo.getSadd());
pstate.setString(3,pojo.getSlinkMan());
pstate.setString(4,pojo.getSlinkTel());
pstate.setInt(5,pojo.getClevel());
pstate.setInt(6,pojo.getCstate());
pstate.setString(7,pojo.getSleader());
pstate.setString(8,pojo.getSleaderTel());
pstate.setBigDecimal(9, pojo.getCid());
pstate.execute();//执行
this.conn.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
try {
this.conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
// TODO: handle exception
} finally{
try {
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = null;
PreparedStatement pstate = null;
ResultSet res = null;
try {
String sql = "select s_name, s_add, " +
"s_link_man, s_link_tel, c_level, c_state ,s_leader, s_leader_tel from customer_resource where c_id = ?";
pstate = this.conn.prepareStatement(sql);
pstate.setBigDecimal(1, cID);
res = pstate.executeQuery();
while(res.next()){
pojo = new CustomerResourcePOJO(cID,res.getString(1),res.getString(2),
res.getString(3),res.getString(4),res.getInt(5),res.getInt(6),
res.getString(7),res.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = new ArrayList<CustomerResourcePOJO>();
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel from (select c_id,s_name, s_add, s_link_man, "+
" s_link_tel, c_level, c_state ,s_leader, "+
" s_leader_tel ,rownum abc "+
" from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
sql.append(" ) where abc>? and abc<=? order by c_level,c_state");
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
pstate.setInt(2, (pageCurrent-1)*pageSize);
pstate.setInt(3, pageCurrent*pageSize);
res = pstate.executeQuery();
while(res.next()){
CustomerResourcePOJO pojo = new CustomerResourcePOJO(res.getBigDecimal(1),res.getString(2),res.getString(3),
res.getString(4),res.getString(5),res.getInt(6),res.getInt(7),
res.getString(8),res.getString(9));
list.add(pojo);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return list;
}
//查询单笔数据
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = 0;
PreparedStatement pstate = null;
ResultSet res = null;
try {
StringBuffer sql = new StringBuffer();
sql.append("select count(c_id) from customer_resource where s_name like ? ");
if(cLevel != 0){
sql.append(" and c_level = "+cLevel);
}
if(cState != 0){
sql.append(" and c_state = "+cState);
}
pstate = this.conn.prepareStatement(sql.toString());
pstate.setString(1, "%"+sName+"%");
res = pstate.executeQuery();
while(res.next()){
count = res.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally{
try {
res.close();
pstate.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return count;
}
}
4.Pojo实体类(数据库里的字段)
package com.wanczy.pojo;
import java.io.Serializable;
import java.math.BigDecimal;
public class CustomerResourcePOJO implements Serializable {
private BigDecimal cid;
private String sname;
private String sadd;
private String slinkMan;
private String slinkTel;
private int clevel;
private int cstate;
private String sleader;
private String sleaderTel;
public BigDecimal getCid() {
return cid;
}
public void setCid(BigDecimal cid) {
this.cid = cid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSadd() {
return sadd;
}
public void setSadd(String sadd) {
this.sadd = sadd;
}
public String getSlinkMan() {
return slinkMan;
}
public void setSlinkMan(String slinkMan) {
this.slinkMan = slinkMan;
}
public String getSlinkTel() {
return slinkTel;
}
public void setSlinkTel(String slinkTel) {
this.slinkTel = slinkTel;
}
public int getClevel() {
return clevel;
}
public void setClevel(int clevel) {
this.clevel = clevel;
}
public int getCstate() {
return cstate;
}
public void setCstate(int cstate) {
this.cstate = cstate;
}
public String getSleader() {
return sleader;
}
public void setSleader(String sleader) {
this.sleader = sleader;
}
public String getSleaderTel() {
return sleaderTel;
}
public void setSleaderTel(String sleaderTel) {
this.sleaderTel = sleaderTel;
}
//一般构造方法都要写一个带id和一个不带id的,还有一个无参的,方便后面的增删改查以及方法的调用
public CustomerResourcePOJO(BigDecimal cid, String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.cid = cid;
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO( String sname, String sadd,
String slinkMan, String slinkTel, int clevel, int cstate,
String sleader, String sleaderTel) {
super();
this.sname = sname;
this.sadd = sadd;
this.slinkMan = slinkMan;
this.slinkTel = slinkTel;
this.clevel = clevel;
this.cstate = cstate;
this.sleader = sleader;
this.sleaderTel = sleaderTel;
}
public CustomerResourcePOJO() {
super();
}
}
5.代理类以及工厂类
package com.wanczy.dao.proxy;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.List;
import com.wanczy.dao.CustomerResourceDAO;
import com.wanczy.dao.impl.CustomerResourceDAOImpl;
import com.wanczy.pojo.CustomerResourcePOJO;
import com.wanczy.pub.GetConnection;
public class CustomerResourceDAOProxy implements CustomerResourceDAO {
Connection conn = null;
CustomerResourceDAOImpl impl = null;
public CustomerResourceDAOProxy(){
try {
this.conn = GetConnection.getConn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.impl = new CustomerResourceDAOImpl(this.conn);
}
public boolean doDel(BigDecimal cID) {
boolean flag = this.impl.doDel(cID);
this.close();
return flag;
}
public boolean doIns(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doIns(pojo);
this.close();
return flag;
}
public boolean doUpd(CustomerResourcePOJO pojo) {
boolean flag = this.impl.doUpd(pojo);
this.close();
return flag;
}
public CustomerResourcePOJO findByCId(BigDecimal cID) {
CustomerResourcePOJO pojo = this.impl.findByCId(cID);
this.close();
return pojo;
}
public List<CustomerResourcePOJO> findByNameLevelState(String sName,
int cLevel, int cState, int pageSize, int pageCurrent) {
List<CustomerResourcePOJO> list = this.impl.findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
this.close();
return list;
}
public int findCountByNameLevelState(String sName, int cLevel, int cState) {
int count = this.impl.findCountByNameLevelState(sName, cLevel, cState);
this.close();
return count;
}
public void close(){
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.servlet
package com.wanczy.servlet.customerResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wanczy.dao.factory.CustomerResourceDAOFactory;
import com.wanczy.pojo.CustomerResourcePOJO;
public class CustomerResourceQuery extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String sName = request.getParameter("sName");
int cLevel = Integer.parseInt(request.getParameter("cLevel"));
int cState = Integer.parseInt(request.getParameter("cState"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
List<CustomerResourcePOJO> list = CustomerResourceDAOFactory.getDAOInstance().findByNameLevelState(sName, cLevel, cState, pageSize, pageCurrent);
int count = CustomerResourceDAOFactory.getDAOInstance().findCountByNameLevelState(sName, cLevel, cState);
PrintWriter out = response.getWriter();
StringBuffer sb = new StringBuffer();
sb.append("<input type='hidden' id='count' value='"+count+"'/>");
sb.append("<table id='sample_1' class='table table-striped table-bordered table-hover table-checkable order-column'><tr><th>学校名称</th><th>学校地址</th><th>联系人</th><th>联系人电话</th><th>客户等级</th><th>合作状态</th><th>院校领导</th><th>领导电话</th><th>操作</th></tr>");
for(CustomerResourcePOJO pojo : list){
String cLevelCode = "";
if(pojo.getClevel() == 1){
cLevelCode = "高";
}else if(pojo.getClevel() == 2){
cLevelCode = "中";
}else{
cLevelCode = "低";
}
String cStateCode = "";
if(pojo.getCstate() == 1){
cStateCode = "常年合作";
}else if(pojo.getCstate() == 2){
cStateCode = "合作少";
}else{
cStateCode = "近年无合作";
}
sb.append("<tr>" +
"<td>"+pojo.getSname()+"</td>" +
"<td>"+pojo.getSadd()+"</td>" +
"<td>"+pojo.getSlinkMan()+"</td>" +
"<td>"+pojo.getSlinkTel()+"</td>" +
"<td>"+cLevelCode+"</td>" +
"<td>"+cStateCode+"</td>" +
"<td>"+pojo.getSleader()+"</td>" +
"<td>"+pojo.getSleaderTel()+"</td>" +
"<td><a href='#' onclick='goUpdate("+pojo.getCid()+")'>修改</a> " +
"<a href='#' onclick='goDelete("+pojo.getCid()+")'>删除</a></td>" +
"</tr>");
}
sb.append("</table>");
out.print(sb.toString());
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
7.web页面
<%@page contentType="text/html; charset=utf-8" %>
<% String path=request.getContextPath(); %>
<html>
<head>
<title>分页操作</title>
</head>
<body>
<form name = "f">
<fieldset title="查询">
<legend>
<span width="12%" height="25" class="STYLE1"
style="color: black;">查询条件</span>
</legend>
学校名称:<input type="text" name="sName"/>
合作等级:<select name="cLevel">
<option value="0" selected="selected">全部</option>
<option value="1">高</option>
<option value="2">中</option>
<option value="3">低</option>
</select>
合作状态:<select name="cState">
<option value="0" selected="selected">全部</option>
<option value="1">常年合作</option>
<option value="2">合作少</option>
<option value="3">近年无合作</option>
</select>
<input type="button" value="查询" onclick="query(0)"/>
<input type="button" value="新增" onclick="goAdd()"/>
</fieldset>
</form>
<hr/>
<div id="showTable"></div>
<div align="right">
<input type="button" id="first" value="|<" onclick="query(1)"/>
<input type="button" id="up" value="<" onclick="query(2)"/>
<input type="button" id="next" value=">" onclick="query(3)"/>
<input type="button" id="end" value=">|" onclick="query(4)"/>
<select id="selectPageCurrent" onchange="query(5)">
<option value="3" selected="selected">显示3笔</option>
<option value="5">显示5笔</option>
<option value="10">显示10笔</option>
</select>
<span id="showPageMessage"></span>
</div>
</body>
<script type="text/javascript">
var pageSize = 3;//一页显示的数据笔数
var pageCurrent = 1;//显示的页数
var allCount = 0;//总共的数据笔数
var allPage = 0;//总共数据页数
query(0);
function query(num){
var sName = f.sName.value;
var cLevel = f.cLevel.value;
var cState = f.cState.value;
if(num == 1){
pageCurrent = 1;
}else if(num == 2){
pageCurrent = pageCurrent -1;
}else if(num == 3){
pageCurrent = pageCurrent + 1;
}else if(num == 4){
pageCurrent = allPage;
}else if(num == 5){
pageCurrent = 1;
pageSize = $("#selectPageCurrent").val();//取得每页显示的数据笔数
}
$(document).ready(function(){
//设置提交的路径,和参数
$.post("<%=path %>/CustomerResourceQuery",{"sName":sName,"cLevel":cLevel,"cState":cState,"pageSize":pageSize,"pageCurrent":pageCurrent},
function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
$("#showTable").html(data);//显示Servlet返回的内容
controlButton();
});
});
}
function controlButton(){
allCount = $("#count").val();
if(allCount%pageSize == 0){
allPage = allCount/pageSize
}else{
allPage = Math.floor(allCount/pageSize) +1;
}
document.getElementById("first").disabled = false;
document.getElementById("up").disabled = false;
document.getElementById("next").disabled = false;
document.getElementById("end").disabled = false;
if(allPage == 1){
document.getElementById("first").disabled = true;
document.getElementById("up").disabled = true;
document.getElementById("next").disabled = true;
document.getElementById("end").disabled = true;
}else if(pageCurrent == 1){
document.getElementById("first").disabled = true;
document.getElementById("up").disabled = true;
}else if(pageCurrent == allPage){
document.getElementById("next").disabled = true;
document.getElementById("end").disabled = true;
}
$("#showPageMessage").html("总共"+allCount+"笔数据,当前显示"+pageCurrent+"页,共"+ allPage+"页");
}
function goAdd(){
var width = window.screen.width ;
var height = window.screen.height ;
window.open("add.jsp","新增客户",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}
function goUpdate(cID){
var width = window.screen.width ;
var height = window.screen.height ;
window.open("<%=path%>/CustomerResourceFindByCID?cID="+cID,"修改客户",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}
function goDelete(cID){
if(confirm("确认删除?")){
$(document).ready(function(){
//设置提交的路径,和参数
$.post("<%=path %>/CustomerResourceDel",{"cId":cID},
function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
if(data == "true"){
alert("删除成功");
query(0);
}else{
alert("删除失败,请联系系统管理员");
}
});
});
}
}
</script>
</body>
</html>
8.页面实现效果展示
总结一下吧,mvc+dao设计模式的好处就是实现了java面向对象的思想,接口和方法的实现分开,便于后期的开发和维护,以及功能的增加,通过接口实现类去实现接口中的方法,通过代理类去取得数据库连接池文件及调用方法。
jsp+servlet实现模糊查询和分页效果的更多相关文章
- Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)
本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...
- Thinkphp5 post提交模糊查询带分页如何保留参数
最近做了一个分页的模糊查询post请求,发现查出来的分页点击下一页导致所有的搜索条件被重置,分页效果就失效了. 以下是网上部分解决办法: 控制器代码 public function index($na ...
- 利用DetachedCriteria实现模糊查询和分页
分类: Java-Developing 前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单 ...
- django实战(二)--带多字段模糊查询的分页(也是不容易)
上节我们实现了分页功能,这节我们要实现对模糊查询后的结果进行分页.(引入了bootstrap框架) urls.py from django.urls import path from . import ...
- Neo4j模糊查询及分页查询
Neo4j模糊查询:采用正则方式: MATCH (n:House) where n.Name =~ '李.*' RETURN n 分页: 使用skip 及 limit MATCH (n:House) ...
- Mysql里查询字段为Json格式的数据模糊查询以及分页方法
public void datagrid(CustomFormEntity customForm,HttpServletRequest request, HttpServletResponse res ...
- 【Django+Element UI】使用一个接口文件,搞定分页获取数据,模糊查询后分页获取数据
1:序列化获取数据的接口设计 1:分页获取序列化数据 2:是个能传参数的接口 class Society(APIView): def post(self, request): keywords = s ...
- .net core模糊查询及分页
在项目文件夹中,创建 PaginatedList类,然后用以下代码替换模板代码. using Microsoft.EntityFrameworkCore; using System; using Sy ...
- Bootstrap-table学习笔记(二)——前后端分页模糊查询
在使用过程中,一边看文档一边做,遇到了一些困难的地方,在此记录一下,顺便做个总结: 1,前端分页 2,后端分页 3,模糊查询 前端分页相当简单,在我添加了2w条测试数据的时候打开的很流畅,没有卡顿. ...
随机推荐
- hibernate持久化框架
Hibernate是一个优秀的持久化框架 瞬时状态:保存在内存的程序数据,程序退出后,数据就消失了,称为瞬时状态 持久状态:保存在磁盘上的程序数据,程序退出后依然存在,称为程序数据的持久状态 持久化: ...
- URL解析器urllib2
urllib2是Python的一个库(不用下载,安装,只需要使用时导入import urllib2)它提供了一系列用于操作URL的功能. urlopen urllib2.urlopen可以接受Requ ...
- finally块执行时间
finally块在代码中什么时候被执行? 在Java语言的异常处理中,finally块的作用十九为了保证无论出现什么情况,finally块里面的代码一定会被执行.由于程序执行return就以为这结束对 ...
- 51单片机I/O口直接输入输出实例(附调试及分析过程)
51单片机P0/P1/P2/P3口的区别: P0口要作为低8位地址总线和8位数据总线用,这种情况下P0口不能用作I/O,要先作为地址总线对外传送低8位的地址,然后作为数据总线对外交换数据: P1口只能 ...
- Java 别名(Aliasing)
别名 (Aliasing) 别名,顾名思义,是有别于现在名字的另一个名字,但指的是不是同一个人或事物呢?比如,你上学的时候同学有没有给你起什么外号?如果有的话,你的名字和同学给你起的外号是不是都指的是 ...
- centos7安装httpd和php
centos7许多命令都变了,又要重新记了. centos7默认安装了httpd吧?记不清了,看一下: rpm -qa |grep httpd 没有的话,安装一下吧. yum -y install h ...
- webrtc学习笔记1(建立连接基本流程)
最近在做一个基于webrtc的视频软件,以下是自己对于上层建立通话连接流程的基本理解,记录于此. 假设A和B要建立视频通话,A为房间创建端,B为加入房间端: 1.A通过http登录.获取其他服务器地址 ...
- PHP命名空间理解
这玩意就是路径! 这玩意就是路径! 这玩意就是路径! 这玩意就是路径! 这玩意就是路径! use 就是声明要用某个路径的文件(类) 再有namespace的情况下,就类似于已经在一个路径里了 这个时候 ...
- tomcat的常用配置
1.解決get请求的中文乱码问题 解决办法: 首先找到tomcat路径下的apache-tomcat-7.0.52\conf文件夹,打开server.xml文件,编辑如下内容: <Connect ...
- JavaScript中var变量引用function与直接声明function
今天在h5开发app的过程中遇到了一个js问题,function的执行问题 在js中声明函数function有这两种方法 var A=function(){...} 或者 function A(){. ...