`
lengyue_dick
  • 浏览: 82816 次
  • 性别: Icon_minigender_1
  • 来自: 内蒙古
社区版块
存档分类
最新评论

Java分页代码

阅读更多
package com.chinabyte.common;

import java.util.Vector;

/**
* 公共的列表
*/
public class CommonList extends Vector {

    /**
     * 当前页数
     */
    public int pageNo;
    /**
     * 每页显示记录数
     */
    public int pageSize;
    /**
     * 总页数
     */
    public int pageNum;
    /**
     * 总纪录数
     */
    public int recNum;

    /**
     * 开始记录数
     */
    public int startPos;
    /**
     * 结束记录数
     */
    public int endPos;

    /**
     * 默认页大小
     */
    public final static int defaultPageSize = 20;
    public static final String COMMONLIST_TAG_KEY = "commonlist_tag_key";

    public CommonList() {

    }

    public CommonList(int pageNo, int pageSize) {
        if (pageNo < 0) {
            pageNo = 0;
        }
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }


    /**
     * 计算其它属性
     * @param recNum 总纪录数
     * @param pageNo 当前页
     * @param pageSize 页大小
     */
    public CommonList(int recNum, int pageNo, int pageSize) {
        calculate(recNum, pageNo, pageSize);
    }


    /**
     * 计算其它属性
     * @param recNum 总纪录数
     */
    public void calculate(int recNum) {
        calculate(recNum, pageNo, pageSize);
    }

    /**
     * 计算其它属性
     * @param recNum 总纪录数
     * @param pageNo 当前页
     * @param pageSize 页大小
     */
    public void calculate(int recNum, int pageNo, int pageSize) {
        if (recNum < 1) return;
        if (pageSize == 0) return;
        if (pageNo < 1) pageNo = 1;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.recNum = recNum;

        if (pageSize > 0) {
            if (recNum % pageSize > 0)
                pageNum = recNum / pageSize + 1;
            else
                pageNum = recNum / pageSize;
            if (pageNo > pageNum) this.pageNo = pageNum;
            if (this.pageNo < 1) this.pageNo = 1;
            startPos = (this.pageNo - 1) * pageSize + 1;
            endPos = this.pageNo * pageSize;
        } else {
            startPos = 0;
            endPos = recNum;
        }
    }


    /**
     *  显示分页标志
     * @param cl 列表
     * @return 结果
     */
    public static String getPage(CommonList cl) {
        return getPage(cl, 10, null);
    }

    /**
     *  显示分页标志
     * @param cl 列表
     * @param pageNum 显示的数目
     * @return 结果
     */
    public static String getPage(CommonList cl, int pageNum) {
        return getPage(cl, pageNum, null);
    }

    /**
     *  显示分页标志
     * @param cl 列表
     * @param pageNum 显示的数目
     * @param strPage 分页参数
     * @return 结果
     */
    public static String getPage(CommonList cl, int pageNum, String strPage) {
        String rValue = null;
        StringBuffer sb = new StringBuffer();
        try {
            if (cl != null) {
                if (strPage != null && !strPage.equals(""))
                    strPage = ",\"" + strPage + "\"";
                else
                    strPage = "";
                if (pageNum < 1) pageNum = 10;
                sb.append("共有[<font color='#FF0000'>" + cl.recNum + "</font>]条记录," + cl.pageNo + "/" + cl.pageNum + "页。");
                if (cl.pageNo > 1) {
                    sb.append(" <a href='" + strPage + ")'>|&lt;</a> <a href='" + (cl.pageNo - 1) + strPage + ")'>&lt;</a>");
                }

                int currentNum = (cl.pageNo % pageNum == 0 ? (cl.pageNo / pageNum) - 1 : (int) (cl.pageNo / pageNum)) * pageNum;
                if (currentNum < 0) currentNum = 0;
                if (cl.pageNo > pageNum) sb.append(" <a href='" + (currentNum - pageNum + 1) + strPage + ")'>...</a>");

                for (int i = 0; i < pageNum; i++) {
                    if ((currentNum + i + 1) > cl.pageNum || cl.pageNum < 2) break;
                    sb.append(" <a href='" + (currentNum + i + 1) + strPage + ")'>" + (currentNum + i + 1 == cl.pageNo ? "<font color='#FF0000'><b>" + (currentNum + i + 1) + "</b></font>" : (currentNum + i + 1) + "") + "</a>");
                }

                if (cl.pageNum > (currentNum + pageNum)) sb.append(" <a href='" + (currentNum + 1 + pageNum) + strPage + ")'>...</a>");

                if (cl.pageNo < cl.pageNum) {
                    sb.append(" <a href='" + (cl.pageNo + 1) + strPage + ")'>&gt;</a> <a href='" + cl.pageNum + strPage + ")'>&gt;|</a>");
                }
                rValue = sb.toString();
            } else {
                rValue = "";
            }
        } catch (Exception e) {
            rValue = "";
        } finally{
            sb = null;
        }
//        cl = null;
        return rValue;
    }

    public String toString() {
        return "com.chinabyte.common.CommonList{" +
                "pageNo=" + pageNo +
                ", pageSize=" + pageSize +
                ", pageNum=" + pageNum +
                ", recNum=" + recNum +
                ", startPos=" + startPos +
                ", endPos=" + endPos +
                "}";
    }


    /*请保留下面的注释*/
/* js 翻页代码
    function tunePage(toPageNo,pageNo) {
        try {
            var topage = 1;
            if(typeof(toPageNo) != "number" || toPageNo < 1) topage = 1;
            else topage = toPageNo;
            var olds = window.location.searchSubject;
            if(typeof(pageNo) == "undefined" || pageNo == "") pageNo = "pageNo";
            var news = "";
            if(olds.length > 1) {
                olds = olds.substring(1,olds.length);
                var arrays = olds.split("&");
                for (var i = 0; i < arrays.length ; i++)
                {
                    if(arrays[i].indexOf(pageNo + "=") < 0 && arrays[i].length > 1) {
                        news += "&" + arrays[i];
                    }
                }
                if(news.length > 1) {
                    news = "?" + news.substring(1,news.length) + "&" + pageNo + "=" + topage;
                }
                else {
                    news = "?" + pageNo + "=" + topage;
                }
            }
            else {
                news = "?" + pageNo + "=" + topage;
            }
            window.location = window.location.pathname + news;
        }
        catch(e) {
            window.location = window.location.pathname + window.location.searchSubject;
        }
    }

    function sAll(thisObj,dObj) {
        try {
            var l = eval(dObj + ".length");
            if(typeof(l) == "undefined") {
                eval(dObj).checked = thisObj.checked;
            }
            else {
                for(var i = 0; i < l; i++) {
                    if(!eval(dObj + "[" + i + "]").disabled) eval(dObj + "[" + i + "]").checked = thisObj.checked;
                }
            }
        }
        catch(e){}
    }

*/
}



























JSP代码:

CommonList zxcl=db.search(1,"","",phototypename,"" , "" , orderby , ascdesc , pageNo,  pageSize);

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="D6E2FF">
  <tr>
<form method="get"> 
      <td align="right"><%=CommonList.getPage(cl)%></td>
</form>  
</tr>
</table>







DB.JAVA代码:

public CommonList search(int status, String playername, String pictitle, String phototypename, String beginDate, String endDate, String orderby,
            String ascdesc, int pageNo, int pageSize)
        throws Exception
    {
        CommonList lst;
        Connection conn;
        PreparedStatement st;
        ResultSet rst;
        Exception exception;
        lst = null;
        StringBuffer buffer = new StringBuffer(1024);
        if(status > -1)
            buffer.append(" and (status=" + status + ")");
        if(playername != null && playername.trim().length() > 0)
            buffer.append(" and (playername like '%" + playername + "%')");
        if(pictitle != null && pictitle.trim().length() > 0)
            buffer.append(" and (pictitle like '%" + pictitle + "%')");
        if(phototypename != null && phototypename.trim().length() > 0)
            buffer.append(" and (phototypename = '" + phototypename + "')");
        if(beginDate != null && beginDate.length() > 0)
            buffer.append(" and (inputdate>=to_date('" + beginDate + "','yyyy-mm-dd'))");
        if(endDate != null && endDate.length() > 0)
        {
            endDate = endDate + " 23:59:59";
            buffer.append(" and (inputdate<=to_date('" + endDate + "','yyyy-mm-dd HH24:MI:SS'))");
        }
        String str_orderby = "";
        if(orderby != null && orderby.trim().length() > 0)
            str_orderby = " order by " + orderby;
        else
            str_orderby = " order by inputdate ";
        if(ascdesc != null && ascdesc.trim().length() > 0)
            str_orderby = str_orderby + "  " + ascdesc;
        else
            str_orderby = str_orderby + " desc ";
        String wherestr = buffer.length() > 0 ? buffer.replace(1, 5, " where ").toString() : "";
        String countSQL = "select count(*) from furniturepicture " + wherestr;
        String querySQL = "select * from (select a.*,rownum rowno from (select   *       from furniturepicture  " + wherestr + str_orderby + "  ) a) where rowno between ? and ?";
        int recordCount = 0;
        int pageCount = 0;
        conn = null;
        st = null;
        rst = null;
        try
        {
            conn = Conn.getConnection("search");
            conn.setAutoCommit(false);
            st = conn.prepareStatement(countSQL);
            rst = st.executeQuery();
            if(rst.next())
                recordCount = rst.getInt(1);
            st.close();
            st = null;
            rst.close();
            rst = null;
            pageCount = recordCount / pageSize;
            if(recordCount % pageSize != 0)
                pageCount++;
            if(pageNo < 1)
                pageNo = 1;
            else
            if(pageNo > pageCount)
            {
                pageNo = pageCount;
                if(pageNo < 1)
                    pageNo = 1;
            }
            lst = new CommonList(recordCount, pageNo, pageSize);
            st = conn.prepareStatement(querySQL);
            st.setInt(1, pageSize * (pageNo - 1) + 1);
            st.setInt(2, pageSize * pageNo);
            FurniturePicture obj;
            for(rst = st.executeQuery(); rst.next(); lst.add(obj))
            {
                obj = new FurniturePicture();
                obj.setFurniturepictureid(rst.getLong("furniturepictureid"));
                obj.setPlayerid(rst.getLong("playerid"));
                obj.setPlayername(rst.getString("playername"));
                obj.setIdcard(rst.getString("idcard"));
                obj.setTel(rst.getString("tel"));
                obj.setEmail(rst.getString("email"));
                obj.setAddress(rst.getString("address"));
                obj.setPassportpin(rst.getString("passportpin"));
                obj.setPictitle(rst.getString("pictitle"));
                obj.setPicnote(rst.getString("picnote"));
                obj.setPhototypeid(rst.getInt("phototypeid"));
                obj.setPhototypename(rst.getString("phototypename"));
                obj.setHousetype(rst.getString("housetype"));
                obj.setPicurl(rst.getString("picurl"));
                obj.setMaterialurllist(rst.getString("materialurllist"));
                obj.setVotenum(rst.getInt("votenum"));
                obj.setHitnum(rst.getInt("hitnum"));
                obj.setStatus(rst.getInt("status"));
                obj.setInputdate(rst.getString("inputdate"));
            }

            conn.commit();
        }
        catch(Exception ex)
        {
            conn.rollback();
            System.out.println("Exception : " + ex.getMessage());
            throw ex;
        }
        finally { }
        try
        {
            conn.setAutoCommit(true);
        }
        catch(SQLException ex) { }
        try
        {
            if(rst != null)
                rst.close();
        }
        catch(Exception e) { }
        try
        {
            if(st != null)
                st.close();
        }
        catch(Exception e) { }
        try
        {
            Conn.close(conn);
        }
        catch(Exception e) { }
        break MISSING_BLOCK_LABEL_1191;
        try
        {
            conn.setAutoCommit(true);
        }
        catch(SQLException ex) { }
        try
        {
            if(rst != null)
                rst.close();
        }
        catch(Exception e) { }
        try
        {
            if(st != null)
                st.close();
        }
        catch(Exception e) { }
        try
        {
            Conn.close(conn);
        }
        catch(Exception e) { }
        throw exception;
        return lst;
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics