「java:分页代码」java分页显示

博主:adminadmin 2023-01-28 02:18:07 457

本篇文章给大家谈谈java:分页代码,以及java分页显示对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

谁能给我个完整的java 分页代码 谢谢了

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.lqh.dao.db.DBCon;

public class PageDAO {

public static final String Text = "text";

public static final String Image = "image";

public static final String BbsText = "bbstext";

public static final String BbsImage = "bbsimage";

private HttpServletRequest request;

private int currentpage = 1; // 当前是第几页

private int pagecount = 0; // 一共有多少页

private int rscount = 0; // 一共有多少行

private int pagesize = 10; // 每页有多少行[默认为20行]

public PageDAO(HttpServletRequest request) {

this.request = request;

}

public int getCurrentpage() {

return currentpage;

}

public void setCurrentpage(int currentpage) {

this.currentpage = currentpage;

}

public int getPagecount() {

return pagecount;

}

public void setPagecount(int pagecount) {

this.pagecount = pagecount;

}

public int getPagesize() {

return pagesize;

}

public void setPagesize(int pagesize) {

this.pagesize = pagesize;

}

public int getRscount() {

return rscount;

}

public void setRscount(int rscount) {

this.rscount = rscount;

}

/**

* 传入SQL语句获取总记录数

*/

public int getRsCountForRs(String sql) {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

DBCon dbcon=new DBCon();

try {

conn = dbcon.getConn();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

if (rs.next()) {

rs.last();

this.rscount = rs.getRow();

} else {

this.rscount = 0;

}

} catch (Exception ex) {

ex.printStackTrace();

this.rscount = 0;

} finally {

dbcon.tryClose(rs, ps, conn);

}

return this.rscount;

}

public int getRsCountForSQL(String sql) {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

DBCon dbcon=new DBCon();

try {

conn = dbcon.getConn();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

if (rs.next()) {

this.rscount = rs.getInt("rscount");

} else {

this.rscount = 0;

}

} catch (Exception ex) {

ex.printStackTrace();

this.rscount = 0;

} finally {

dbcon.tryClose(rs, ps, conn);

}

return this.rscount;

}

/**

* 获取总页数

*

* @return int

*/

public int getPageCount() {

try {

this.pagecount = ((this.rscount - 1) / this.pagesize) + 1;

} catch (Exception ex) {

this.pagecount = 0;

}

return this.pagecount;

}

/**

* 获取当前页码的设置

*

* @return int

*/

public int getCurrentPage() {

try {

if (this.request.getParameter("currentpage") != null

Integer.parseInt(this.request

.getParameter("currentpage")) 1) {

this.currentpage = Integer.parseInt(this.request

.getParameter("currentpage"));

} else {

this.currentpage = 1;

}

} catch (Exception ex) {

this.currentpage = 1;

}

return this.currentpage;

}

/**

* 分页工具条

*

* @param fileName

* String

* @return String

*/

public String pagetool(String flag) {

StringBuffer str = new StringBuffer();

String url = this.getParamUrl();

int ProPage = this.currentpage - 1;

int Nextpage = this.currentpage + 1;

// 文字的分页

if (flag.equals(PageDAO.Text)) {

str.append("form method='post' name='pageform' action=''");

str

.append("table style='color: windowframe' width='100%' border='0' cellspacing='0' cellpadding='0'");

str.append("tr");

str.append("td width='20%'/td");

str.append("td height='26'");

str.append("共有记录" + this.rscount + "条 ");

str.append("共" + this.pagecount + "页 ");

str.append("每页" + this.pagesize + "记录 ");

str.append("现在" + this.currentpage + "/" + this.pagecount + "页");

str.append("/tdtd");

if (this.currentpage 1) {

str.append("a href='" + url + "currentpage=1'首页/a");

str.append(" ");

str.append("a href='" + url + "currentpage=" + ProPage

+ "'上一页/a");

str.append(" ");

} else {

str.append("首页");

str.append(" ");

str.append("上一页");

str.append(" ");

}

if (this.currentpage this.pagecount) {

str.append("a href='" + url + "currentpage=" + Nextpage

+ "'下一页/a");

str.append(" ");

} else {

str.append("下一页");

str.append(" ");

}

if (this.pagecount 1 this.currentpage != this.pagecount) {

str.append("a href='" + url + "currentpage=" + pagecount

+ "'尾页/a");

str.append(" ");

} else {

str.append("尾页");

str.append(" ");

}

str.append("转到");

str

.append("select name='currentpage' onchange='javascript:ChangePage(this.value);'");

for (int j = 1; j = pagecount; j++) {

str.append("option value='" + j + "'");

if (currentpage == j) {

str.append("selected");

}

str.append("");

str.append("" + j + "");

str.append("/option");

}

str.append("/select页");

str.append("/tdtd width='3%' /td/tr/table");

str.append("script language='javascript'");

str.append("function ChangePage(testpage){");

str.append("document.pageform.action='" + url

+ "currentpage='+testpage+'';");

str.append("document.pageform.submit();");

str.append("}");

str.append("/script");

str.append("/form");

} else if (flag.equals(PageDAO.Image)) {

/**

* 图片的分页

*/

} else if (flag.equals(PageDAO.BbsText)) {

/**

* 论坛形式的分页[直接以数字方式体现]

*/

str

.append("table width='100%' border='0' cellspacing='0' cellpadding='0'");

str.append("tr");

str.append("td width='3%' /td");

str.append("td height='26'");

str.append("记录" + this.rscount + "条  ");

str.append("共" + this.pagecount + "页  ");

str.append("每页" + this.pagesize + "记录  ");

str.append("现在" + this.currentpage + "/" + this.pagecount + "页");

str.append("/tdtd");

// 设定是否有首页的链接

if (this.currentpage 1) {

str.append("a href='" + url + "currentpage=1'首页/a");

str.append("  ");

}

// 设定是否有上一页的链接

if (this.currentpage 1) {

str.append("a href='" + url + "currentpage=" + ProPage

+ "'上一页/a");

str.append("   ");

}

// 如果总页数只有10的话

if (this.pagecount = 10) {

for (int i = 1; i = this.pagecount; i++) {

if (this.currentpage == i) {

str.append("font color=red[" + i

+ "]/font  ");

} else {

str.append("a href='" + url + "currentpage=" + i

+ "'" + i + "/a  ");

}

}

} else {

// 说明总数有超过10页

// 制定特环的开始页和结束页

int endPage = this.currentpage + 4;

if (endPage this.pagecount) {

endPage = this.pagecount;

}

int startPage = 0;

if (this.pagecount = 8 this.currentpage = 8) {

startPage = this.currentpage - 5;

} else {

// 表示从第一页开始算

startPage = 1;

}

System.out.println(startPage);

System.out.println(endPage);

for (int i = startPage; i = endPage; i++) {

if (this.currentpage == i) {

str.append("font color=red[" + i

+ "]/font  ");

} else {

str.append("a href='" + url + "currentpage=" + i

+ "'" + i + "/a  ");

}

}

}

// 设定是否有下一页的链接

if (this.currentpage this.pagecount) {

str.append("a href='" + url + "currentpage=" + Nextpage

+ "'下一页/a");

str.append("  ");

}

// 设定是否有尾页的链接

if (this.pagecount 1 this.currentpage != this.pagecount) {

str.append("a href='" + url + "currentpage=" + pagecount

+ "'尾页/a");

str.append("  ");

}

str.append("/tdtd width='3%' /td/tr/table");

} else if (flag.equals(PageDAO.BbsImage)) {

/**

* 论坛形式的分页[以图片的方式体现]

*/

// 设定分页显示的CSS

str.append("style");

str

.append("BODY {FONT-SIZE: 12px;FONT-FAMILY:宋体;WIDTH: 60%; PADDING-LEFT: 25px;}");

str

.append("DIV.meneame {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; FONT-SIZE: 80%; PADDING-BOTTOM: 3px; MARGIN: 3px; COLOR: #ff6500; PADDING-TOP: 3px; TEXT-ALIGN: center}");

str

.append("DIV.meneame A {BORDER-RIGHT: #ff9600 1px solid; PADDING-RIGHT: 7px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #ff9600 1px solid; PADDING-LEFT: 7px; BACKGROUND-IMAGE: url('"

+ this.request.getContextPath()

+ "/meneame.jpg'); PADDING-BOTTOM: 5px; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff9600 1px solid; TEXT-DECORATION: none}");

str

.append("DIV.meneame A:hover {BORDER-RIGHT: #ff9600 1px solid; BORDER-TOP: #ff9600 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; BORDER-BOTTOM: #ff9600 1px solid; BACKGROUND-COLOR: #ffc794}");

str

.append("DIV.meneame SPAN.current {BORDER-RIGHT: #ff6500 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ff6500 1px solid; PADDING-LEFT: 7px; FONT-WEIGHT: bold; PADDING-BOTTOM: 5px; BORDER-LEFT: #ff6500 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff6500 1px solid; BACKGROUND-COLOR: #ffbe94}");

str

.append("DIV.meneame SPAN.disabled {BORDER-RIGHT: #ffe3c6 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ffe3c6 1px solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 5px; BORDER-LEFT: #ffe3c6 1px solid; COLOR: #ffe3c6; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ffe3c6 1px solid}");

str.append("/style");

str.append("div class=\"meneame\"");

// 判定是否有上一页

if (this.currentpage 1) {

str.append("a href='" + url

+ "currentpage=1' hidefocus=\"true\"首页/a");

str.append("   ");

str.append("a href='" + url + "currentpage=" + ProPage

+ "' hidefocus=\"true\"上一页/a");

str.append("   ");

} else {

str.append("span class=\"disabled\"首页/span");

str.append("  ");

str.append("span class=\"disabled\"上一页/span");

str.append("  ");

}

// 显示中间的图片

if (this.pagecount = 10) {

for (int i = 1; i = this.pagecount; i++) {

if (this.currentpage == i) {

str.append("span class=\"current\"" + i + "/span");

} else {

str.append("a href='" + url + "currentpage=" + i

+ "' hidefocus=\"true\"" + i

+ "/a  ");

}

}

} else {

// 说明总数有超过10页

// 制定特环的开始页和结束页

int endPage = this.currentpage + 4;

if (endPage this.pagecount) {

endPage = this.pagecount;

}

int startPage = 0;

if (this.pagecount = 8 this.currentpage = 8) {

startPage = this.currentpage - 5;

} else {

// 表示从第一页开始算

startPage = 1;

}

System.out.println(startPage);

System.out.println(endPage);

for (int i = startPage; i = endPage; i++) {

if (this.currentpage == i) {

str.append("span class=\"current\"" + i + "/span");

} else {

str.append("a href='" + url + "currentpage=" + i

+ "' hidefocus=\"true\"" + i

+ "/a  ");

}

}

}

// 判断下一页和尾页

if (this.currentpage this.pagecount) {

if (this.currentpage this.pagecount - 10) {

str.append("...");

str.append("a href='" + url + "currentpage="

+ (this.pagecount - 1) + "' hidefocus=\"true\""

+ (this.pagecount - 1) + "/a  ");

str.append("a href='" + url + "currentpage="

+ this.pagecount + "' hidefocus=\"true\""

+ this.pagecount + "/a  ");

}

str.append("a href='" + url + "currentpage=" + Nextpage

+ "' hidefocus=\"true\"下一页/a");

str.append("  ");

} else {

str.append("span class=\"disabled\"下一页/span");

str.append("  ");

}

if (this.pagecount 1 this.currentpage != this.pagecount) {

str.append("a href='" + url + "currentpage=" + pagecount

+ "' hidefocus=\"true\"尾页/a");

str.append("  ");

} else {

str.append("span class=\"disabled\"尾页/span");

str.append("  ");

}

str.append("/div");

}

return str.toString();

}

public String getParamUrl() {

String url = "";

url = this.request.getRequestURI().toString();

if (url.indexOf("?") == -1) {

url = url + "?";

}

String totalParams = "";

Enumeration params = this.request.getParameterNames();// 得到所有参数名

while (params.hasMoreElements()) {

String tempName = params.nextElement().toString();

String tempValue = this.request.getParameter(tempName);

if (tempValue != null !tempValue.equals("")

!tempName.equals("currentpage")) {

if (totalParams.equals("")) {

totalParams = totalParams + tempName + "=" + tempValue;

} else {

totalParams = totalParams + "" + tempName + "="

+ tempValue;

}

}

}

String totalUrl = url + totalParams;

return totalUrl;

}

}

