如何用java实现分页效果(eclipse工具)
展开全部
package dl.wsxx.base;
public class Pager {
private int totalRows; // 总行数
private int pageSize; // 每页显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据库中的起始行
private int pageStartRow; // 当前页开始行
private int pageEndRow; // 当前页结束行
private int hasNextPage; // 下一页存在标识[0:不存在,1:存在]
private int hasPreviousPage; // 前一页存在标识[0:不存在,1:存在]
public Pager() {
}
public Pager(int _totalRows,int _pageSize) {
pageSize = _pageSize;
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod > 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getpageStartRow() {
return pageStartRow;
}
public int getpageEndRow() {
return pageEndRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public int getHasNextPage() {
return hasNextPage;
}
public int getHasPreviousPage() {
return hasPreviousPage;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setHasNextPage(int hasNextPage) {
this.hasNextPage = hasNextPage;
}
public void setHasPreviousPage(int hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
this.hasFlagSet(currentPage, totalPages);
}
private void hasFlagSet(int currentPage, int totalPages) {
if (currentPage == totalPages) {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 0;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 0;
}
} else {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 1;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 1;
}
}
}
}
这是我的工程里的分页核心代码,希望对你有用,还有ssh分页文档,可以参照研究一下。
2016-02-22 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
用java实现翻页代码跟eclipse没有关系。
参考代码如下:
<head>
<%
const MaxPerPage=20
dim sql
dim rs
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
%>
</head>
<body>
<%
conn = "DBQ=" + server.mappath("wj028.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
sql = "SELECT * FROM USER order by id desc"
set rs=server.createobject("adodb.recordset")
rs.open SQL,conn,1,1
rs.MoveFirst
rs.pagesize=MaxPerPage
howmanyfields=rs.Fields.Count-1
If trim(Request("Page"))<>"" then
CurrentPage= CLng(request("Page"))
If CurrentPage> rs.PageCount then
CurrentPage = rs.PageCount
End If
Else
CurrentPage= 1
End If
if rs.eof then
response.write "<p align='center'>没有记录!</p>"
else
totalPut=rs.recordcount
if CurrentPage<>1 then
if (currentPage-1)*MaxPerPage<totalPut then
rs.move(currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
end if
end if
dim n,k
if (totalPut mod MaxPerPage)=0 then
n= totalPut \ MaxPerPage
else
n= totalPut \ MaxPerPage + 1
end if%>
<% i=0
do while not rs.EOF and i<maxperpage
%>
<%
rs.movenext
i=i+1
loop
%>
第<%=currentpage%>页
共<%=n%>页
<%
k=currentpage
if k<>1 then
response.write "[<b>"+"<a href='list.asp?page=1'>首页</a></b>]"
response.write "[<b>"+"<a href='list.asp?page="+cstr(k-1)+"'>上一页</a></b>]"
else
Response.Write "[首页][上一页]"
end if
if k<>n then
response.write "[<b>"+"<a href='list.asp?page="+cstr(k+1)+"'>下一页</a></b>] "
response.write "[<b>"+"<a href='list.asp?page="+cstr(n)+"'>尾页</a></b>] "
else
Response.Write "[下一页][尾页]"
end if
%>
<%
end if
set rs=nothing
set conn=nothing
%>
</body>
参考代码如下:
<head>
<%
const MaxPerPage=20
dim sql
dim rs
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
%>
</head>
<body>
<%
conn = "DBQ=" + server.mappath("wj028.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
sql = "SELECT * FROM USER order by id desc"
set rs=server.createobject("adodb.recordset")
rs.open SQL,conn,1,1
rs.MoveFirst
rs.pagesize=MaxPerPage
howmanyfields=rs.Fields.Count-1
If trim(Request("Page"))<>"" then
CurrentPage= CLng(request("Page"))
If CurrentPage> rs.PageCount then
CurrentPage = rs.PageCount
End If
Else
CurrentPage= 1
End If
if rs.eof then
response.write "<p align='center'>没有记录!</p>"
else
totalPut=rs.recordcount
if CurrentPage<>1 then
if (currentPage-1)*MaxPerPage<totalPut then
rs.move(currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
end if
end if
dim n,k
if (totalPut mod MaxPerPage)=0 then
n= totalPut \ MaxPerPage
else
n= totalPut \ MaxPerPage + 1
end if%>
<% i=0
do while not rs.EOF and i<maxperpage
%>
<%
rs.movenext
i=i+1
loop
%>
第<%=currentpage%>页
共<%=n%>页
<%
k=currentpage
if k<>1 then
response.write "[<b>"+"<a href='list.asp?page=1'>首页</a></b>]"
response.write "[<b>"+"<a href='list.asp?page="+cstr(k-1)+"'>上一页</a></b>]"
else
Response.Write "[首页][上一页]"
end if
if k<>n then
response.write "[<b>"+"<a href='list.asp?page="+cstr(k+1)+"'>下一页</a></b>] "
response.write "[<b>"+"<a href='list.asp?page="+cstr(n)+"'>尾页</a></b>] "
else
Response.Write "[下一页][尾页]"
end if
%>
<%
end if
set rs=nothing
set conn=nothing
%>
</body>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
分页无非就那几个参数,每页显示数、当前页、总数据量,然后根据公式去算把,很简单。稍微复杂一点的是根据条件查询,查询的结果分页,不过你思路对了也没问题
追问
你有没有代码,我这些都不太懂啊,最好有注释
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个跟eclipse没关系,参考代码:
package dl.wsxx.base;
public class Pager {
private int totalRows; // 总行数
private int pageSize; // 每页显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据库中的起始行
private int pageStartRow; // 当前页开始行
private int pageEndRow; // 当前页结束行
private int hasNextPage; // 下一页存在标识[0:不存在,1:存在]
private int hasPreviousPage; // 前一页存在标识[0:不存在,1:存在]
public Pager() {
}
public Pager(int _totalRows,int _pageSize) {
pageSize = _pageSize;
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod > 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getpageStartRow() {
return pageStartRow;
}
public int getpageEndRow() {
return pageEndRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public int getHasNextPage() {
return hasNextPage;
}
public int getHasPreviousPage() {
return hasPreviousPage;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setHasNextPage(int hasNextPage) {
this.hasNextPage = hasNextPage;
}
public void setHasPreviousPage(int hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
this.hasFlagSet(currentPage, totalPages);
}
private void hasFlagSet(int currentPage, int totalPages) {
if (currentPage == totalPages) {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 0;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 0;
}
} else {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 1;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 1;
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询