我觉得可以没有那么复杂,可从连接数据库中创建statment时给出相应参数,在连接数据库时同时对数据库中的记录集进行操作(类似于指针,从所需要的记录集中进行分页及相应算法操作),其实和上面的数据操作是同一个道理,只不过上面是通过访问数据集中每一条记录,然后一条记录一条记录的向下循环.以下是我的程序:
<%@page contentType="text/html;charset=gb2312"language="java" import="java.sql.*" %>
<%
String MM_conn_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
String MM_conn_USERNAME="";
String MM_conn_PASSWORD="";
String MM_conn_STRING="jdbc:odbc:classes";
Driver DriverRecordset1= (Driver)Class.forName(MM_conn_DRIVER).newInstance();
Connection ConnRecordset1=DriverManager.getConnection(MM_conn_STRING,MM_conn_USERNAME,MM_conn_PASSWORD);
Statement stmt=ConnRecordset1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from student");
String pageStr=(String)request.getParameter("pageNo");
int pageSize=10;
if(pageStr==null || pageStr.equals("")) pageStr="1";
int pageNo=java.lang.Integer.parseInt(pageStr);
if (pageNo==0) pageNo=1;
rs.last();
int intRowCount = rs.getRow();
int intPageCount = (intRowCount+pageSize-1)/pageSize;
if(pageNo>intPageCount) pageNo=intPageCount;
rs.absolute((pageNo-1)*pageSize+1);
%>
<html><head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>dfasdf</title>
<style type="text/css">
<!--
.style3{font-size:large}
-->
</style>
<script language='javascript'>
function toUrl(num){window.location.href="jspJDBC.jsp?pageNo="+num;}
</script>
</head><body>
<table width="70%" border="0" align="center" bgcolor="#00CC99">
<tr>
<th colspan="3" bgcolor="#00CCCC" scope="col"><span class="style3">用户管理</span>--密码显示
</th>
</tr><tr bgcolor="$FFFFFF">
<th scope="col"><kbd>ID</kbd></th>
<th scope="col"><kbd>用户名</kbd></th>
<th scope="col"><kbd>密码</kbd></th>
</tr><%
int i= 0;
while(i<pageSize && !rs.isAfterLast())
{%>
<tr bgcolor="#FFFFFF">
<th width+"28%" scope="col"><%=rs.getObject("STUDENTID") %></th>
<th width+"27%" scope="col"><%=rs.getObject("STUDENTNAME") %></th>
<th width+"45%" scope="col"><%=rs.getObject("STUDENTAGE") %></th>
</tr><%
rs.next();
i++;
}
%><tr><th colspan="3" scope="row"><% if (pageNo==1){out.print("首页 上一页");
} else{%>
<a href="javascript:toUrl('1');">首页</a> <a href="javascript:toUrl('<%=pageNo-1%>');">上一页</a>
<% } %>
<% if (pageNo+1>intPageCount){out.print("下一页 尾页");}
else{%>
<a href="javascript:toUrl('<%=pageNo+1%>');">下一页</a> <a href="javascript:toUrl('<%=intPageCount%>');">尾页</a>
<% } %>
</th></tr>
</table>
</body>
</html>
<%
rs.close(); stmt.close() ; ConnRecordset1.close();
%>你可以连接ACCESS等数据库进行测试,而且我给出了整个页面的程序,你只需有这个数据库及相应的字段就可以测试.
<%@page contentType="text/html;charset=gb2312"language="java" import="java.sql.*" %>
<%
String MM_conn_DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
String MM_conn_USERNAME="";
String MM_conn_PASSWORD="";
String MM_conn_STRING="jdbc:odbc:classes";
Driver DriverRecordset1= (Driver)Class.forName(MM_conn_DRIVER).newInstance();
Connection ConnRecordset1=DriverManager.getConnection(MM_conn_STRING,MM_conn_USERNAME,MM_conn_PASSWORD);
Statement stmt=ConnRecordset1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from student");
String pageStr=(String)request.getParameter("pageNo");
int pageSize=10;
if(pageStr==null || pageStr.equals("")) pageStr="1";
int pageNo=java.lang.Integer.parseInt(pageStr);
if (pageNo==0) pageNo=1;
rs.last();
int intRowCount = rs.getRow();
int intPageCount = (intRowCount+pageSize-1)/pageSize;
if(pageNo>intPageCount) pageNo=intPageCount;
rs.absolute((pageNo-1)*pageSize+1);
%>
<html><head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>dfasdf</title>
<style type="text/css">
<!--
.style3{font-size:large}
-->
</style>
<script language='javascript'>
function toUrl(num){window.location.href="jspJDBC.jsp?pageNo="+num;}
</script>
</head><body>
<table width="70%" border="0" align="center" bgcolor="#00CC99">
<tr>
<th colspan="3" bgcolor="#00CCCC" scope="col"><span class="style3">用户管理</span>--密码显示
</th>
</tr><tr bgcolor="$FFFFFF">
<th scope="col"><kbd>ID</kbd></th>
<th scope="col"><kbd>用户名</kbd></th>
<th scope="col"><kbd>密码</kbd></th>
</tr><%
int i= 0;
while(i<pageSize && !rs.isAfterLast())
{%>
<tr bgcolor="#FFFFFF">
<th width+"28%" scope="col"><%=rs.getObject("STUDENTID") %></th>
<th width+"27%" scope="col"><%=rs.getObject("STUDENTNAME") %></th>
<th width+"45%" scope="col"><%=rs.getObject("STUDENTAGE") %></th>
</tr><%
rs.next();
i++;
}
%><tr><th colspan="3" scope="row"><% if (pageNo==1){out.print("首页 上一页");
} else{%>
<a href="javascript:toUrl('1');">首页</a> <a href="javascript:toUrl('<%=pageNo-1%>');">上一页</a>
<% } %>
<% if (pageNo+1>intPageCount){out.print("下一页 尾页");}
else{%>
<a href="javascript:toUrl('<%=pageNo+1%>');">下一页</a> <a href="javascript:toUrl('<%=intPageCount%>');">尾页</a>
<% } %>
</th></tr>
</table>
</body>
</html>
<%
rs.close(); stmt.close() ; ConnRecordset1.close();
%>你可以连接ACCESS等数据库进行测试,而且我给出了整个页面的程序,你只需有这个数据库及相应的字段就可以测试.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货