java分页

分页想清楚了就没什么难的了。一般有两种(我就知道两种):

1.数据库分页:

/**

* @param pageItems:一页显示条数 currentPage:当前第几页

* @autor godelegant

*/

public List findAll(int pageItems,int currentPage){

StringBuffer sqlStr = new StringBuffer("select * from Product limit ?,?");

...

...

int startIndex = (currentPage-1)*pageItems;);//(currentPage-1)*pageItems能过当前页和页面记录数得到应该从哪条开始取

int endIndex = startIndex+pageItems;

db.getPstmt().setInt(1,startIndex);

db.getPstmt().setInt(2,endIndex);

ResultSet rs = db.getPstmt().executQuery();//假设你已经得到了数据库连接

//以上为MYSQL的JDBC分页,下面是ORALCE的,差别只在于SQL,所以你换一下SQL就OK:

StringBuffer sqlStr = new StringBuffer("select *,rownum rn from (select * from Product) where rn =? and rownum =?");

}

求总页数的方法很简单,查出所有记录数,除一页显示数。就可以得到

以上是JDBC的,如果使用hibernate等,数据库分页将会变得很简单,只需要设置两个参数,就是从哪取和取多少。

2.代码分页

思路是将数据库中的所有记录都取出来,然后再分页。

/**

* @para items:数据库中的所有记录,你可以使用另一个方法得到,如何查询得到我就不用说了吧

* @autor godelegant

*/

