java、asp.net 通用分页码函数

/*取得数据库记录数*/
    public static int getRecordCount ( String tableName)
    {
        int rowCount = 0;
        try {
            Connection conn = DBUtil.getConnection();
            Statement statement = conn.createStatement();

            ResultSet rset = statement.executeQuery("select count(*) totalCount from "+tableName);
            if (rset.next()) {
                rowCount = rset.getInt("totalCount");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowCount;
    }

//页码总数
    public static int GetPageCount(int paging,String tableName)
    {
        int _page = getRecordCount(tableName) / paging;
        if (_page < ((double)getRecordCount(tableName) / (double)paging)) _page += 1;
        if (_page<1) _page =1;
        return _page;
    }

public static String Paging (int fPage_Ing,String tableName,int fPage_Pading)
    {
        int fPage_Count; //总页数

        String pageStr = "";
        fPage_Count = GetPageCount(fPage_Pading,tableName);
        int p_begin = 0, p_end = 10;
        boolean view_first, view_last;

        if (fPage_Count <= 10)
        {
            p_begin = 0;
            p_end = fPage_Count;
            view_first = false;
            view_last = false;
        }
        else
        {
            p_begin = (fPage_Ing - 1) - 5;
            if (p_begin <= 0)
            {
                p_begin = 0;
                p_end = 10;
                view_first = false;
                view_last = true;
            }
            else
            {
                p_end = p_begin + 10;
                if (p_end >= fPage_Count)
                {
                    p_begin = fPage_Count - 10;
                    p_end = fPage_Count;
                    view_first = true;
                    view_last = false;
                }
                else
                {
                    p_end = p_begin + 10;
                    view_first = true;
                    view_last = true;
                }
            }
        }

        for (int i = p_begin; i < p_end; i++)
        {
            if ((i+1)==fPage_Ing) {
                pageStr += "[<button style=\"outline: none;color:#ffff00;font-size: 1.2rem;border:0px;background:transparent;cursor: pointer\">" +
                        (i + 1) + "</button>] ";
            } else {
                pageStr += "[<button onclick=\"ajaxGetPaging(" + (i+1) + ")\" style=\"outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer\">" +
                        (i + 1) + "</button>] ";

            }
        }
        if (view_first)
        {
            pageStr = "[<button onclick=\"ajaxGetPaging(1)\" style=\"outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer\">"+
                    "1</button>] "+" ... " +pageStr;
        }

        if (view_last)
        {
            pageStr +=" ... [<button onclick=\"ajaxGetPaging("+fPage_Count+")\" style=\"outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer\">"+
                    fPage_Count+"</button>] ";
        }

        pageStr += "&nbsp;&nbsp;&nbsp;&nbsp;";
        return pageStr;
    }

稍加修改,java、asp.net都可用,原理相同,c/s、b/s都一样原理,不过现实方式不同而已。

猜你喜欢

转载自www.cnblogs.com/qiaoke/p/9197369.html
今日推荐