分頁技術

 

一:建Dao

package dao;

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

import java.sql.ResultSet;

import java.util.List;

import java.util.Vector;

 

import bean.FwxxBean;

import db.DBUtil;

 

public class FwxxDao {

      

       private int pageNum=5;

       private int currentPage;

      

       public int getCurrentPage() {

              return currentPage;

       }

 

       public List getAll(int currentPage)

       {

              List list=new Vector();

              this.currentPage=currentPage;

              try

              {

                     DBUtil db=new DBUtil();

                     String sqlText=

                            "select top "+pageNum+" * from view_fwxx where fwid not in(select top "+(currentPage-1)*5+" fwid from view_fwxx)";

                     ResultSet rs=db.executeQ(sqlText, null);

                     while(rs.next())

                     {

                            FwxxBean bean=new FwxxBean();

                            bean.setFwid(rs.getInt("fwid"));

                            bean.setShi(rs.getInt("shi"));

                            bean.setTing(rs.getInt("ting"));

                            list.add(bean);

                     }

                     db.closeAll();

              }catch(Exception ex)

              {

                     ex.printStackTrace();

              }finally

              {

                     return list;

              }

       }

       //獲得總記錄數

       public int getRowTotal()

       {

              int rowTotal=0;

              try

              {

                     DBUtil db=new DBUtil();

                     ResultSet rs=db.executeQ("select Count(*) from view_fwxx", null);

                     rs.next();

                     rowTotal=rs.getInt(1);

                     db.closeAll();

              }catch(Exception ex)

              {

                     ex.printStackTrace();

              }finally

              {

                     return rowTotal;

              }

       }

       //獲得總頁數

       public int getPageTotal()

       {

              int totalPage=this.getRowTotal()/pageNum;

              if(this.getRowTotal()%pageNum!=0)

              {

                     totalPage++;

              }

              System.out.println(this.getRowTotal()+" "+totalPage);

              return totalPage;

       }

}

二:Jsp頁面調用Dao方法

int pageNum=1;

       String pageN=request.getParameter("currentPage");

       if(pageN!=null)

       pageNum=Integer.parseInt(pageN);

       FwxxDao dao=new FwxxDao();

       List lst=dao.getAll(pageNum);

       int current_page=dao.getCurrentPage();

       int page_total=dao.getPageTotal();

       pageContext.setAttribute("fwxx",lst);

       pageContext.setAttribute("current_page", current_page);

       pageContext.setAttribute("page_total", page_total);

三:分頁

<tr>

        <td>標題</td>

        <td>月租金</td>

        <td>發佈日期</td>

        </tr>

         <tr>

        <td colspan="3" >

        <hr>

        </td>

      </tr>

      <logic:present name="fwxx" scope="page">

         <logic:iterate id="data" name="fwxx" scope="page">

           <tr>

            <td>${data.title }</td>

            <td>${data.zj }</td>

            <td>${data.date }</td>

            </tr>

        </logic:iterate>

      </logic:present>

      <tr>

          <td colspan="3" >

        <hr>

        </td>

      </tr>

      <tr>

          <td colspan="3" >

            <a href="list.jsp?currentPage=1">首頁</a>

            <a href="list.jsp?currentPage=${current_page-1 }">上一頁</a>

            <a href="list.jsp?currentPage=${current_page+1 }">下一頁</a>

            <a href="list.jsp?currentPage=${page_total }">尾頁</a>

        </td>

      </tr>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章