java语言分页代码 java分页怎么实现

java的分页this.sqlStr=sqlStr "limit" irows "," pageSize;
这句是:sqlStr 是用来存放你的SQL语句的变量;整个的意思就是:
比如:sqlStr="select * from user";
this.sqlStr="select * from user limit 9,4
就是查询表user 数据从第九行开始,向后查4行 。每页显示4行数据 。
String[] sData = https://www.04ip.com/post/new String[6];定义一个大小为6的字符串数组 ,
for(int j=0;jrsmd.getColumnCount();j){*******************getColumnCount()什么意思有啥用????
sData[j]=rs.getString(j 1);
}
这句是循环遍历,将数据库的数据循环遍历的赋给字符串数组 。
亲 , 希望我的回答对你有帮助 。
谁能给我个完整的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
【java语言分页代码 java分页怎么实现】* @return String
*/
public String pagetool(String flag) {
StringBuffer str = new StringBuffer();
String url = this.getParamUrl();
int ProPage = this.currentpage - 1;
int Nextpage = this.currentpage1;
// 文字的分页
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.currentpage1) {
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.currentpagethis.pagecount) {
str.append("a href='"url"currentpage="Nextpage
"'下一页/a");
str.append(" ");
} else {
str.append("下一页");
str.append(" ");
}
if (this.pagecount1this.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.currentpage1) {
str.append("a href='"url"currentpage=1'首页/a");
str.append("");
}
// 设定是否有上一页的链接
if (this.currentpage1) {
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.currentpage4;
if (endPagethis.pagecount) {
endPage = this.pagecount;
}
int startPage = 0;
if (this.pagecount = 8this.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.currentpagethis.pagecount) {
str.append("a href='"url"currentpage="Nextpage
"'下一页/a");
str.append("");
}
// 设定是否有尾页的链接
if (this.pagecount1this.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.currentpage1) {
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.currentpage4;
if (endPagethis.pagecount) {
endPage = this.pagecount;
}
int startPage = 0;
if (this.pagecount = 8this.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.currentpagethis.pagecount) {
if (this.currentpagethis.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.pagecount1this.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 = https://www.04ip.com/post/this.request.getParameter(tempName);
if (tempValue != null!tempValue.equals("")
!tempName.equals("currentpage")) {
if (totalParams.equals("")) {
totalParams = totalParamstempName"="tempValue;
} else {
totalParams = totalParams""tempName"="
tempValue;
}
}
}
String totalUrl = urltotalParams;
return totalUrl;
}
}
Java中实现分页效果的详细代码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 CurrentPage1 then
if (currentPage-1)*MaxPerPagetotalPut 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 \ MaxPerPage1
end if%
% i=0
do while not rs.EOF and imaxperpage
%
%
rs.movenext
i=i 1
loop
%
第%=currentpage%页
共%=n%页
%
k=currentpage
if k1 then
response.write "[b" "a href='https://www.04ip.com/post/list.asp?page=1'首页/a/b]"
response.write "[b" "a href='https://www.04ip.com/post/list.asp?page=" cstr(k-1) "'上一页/a/b]"
else
Response.Write "[首页][上一页]"
end if
if kn then
response.write "[b" "a href='https://www.04ip.com/post/list.asp?page=" cstr(k 1) "'下一页/a/b] "
response.write "[b" "a href='https://www.04ip.com/post/list.asp?page=" cstr(n) "'尾页/a/b] "
else
Response.Write "[下一页][尾页]"
end if
%
%
end if
set rs=nothing
set conn=nothing
%
/body
怎样用java实现分页显示实现原理很简单,就是建立一个Page类,里面放当前访问的页数和每一页显示的记录行数 。然后通过分页计算就可以得出下列数据 。
总页数 = 总记录数/每页大小,如果0!=总记录数%每页大?。敲醋芤呈?1 。
当前页数 。
表记录的起始位置=(当前页数-1)*每页大小 。
总记录数(select count(*) from [表名] [where [条件]] 。从数据库中查询得到)
每页大小,可以固定,也可以从页面传过来有了这几个参数之后,就用sql语句查出对应的记录就可以了 。
Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言 。
Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群 。
它最初被命名为Oak,目标设定在家用电器等小型系统的编程语言,来解决诸如电视机、电话、闹钟、烤面包机等家用电器的控制和通讯问题 。
由于这些智能化家电的市场需求没有预期的高,Sun放弃了该项计划 。就在Oak几近失败之时,随着互联网的发展,Sun看到了Oak在计算机网络上的广阔应用前景,于是改造了Oak,以“Java”的名称正式发布 。
Java的主要工作是通过编程语言来制作互联网页面、制作动态效果以及网站等技术 。
求java分页代码,急用!package common.util;
import java.util.*;
public class PageController implements IPageModel {
private Collection model;
//数据总行数
private int totalRowCount = 0; //
//总页数
private int pageCount = 0;
//每页应显示java语言分页代码的行数
private int maxPageRowCount = 0;
//当前页行数
private int currPageRowCount = 0;
//当前页号
private int currPageNum;
//默认构造
public PageController() {
super();
}
//传入模型
public PageController(Collection model) {
setPageController(model);
}
//设一个分页模型
public void setPageController(Collection model) {
this.model = model;
this.totalRowCount = model.size();
}
/**
* 总页数
* @return int
*/
public int getPageCount() {
return this.pageCount;
}
/**
* getPageContents
*
* @param intPageNum int
* @return Object
*/
public Object getPageContents(int intPageNum) {
//非法数据
if(intPageNum1){
intPageNum=1;
}
if(intPageNumpageCount){
intPageNum=pageCount;
}
//指定当前页
this.currPageNum=intPageNum;
int i = 0;
ArrayList arr = new ArrayList();
//如果是合法的范围
if (intPageNum0intPageNum = pageCount) {
//计算该页的开始号和结束号
int lfromrow = (intPageNum - 1) * maxPageRowCount;
arr = (ArrayList) getElementsAt(model, lfromrow, lfromrowmaxPageRowCount-1);
}
currPageNum=intPageNum;
return arr;
}
public Object getLastPage() {
return this.getPageContents(pageCount);
}
public Object getFirstPage() {
return this.getPageContents(0);
}
/**
* getCurrentPageRowsCount
*
* @return int
*/
public int getCurrentPageRowsCount() {
if(currPageNumpageCount){
return maxPageRowCount;
}
else{//最后一页
return totalRowCount-(pageCount-1)*maxPageRowCount;
}
}
public int getCurrentPageNum(){
return currPageNum;
}
/**
* setMaxPageRows
*
* @return int
*/
public void setMaxPageRows(int rowCount) {
maxPageRowCount = rowCount;
//计算总页数
if (totalRowCount % maxPageRowCount0) { //有余数
pageCount = totalRowCount / maxPageRowCount1;
}
else {
pageCount = totalRowCount / maxPageRowCount;
}
}
/**
* getMaxPageRows
*/
public int getMaxPageRows() {
return maxPageRowCount;
}
//私有方法java语言分页代码 , 返回集合中指定范围的数据
private Object getElementsAt(Collection model, int fromIndex, int toIndex) {
Iterator iter = model.iterator();
ArrayList arr = new ArrayList();
if (iter != null) {
int i = 0;
while (iter.hasNext()) {
Object obj=iter.next();
if (i = fromIndexi = toIndex) {
arr.add(obj);
}
if (itoIndex) {
break;
}
i = i1;
}
}
return arr;
}
}
关于java语言分页代码和java分页怎么实现的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读