oracel 分页 查询

Page实体类

import java.io.Serializable;


public class Page implements Serializable {


private static final long serialVersionUID = -2213069645383858323L; 
private int pageNow = 1; // 当前页数 
private int pageSize = 20; // 每页显示记录的条数 
private int totalCount; // 总的记录条数 
private int totalPageCount; // 总的页数 
private int startPos; // 开始位置,从0开始 
private boolean hasFirst;// 是否有首页 
private boolean hasPre;// 是否有前一页 
private boolean hasNext;// 是否有下一页 
private boolean hasLast;// 是否有最后一页 
private int startNum;//开始数
         private int endNum;//结束数
         
         
 
/** 
  * 通过构造函数 传入 总记录数 和 当前页 
  * @param totalCount 
  * @param pageNow 
  */
public Page(int totalCount, int pageNow) { 
  this.totalCount = totalCount; 
  this.pageNow = pageNow; 

/** 
  * 取得总页数,总页数=总记录数/总页数 
  * @return 
  */
public int getTotalPageCount() { 
  totalPageCount = getTotalCount() / getPageSize(); 
  return (totalCount % pageSize == 0) ? totalPageCount 
    : totalPageCount + 1; 

public void setTotalPageCount(int totalPageCount) { 
  this.totalPageCount = totalPageCount; 

public int getPageNow() { 
  return pageNow; 

public void setPageNow(int pageNow) { 
  this.pageNow = pageNow; 

public int getPageSize() { 
  return pageSize; 

public void setPageSize(int pageSize) { 
  this.pageSize = pageSize; 

public int getTotalCount() { 
  return totalCount; 

public void setTotalCount(int totalCount) { 
  this.totalCount = totalCount; 
}
         
         
/** 
  * 取得选择记录的初始位置 
  * @return 
  */
public int getStartPos() { 
  return (pageNow - 1) * pageSize; 

public void setStartPos(int startPos) { 
  this.startPos = startPos; 

/** 
  * 是否是第一页 
  * @return 
  */
public boolean isHasFirst() { 
  return (pageNow == 1) ? false : true; 

public void setHasFirst(boolean hasFirst) { 
  this.hasFirst = hasFirst; 

/** 
  * 是否有首页 
  * @return 
  */
public boolean isHasPre() { 
  // 如果有首页就有前一页,因为有首页就不是第一页 
  return isHasFirst() ? true : false; 

public void setHasPre(boolean hasPre) { 
  this.hasPre = hasPre; 

/** 
  * 是否有下一页 
  * @return 
  */
public boolean isHasNext() { 
  // 如果有尾页就有下一页,因为有尾页表明不是最后一页 
  return isHasLast() ? true : false; 

  
public void setHasNext(boolean hasNext) { 
  this.hasNext = hasNext; 

/** 
  * 是否有尾页 
  * @return 
  */
public boolean isHasLast() { 
  // 如果不是最后一页就有尾页 
  return (pageNow >= getTotalPageCount()) ? false : true; 

  
public void setHasLast(boolean hasLast) { 
  this.hasLast = hasLast; 
}


        
        public void setStartNum(int startNum) {
            this.startNum = startNum;
        }
    
        public int getStartNum() {
            return (this.getPageNow()-1)*pageSize;
        }
    
        public void setEndNum(int endNum) {
            this.endNum = endNum;
        }
    
        public int getEndNum() {
            return this.getPageNow()*pageSize;
        }

}

————————————————————————————————————————————

action层

            SigninHelper helper = new SigninHelper();//dao层

            List<blacklistInfo> list = null;

            int totalCount = 0;
            String content = request.getParameter("content");
            String pageNowold = request.getParameter("pageNow");//获取传过来的'当前页数'
            Page page = null;
            if(content==null){content="";}
            totalCount = helper.queryCount("v_users_blacklist",content);//查询自己的表的总的条数
            int pageNow = pageNowold==null?1:Integer.parseInt(pageNowold);//当前页数
            page = new Page(totalCount,pageNow);

            list = helper.queryBlacklist(page,content);//模糊查询内容

            request.setAttribute("list",list);

            request.setAttribute("page",page);

            request.setAttribute("content",content);

-------------------------------------------------------------------------------------------------------------------

Dao层

    /**
     *查询白名单信息 分页查询
     * @return
     */
    public List<blacklistInfo> queryWhitelist(Page page,String content) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<blacklistInfo> list = null;
    
        try {
            conn = DBConnectionPoolFactory.create().getConnection();
            stmt = conn.createStatement();
            conn.setAutoCommit(false);
            
            String sql=" Select * from( select rownum rm, t.* from v_users_whitelist t where (t.chinese_name like '%"+content+"%' or t.english_name like '%"+content+"%') ) tm where rm > "+page.getStartNum()+" and rm <="+page.getEndNum();
            rs=stmt.executeQuery(sql);
            list = new ArrayList<blacklistInfo>();
            while(rs.next()){
                blacklistInfo bl = new blacklistInfo();
                bl.setId(rs.getString("id"));
                bl.setEmail(rs.getString("email"));
                bl.setChineseName(rs.getString("chinese_name"));
                bl.setEnglishName(rs.getString("english_name"));
                bl.setYear(rs.getString("year"));
                bl.setFlag(rs.getString("flag"));
                list.add(bl);
            }
            return list;
        }catch (Exception e) {
            System.err.println("Exception occur while queryWhitelist in SigninHelper:"+e.toString());
            e.printStackTrace();
            return list;
        }finally{
            if(rs!=null) try{rs.close();}catch(Exception e){}
            if(stmt!=null) try{stmt.close();}catch(Exception e){}
            if(conn!=null) try{conn.close();}catch(Exception e){}
        }
    }

        /**
     *查询总的数量
     * @return
     */
    public int queryCount(String tableName,String content) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Integer count = 0;
        try {
            conn = DBConnectionPoolFactory.create().getConnection();
            stmt = conn.createStatement();
            conn.setAutoCommit(false);
            
            String sql=" select count(*) from "+tableName +" t  where (t.chinese_name like '%"+content+"%' or t.english_name like '%"+content+"%')";
            rs=stmt.executeQuery(sql);
            if(rs!=null&&rs.next()) count=rs.getInt(1);
            return count;
        }catch (Exception e) {
            System.err.println("Exception occur while ssoCheckUser in SigninHelper:"+e.toString());
            e.printStackTrace();
            return count;
        }finally{
            if(rs!=null) try{rs.close();}catch(Exception e){}
            if(stmt!=null) try{stmt.close();}catch(Exception e){}
            if(conn!=null) try{conn.close();}catch(Exception e){}
        }

    }


------------------------------------------------------------------------------------------------------------------

jsp页面    

<%

List<blacklistInfo> list = (List)request.getAttribute("blacklist");
Page p = (Page) request.getAttribute("page");
String action = (String)request.getAttribute("action");
String content = (String)request.getAttribute("content");
if(content==null || content=="null"){
    content = "";
}

%>

<!-- 上一页 页数 下一页-->

<td colspan="5">
         <a href="javascript:JumpPage(<%=1%>)">首页</a>
        <%if(p.isHasPre()){%>
             <a href="javascript:JumpPage(<%=p.getPageNow()-1%>)">上一页</a>
        <%}else{%>
             上一页
        <%}
            for(int j=1;j<=p.getTotalPageCount();j++){
              
                    if(j==p.getPageNow()){//当前页
                    %>
                        <a style="color:red;" href="javascript:JumpPage(<%=j%>)" ><%=j%></a>              
                    <%
                    }else{
                        if((p.getPageNow()-2<=j &&p.getPageNow()+2>=j)){
                    %>
                        <a href="javascript:JumpPage(<%=j%>)" ><%=j%></a>
                    <%
                    }
                   }
            }
        %>
        <%if(p.isHasNext()){%>
           <a href="javascript:JumpPage(<%=p.getPageNow()+1%>)">下一页</a>
        <%}else{%>
            下一页
        <%}%>
        <a href="javascript:JumpPage(<%=p.getTotalPageCount()%>)">尾页</a>
    </td>






<script type="text/javascript" src="script/jquery-1.4.1.js"></script>  

<script type="text/javascript">

$(document).ready(function(){
            $(".btn").click(function(e){
                e.preventDefault()
                var content = document.getElementById('searchcontent').value;
                //alert(content)
                var url = "${pageContext.request.contextPath}/blacklistAction.do?action=<%=action%>&content="+content;
                window.location.href = url;
            })
});
        function JumpPage(pageNow){
            var content = document.getElementById('searchcontent').value;
            ///alert(content)
            var url = "${pageContext.request.contextPath}/blacklistAction.do?action=<%=action%>&pageNow="+pageNow+"&content="+content;
            window.location.href = url;
        }
        
</script>












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值