主要思路就是在点击换页的链接时给页面传是第几页的参数,比如你共有total条记录,每页pageSize条记录,那么第n页就应该显示第 (n-1)*pageSize 到第 n*pageSize 条记录, 共有 total/pageSize+1 页(因为是int型)
下面是我来的代码,供参考
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
String strPage;
//设置一页显示的记录数
intPageSize = 25;
//取得待显示页码
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
}else{//将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage< 1){
intPage = 1;
}
}
Rs=connJB.executeQuery(sql);
Rs.absolute(1);
//获取记录总数
Rs.last();
intRowCount = Rs.getRow();
//记算总页数
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//调整待显示的页码
if(intPage >intPageCount) intPage = intPageCount;
if(intPageCount >0){
//将记录指针定位到待显示页的第一条记录上
Rs.absolute((intPage-1) * intPageSize+1);
//显示数据
i = 0;
while(i< intPageSize && !Rs.isAfterLast()){
if(!className.equals("行业分类")){
//id=Rs.getInt("INFORMATION_ID");
strId=Rs.getString("INFORMATION_ID");
subjectName=Rs.getString("INFORMATION_NAME");
informationRank=Rs.getString("INFORMATION_RANK");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index2.jsp?INFORMATION_ID=<%=strId%>&INFORMATION_RANK=<%=informationRank%>" class="t8"><%=subjectName%></a></td>
</tr>
<% }else{
strId=Rs.getString("SUB_CLASS_VALUE");
subjectName=Rs.getString("SUB_CLASS_VALUE");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index_way.jsp?SMALL_CLASS=<%=strId%>" class="t8"><%=subjectName%></a></td>
</tr>
<%}%>
<%
Rs.next();
i++;
}Rs.close();
i=0;
%>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td valign="top" bgcolor="#F2F2F2">
<div align="left">
</div></td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C8C8C">
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">第<%=intPage%>页 共<%//=intRowCount%>条记录/共<%=intPageCount%>页
<%
if(intPage<2){
out.print("首页 | 上一页");
}else{%>
<a href="index.jsp?CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">首页 </a> | <a href="index.jsp?page=<%=intPage-1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">上一页</a>
<%}%>
|
<%
if((intPageCount-intPage)<1)
{
out.print("下一页 | 尾页");
}else{%>
<a href="index.jsp?page=<%=intPage+1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">下一页</a> | <a href="index.jsp?page=<%=intPageCount%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">尾页</a>
<%}
}
else{
out.print("<center><table width=776 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF><center>目前还没有内容!</center></td></tr></table></centre>");
}%>
int intPageSize; //一页显示的记录数
int intRowCount; //记录总数
int intPageCount; //总页数
int intPage; //待显示页码
String strPage;
//设置一页显示的记录数
intPageSize = 25;
//取得待显示页码
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
}else{//将字符串转换成整型
intPage = java.lang.Integer.parseInt(strPage);
if(intPage< 1){
intPage = 1;
}
}
Rs=connJB.executeQuery(sql);
Rs.absolute(1);
//获取记录总数
Rs.last();
intRowCount = Rs.getRow();
//记算总页数
intPageCount = (intRowCount+intPageSize-1) / intPageSize;
//调整待显示的页码
if(intPage >intPageCount) intPage = intPageCount;
if(intPageCount >0){
//将记录指针定位到待显示页的第一条记录上
Rs.absolute((intPage-1) * intPageSize+1);
//显示数据
i = 0;
while(i< intPageSize && !Rs.isAfterLast()){
if(!className.equals("行业分类")){
//id=Rs.getInt("INFORMATION_ID");
strId=Rs.getString("INFORMATION_ID");
subjectName=Rs.getString("INFORMATION_NAME");
informationRank=Rs.getString("INFORMATION_RANK");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index2.jsp?INFORMATION_ID=<%=strId%>&INFORMATION_RANK=<%=informationRank%>" class="t8"><%=subjectName%></a></td>
</tr>
<% }else{
strId=Rs.getString("SUB_CLASS_VALUE");
subjectName=Rs.getString("SUB_CLASS_VALUE");
%>
<tr>
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td>
<td width="96%"><a href="index_way.jsp?SMALL_CLASS=<%=strId%>" class="t8"><%=subjectName%></a></td>
</tr>
<%}%>
<%
Rs.next();
i++;
}Rs.close();
i=0;
%>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td valign="top" bgcolor="#F2F2F2">
<div align="left">
</div></td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C8C8C">
<tr>
<td height="22" align="center" bgcolor="#FFFFFF">第<%=intPage%>页 共<%//=intRowCount%>条记录/共<%=intPageCount%>页
<%
if(intPage<2){
out.print("首页 | 上一页");
}else{%>
<a href="index.jsp?CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">首页 </a> | <a href="index.jsp?page=<%=intPage-1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">上一页</a>
<%}%>
|
<%
if((intPageCount-intPage)<1)
{
out.print("下一页 | 尾页");
}else{%>
<a href="index.jsp?page=<%=intPage+1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">下一页</a> | <a href="index.jsp?page=<%=intPageCount%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">尾页</a>
<%}
}
else{
out.print("<center><table width=776 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF><center>目前还没有内容!</center></td></tr></table></centre>");
}%>
『贰』 jsp查询到的所有结果用一张表格显示在一个页面上,我想在表格下方实现上一页,下一页翻页的功能按钮
jsp页面可以用分页来控制查询到的多条数据。
具体实例如下:
public class PageResult {
private List list; //装分页列表中的内容
private Integer firstPage; //第一页
private Integer prePage; //上一页
private Integer nextPage;//下一页
private Integer currentPage;//当前页面
private Integer totalPage; //总页数/尾页
private Integer count;//总条数
private Integer size; //每页多少条
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Integer getFirstPage() {
return firstPage;
}
public void setFirstPage(Integer firstPage) {
this.firstPage = firstPage;
}
public Integer getPrePage() {
return (this.currentPage-1==0?1:this.currentPage-1);
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return (this.currentPage==this.totalPage?this.totalPage:this.currentPage+1);
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalPage() {
return (this.count%this.size==0?this.count/this.size:this.count/this.size+1);
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
public class listServlet extends HttpServlet {
private IProctDAO ;
@Override
public void init() throws ServletException {
= new IProctDAOImpl();
super.init();
}
<title>货品列表</title>
</head>
<body>
<form action="/list" method="post">
<table border="1">
<tr>
<td>货品名称</td>
<td><input type="text" name="proctName"></td>
</tr>
<tr>
<td>品牌</td>
<td><input type="text" name="brand"></td>
</tr>
<tr>
<td>零售价</td>
<td><input type="text" name="salePrice"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="查询"></td>
</tr>
</table>
</form>
<table border="1">
<tr>
<th>货品</th>
<th>零售价</th>
<th>品牌</th>
<th>折扣</th>
<th>供应商</th>
<th>操作</th>
</tr>
<c:forEach items="${pageResult.list}" var="proct">
<tr>
<td>${proct.proctName}</td>
<td>${proct.salePrice}</td>
<td>${proct.brand}</td>
<td>${proct.cutoff}</td>
<td>${proct.supplier}</td>
<td><a href='/employee?cmd=remove&id=${employee.id}'>删除</a> <a href='/employee?cmd=edit&id=${employee.id}'>修改</a></td>
</tr>
</c:forEach>
<tr>
<th><a href="/list?currentPage=${pageResult.firstPage}">首页</a>${pageResult.firstPage}</th>
<th><a href="/list?currentPage=${pageResult.prePage}">上一页</a>${pageResult.prePage}</th>
<th><a href="/list?currentPage=${pageResult.nextPage}">下一页</a>${pageResult.nextPage}</th>
<th><a href="/list?currentPage=${pageResult.totalPage}">尾页</a>${pageResult.totalPage}</th>
<th>当前${pageResult.currentPage}/${pageResult.totalPage}页</th>
<th>总条数:${pageResult.count}</th>
</tr>
</table>
</body>
</html>
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPage = request.getParameter("currentPage");
PageResult pageResult = new PageResult();
if(StringUtils.isBlank(currentPage))
{
currentPage="1";
}
pageResult.setCurrentPage(Integer.parseInt(currentPage));
pageResult.setSize(5);
List<Proct> list = .list(pageResult.getCurrentPage(), pageResult.getSize());
pageResult.setList(list);
int count = .count();
pageResult.setCount(count);
//如果当前页等于第一页,则上一页就是第一页,否则为当前页上一页
pageResult.setPrePage(pageResult.getPrePage());
//如果当前页是尾页,则下一页就是尾页,否则为当前页的下一页
pageResult.setNextPage(pageResult.getNextPage());
pageResult.setCount(count);
pageResult.setTotalPage(pageResult.getTotalPage());
request.setAttribute("pageResult", pageResult);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}
『叁』 JSP实现翻页功能
第一个文件pagescount.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>pagescount</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
//设置每张网页显示三笔记录(每页显示的记录数)
int PageSize=3;
//设置欲显示的页数(初始页)
int ShowPage=1;
//ResultSet的记录笔数(总记录数)
int RowCount=0;
//ResultSet分页后的总数(总页数)
int PageCount=0;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
%>
<%!
//连接数据库并初始数据
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";
Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");
//将指标移至最后一条记录
rs.last();
//获取记录总数
RowCount=rs.getRow();
//计算显示的页数(关键)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//执行关闭各个对象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>
第二个文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页显示</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");
//判断是否取得ToPage参数
if(ToPage!=null)
{
//取得指定显示的分页页数
ShowPage=Integer.parseInt(ToPage);
//下面的语句判断用户输入的页数是否正确
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}
//计算欲显示页的第一笔记录位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>当前在第
<font size="4" color="red"><%=ShowPage %></font>页,共<%=PageCount %>页</h3>
<p>
<%
//利用for循环配合PageSize属性取出一页中的数据
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>书名ID</td>
<td><%=rs.getString("pub_id") %></td>
<td>书名</td>
<td><%=rs.getString("title") %></td>
<td>类型</td>
<td><%=rs.getString("type") %></td>
<td>价格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>
<%
//下面的语句用于输出最后一条记录时,将指针移到最后一笔记录之后
if(!rs.next())
{
//跳出for循环
break;
}
}
%>
<table>
<tr>
<%
//判断当前是否在第一页,不是第一页,则显示到第一页与下一页的连接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一页</a>
</td>
<%
}
//判断是否在最后一页,是,则显示到最后一页和下一页
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最后一页</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">页
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>
『肆』 JSP 翻页功能怎么做
第一个文件pagescount.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>pagescount</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
//设置每张网页显示三笔记录(每页显示的记录数)
int PageSize=3;
//设置欲显示的页数(初始页)
int ShowPage=1;
//ResultSet的记录笔数(总记录数)
int RowCount=0;
//ResultSet分页后的总数(总页数)
int PageCount=0;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
%>
<%!
//连接数据库并初始数据
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";
Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");
//将指标移至最后一条记录
rs.last();
//获取记录总数
RowCount=rs.getRow();
//计算显示的页数(关键)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//执行关闭各个对象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>
第二个文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页显示</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");
//判断是否取得ToPage参数
if(ToPage!=null)
{
//取得指定显示的分页页数
ShowPage=Integer.parseInt(ToPage);
//下面的语句判断用户输入的页数是否正确
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}
//计算欲显示页的第一笔记录位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>当前在第
<font size="4" color="red"><%=ShowPage %></font>页,共<%=PageCount %>页</h3>
<p>
<%
//利用for循环配合PageSize属性取出一页中的数据
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>书名ID</td>
<td><%=rs.getString("pub_id") %></td>
<td>书名</td>
<td><%=rs.getString("title") %></td>
<td>类型</td>
<td><%=rs.getString("type") %></td>
<td>价格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>
<%
//下面的语句用于输出最后一条记录时,将指针移到最后一笔记录之后
if(!rs.next())
{
//跳出for循环
break;
}
}
%>
<table>
<tr>
<%
//判断当前是否在第一页,不是第一页,则显示到第一页与下一页的连接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一页</a>
</td>
<%
}
//判断是否在最后一页,是,则显示到最后一页和下一页
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最后一页</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">页
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>
『伍』 JSP翻页查询
;%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>pagescount</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
//设置每张网页显示三笔记录(每页显示的记录数)
int PageSize=3;
//设置欲显示的页数(初始页)
int ShowPage=1;
//ResultSet的记录笔数(总记录数)
int RowCount=0;
//ResultSet分页后的总数(总页数)
int PageCount=0;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
%>
<%!
//连接数据库并初始数据
public void jspInit()
{
try{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "sa";
Class.forName(Driver);
conn=DriverManager.getConnection(DBurl,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from titles");
//将指标移至最后一条记录
rs.last();
//获取记录总数
RowCount=rs.getRow();
//计算显示的页数(关键)
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):(RowCount/PageSize)+1);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//执行关闭各个对象的操作
public void jspDestory()
{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
%>
</body>
</html>
第二个文件
pages.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%@include file="pagescount.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>分页显示</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="author" content="sunxch">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="cce8cf">
<center>
<%
String ToPage=request.getParameter("ToPage");
//判断是否取得ToPage参数
if(ToPage!=null)
{
//取得指定显示的分页页数
ShowPage=Integer.parseInt(ToPage);
//下面的语句判断用户输入的页数是否正确
if(ShowPage>=PageCount)
{
ShowPage=PageCount;
}
else if(ShowPage<=0)
{
ShowPage=1;
}
}
//计算欲显示页的第一笔记录位置
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>当前在第
<font size="4" color="red"><%=ShowPage %></font>页,共<%=PageCount %>页</h3>
<p>
<%
//利用for循环配合PageSize属性取出一页中的数据
for(int i=1;i<=PageSize;i++)
{
%>
<table border="1" width="90%">
<tr>
<td>书名ID</td>
<td><%=rs.getString("pub_id") %></td>
<td>书名</td>
<td><%=rs.getString("title") %></td>
<td>类型</td>
<td><%=rs.getString("type") %></td>
<td>价格</td>
<td><%=rs.getFloat("price") %></td>
</tr>
<tr>
<td>备注</td>
<td colspan="7"><%=rs.getString("notes") %></td>
</tr>
</table>
<p>
<%
//下面的语句用于输出最后一条记录时,将指针移到最后一笔记录之后
if(!rs.next())
{
//跳出for循环
break;
}
}
%>
<table>
<tr>
<%
//判断当前是否在第一页,不是第一页,则显示到第一页与下一页的连接
if(ShowPage!=1)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=1 %>">第一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage-1 %>">上一页</a>
</td>
<%
}
//判断是否在最后一页,是,则显示到最后一页和下一页
if(ShowPage!=PageCount)
{
%>
<td width=150>
<a href="pages.jsp?ToPage=<%=ShowPage+1 %>">下一页</a>
</td>
<td width=150>
<a href="pages.jsp?ToPage=<%=PageCount %>">最后一页</a>
</td>
<%
}
%>
</tr>
<tr>
<td colspan="4" align="center">
<form action="pages.jsp" method="post" name="form1">
<input type="text" name="ToPage" value="<%=ShowPage %>" style="height:25px;width:40px">页
<a href="javascript:window.document.form1.submit();">GO</a>
</form>
</td>
</tr>
</table>
</center>
<script language="javascript">
function go()
{
window.document.form1.submit();
}
</script>
</body>
</html>