public List findAll(List items,int pageItems,int currentPage){

int startIndex = (currentPage-1)*pageItems;//从哪里开始取

int offset = items.size()-startIndex;//还有多少没有取过

int pageCount = offsetpageItems?pageItems:offset;//如果没有取过的大于需要显示的数,则实现显示数pageCount=pageItems,反之然;

ListProduct pageList = new ArrayList();

for(int i=startIndex,istartIndex+pageCount,i++){

pageList.add(items.get(i));

}

return pageList;

}

写了40分钟,希望对你有用

java中如何实现百度中的分页

/**

 * 分页代码

 * 

 * @author Star

 * @version 1.0 2008/07/08

 */

public class CutPage implements Serializable{

private static Log log = LogFactory.getLog(CutPage.class);

private int curPageNo = 0; // 当前页数,从0开始

private int size = 0; // 所有数据条数

private String url; // 页面跳转的路径

private List showList; // 当前页面需要显示的数据列表

private int pageSize = 20;// 每页显示的数据条数

private int groupSize = 1;// 多少页为一组

private String pageNavigation;// 导航条

/**

 * 每次通过sql语句从数据库里面分组取出需要显示的数据

 * 

 * @param request

 *            javax.servlet.http.HttpServletRequest对象

 * @param sql

 *            String 查询数据库的sql语句

 * @param pageSize

 *            int 每页显示的条数

 * @param groupSize

 *            int 分成多少组

 * @param url

 *            String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",

 *            如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径

 */

public void init(HttpServletRequest request, String sql, int pageSize,

int groupSize, int pageNo, String url) {

// 上一页、下一页跳转路径

if (url != null) {

this.url = url;

} else {

this.url = request.getRequestURL() + "";

}

if (pageSize  0)

this.pageSize = pageSize;// 每页多少条记录

if (groupSize  0)

this.groupSize = groupSize;

// 当前第几页

if (pageNo  0) {

this.curPageNo = 0;

} else {

this.curPageNo = pageNo;

}

int curGroup = this.curPageNo / this.groupSize + 1;

// 是否是新的一组数据,如果是则到数据库取数据

this.size = parseInt(request.getSession().getAttribute("page_all_size")

+ "", 0);

if (this.curPageNo % this.groupSize == 0

|| (request.getSession().getAttribute("cur_group") != null  parseInt(

"" + request.getSession().getAttribute("cur_group"), 1) != curGroup)

|| this.size == 0 || request.getParameter("reload") != null) {

request.getSession().setAttribute("cur_group", curGroup);

if (pageNo  0

 request.getSession().getAttribute("page_sql") != null) {

sql = request.getSession().getAttribute("page_sql") + "";

} else {

request.getSession().setAttribute("page_sql", sql);

}

this.size = getTotalCount(sql);

List list = getPageData(sql, (this.curPageNo / this.groupSize)

* this.pageSize * this.groupSize, this.pageSize

* this.groupSize);

request.getSession().setAttribute("page_all_size", this.size);

request.getSession().setAttribute("page_cur_list", list);

this.setShowList(list);// 设置页面上的显示数据

} else {

this.setShowList((List) request.getSession().getAttribute(

"page_cur_list"));// 设置页面上的显示数据

}

}

/**

 * 每次通过sql语句从数据库里面分组取出需要显示的数据

 * 

 * @param request

 *            javax.servlet.http.HttpServletRequest对象

 * @param sql

 *            String 查询数据库的sql语句

 * @param pageSize

 *            int 每页显示的条数

 * @param groupSize

 *            int 分成多少组

 * @param url

 *            String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",

 *            如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径

 */

public void init(HttpServletRequest request, String sql, int pageSize,

int groupSize, String url) {

// 当前第几页

String curPage = request.getParameter("pageNo");

init(request, sql, pageSize, groupSize, parseInt(curPage, -1), url);

}

/**

 * 每次通过sql语句从数据库里面分组取出需要显示的数据

 * 

 * @param request

 *            javax.servlet.http.HttpServletRequest对象

 * @param sql

 *            String 查询数据库的sql语句

 * @param pageSize

 *            int 每页显示的条数

 * @param groupSize

 *            int 分成多少组

 * @param url

 *            String 页面跳转的路径,若没有特殊的参数传递,可以传入null或"",

 *            如是在aciton里面调用,并且action是继承自DispatherAction的话最好传入完整的路径

 */

public void init(HttpServletRequest request, String sql, int pageSize,

int groupSize, int pageNo) {

init(request, sql, pageSize, groupSize, pageNo, "");

}

太多了,贴不下,见附件

关于java:分页代码和java分页显示的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。