int pageSize=你的每页显示纪录数;
int rowCount=0; //总的记录数
while (rscount
int pageCount; //总的页数
int currPage; //当前页数
String strPage;
strPage=request.getParameter("page");
if (strPage==null){
currPage=1;
}
else{
currPage=Integer.parseInt(strPage);
if (currPage<1) currPage=1;
}
pageCount=(rowCount+pageSize-1)/pageSize;
if (currPage>pageCount) currPage=pageCount;
int thepage=(currPage-1)*pageSize;
int n=0;
rs.absolute(thepage+1);
while (n<(pageSize)&&!rs
%>
<%rs.close();
rscount.close();
stmt.close();
stmtcount.close();
conn.close();
%>
//下面是 第几页等
<form name="sinfo" method="post" action="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>" onSubmit="return testform(this)">
第<%=currPage%>页 共<%=pageCount%>页 共<%=rowCount%>条
<%if(currPage>1){%><a href="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>">首页</a><%}%>
<%if(currPage>1){%><a href="sbinfo_index.jsp?page=<%=currPage-1%>&condition=<%=condition%>&type=<%=type%>">上一页</a><%}%>
<%if(currPage<pageCount){%><a href="sbinfo_index.jsp?page=<%=currPage+1%>&condition=<%=condition%>&type=<%=type%>">下一页</a><%}%>
<%if(pageCount>1){%><a href="sbinfo_index.jsp?page=<%=pageCount%>&condition=<%=condition%>&type=<%=type%>">尾页</a><%}%>
跳到<input type="text" name="page" size="4" style="font-size:9px">页
<input type="submit" name="submit" size="4" value="GO" style="font-size:9px">
</form>
谢谢,分页的问题我已经解决了。
现把代码拿出来给大家分享:
<%@page language="java" contentType="text/html;charset=GB2312"%>
<%@page import="java.sql.*" %>
<html>
<head>
<title>分页显示</title>
</head>
<body>
<center>
<font size='5' color='bule'>分页显示</font>
</center>
<br>
<hr>
<br>
<center>
<%!
int getPages(String tabName, int page_size) throws Exception
{
int total = 0;
int rows = this.getRows(tabName); //取得记录笔数
if(rows % page_size == 0) //计算显示的页数
{
total = rows / page_size;
}
else
{
total = rows / page_size + 1;
}
return total;
}
int getRows(String tabName) throws Exception
{
String s1 = "select count(*) from " + tabName;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:test1"); //数据源名为test1,连接的是pubs数据库
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(s1);
int rows = 0;
if(rs.next())
{
rows = rs.getInt(1);
}
return rows;
}
%>
<%
int page_size = 5; //设置每张页面显示的记录数
int cur_page = 1; //设置当前页数
String tabName = "jobs"; //显示的表名
int total_page = this.getPages(tabName,page_size); //设置总页数
Object obPage = request.getParameter("page");
if(obPage != null)
{
cur_page = Integer.parseInt(obPage.toString());
}
if(cur_page > total_page)
{
cur_page = total_page;
}
if(cur_page <= 0)
{
cur_page = 1;
}
///////////////////////////////////////////////////////////////////////////////
String s1 = "select * from " + tabName;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test1"); //数据源名为test1,连接的是pubs数据库
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(s1);
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
StringBuffer sb = new StringBuffer();
sb.append("<table border='10' align='center' width='90%'>");
sb.append("<tr>");
for(int i=0; i<cols; i++)
{
sb.append("<td>" + rsmd.getColumnName(i+1) + "</td>");
}
sb.append("</tr>");
////////////////////////////////////////////////////////////////////////
int cur_row = ((cur_page-1) * page_size) + 1;
rs.absolute(cur_row); //计算欲显示页的第一笔记录位置
for(int j=0; j<page_size; j++)
{
sb.append("<tr>");
for(int i=0; i<cols; i++)
{
sb.append("<td>" + rs.getString(i+1) + "</td>");
}
sb.append("</tr>");
boolean b = rs.next();
if(!b)
{
break;
}
}
out.println(sb.toString());
sb.append("</table>");
%>
<table width='50%'>
<tr algin='center'>
<%
if(cur_page!=1)
{
out.println("<td><a href='displayTable.jsp?page=1'>第一页</a></td>");
out.println("<td><a href='displayTable.jsp?page=" + (cur_page-1) + "'>上一页</a></td>");
}
if(cur_page!=total_page)
{
out.println("<td><a href='displayTable.jsp?page=" + (cur_page+1) + "'>下一页</a></td>");
out.println("<td><a href='displayTable.jsp?page=" + (total_page) + "'>最后一页</a></td>");
}
%>
<td>
<form action='displayTable.jsp' method='post'>到
<input type='text' style='height:25px;width:40px' name='page' value=<%=cur_page%>>页
</form>
</td>
</tr>
</table>
</body>
</html>
[此贴子已经被作者于2006-7-8 21:20:34编辑过]