谢谢,分页的问题我已经解决了。
现把代码拿出来给大家分享:
<%@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编辑过]