模糊查询分页与主题分页

  <ul class="classlist">
          <%
              //接收新闻主题
              String id = request.getParameter("tid");
              String countSql = "select count(*) from news";
              String pageSql = "select * from (select a.*,rownum mid from news a)b where mid>=? and mid<=?";
              int tid = 1;
              if(id!=null){//要执行 根据主题查询 的 分页
                  tid = Integer.valueOf(id);
                  countSql = "select count(*) from news where tid="+tid;
                  pageSql = "select * from (select a.*,rownum mid from news a where tid="+tid+")b where mid>=? and mid<=?";
              }
          
              //查询新闻总条数
              ps = con.prepareStatement(countSql);
              rs = ps.executeQuery();
              int count = 0;
              if(rs.next()){
                  count = rs.getInt(1);
              }
              
              //获取页面
              String index = request.getParameter("pageIndex");
              int pageIndex = 1;
              if(index!=null){
                pageIndex = Integer.valueOf(index);                  
              }
              //查询新闻
              int pageSize = 5;
              //求出最大页码
              int maxPage = count/pageSize;
              if(count%pageSize != 0){
                  maxPage++;
              }
              int start = (pageIndex-1)*pageSize+1;
              int end = pageIndex*pageSize;
              ps = con.prepareStatement(pageSql);
              ps.setInt(1, start);
              ps.setInt(2, end);
              rs = ps.executeQuery();
              while(rs.next()){
          %>
        <li>
            <a href='newspages/news_add.html'><%=rs.getString("ntitle") %> </a>
            <span> <%=rs.getDate("ndate") %> </span>
        </li>
        <li class='space'></li>
        <%} %>
        <p align="right"> 
            <a href="index.jsp<%
                if(id!=null){
                    out.print("?tid="+tid);
                }
            %>">首页</a> 
            <a href="index.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
                if(id!=null){
                    out.print("&tid="+tid);
                }
            %>">上一页</a>
                &nbsp;当前页数:[<%=pageIndex %>/<%=maxPage %>]&nbsp; 
            <a href="index.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
                    if(id!=null){
                        out.print("&tid="+tid);
                    }
            %>">下一页</a> 
            <a href="index.jsp?pageIndex=<%=maxPage%><%
                if(id!=null){
                    out.print("&tid="+tid);
                }
            %>">末页</a> 
        </p>
      </ul>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzhaifx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值