今天用jsp写分页的时候碰到这个错误,半天才明白是后面的数据库行数不够导致rs.next()方法访问时出现该异常。见红色改正过的代码
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="DBConnection" scope="page" class="com.DBConnection" />
<jsp:useBean id="userInfo" scope="session" class="com.UserInfo" />
<!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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 读取数据库中的用户信息分页显示在web页面上-->
<%
String sql = "select * from UserInfo order by userid asc";
int currentPage = 1; //当前页号,初始页号为1
int totalPages = 0; //总页数
int pageSize = 10; //每页显示的记录数
ResultSet rs = DBConnection.executeQuery(sql);
rs.last();
int lastRow = rs.getRow();
totalPages = (lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1); //计算分页后的总页数
%>
<%--用户通过表单提交想要显示的页码数--%>
<FORM action="displayUserInfo.jsp" method=get>
输入页码数:<INPUT type="text" name="showPage" value="1" size=4>
<INPUT type="submit" name="go" value="GO">
</FORM>
<P>
共有<%=totalPages%>页记录
<BR>每页显示<%=pageSize%>条记录
<%--get requested page--%>
<%
String requestPage = request.getParameter("showPage");
if(requestPage==null){
requestPage = "1";
}
try{
currentPage = Integer.parseInt(requestPage);
}
catch(NumberFormatException e){
currentPage = 1;
}
if(currentPage<=1){
currentPage =1;
}
if(currentPage>=totalPages){
currentPage = totalPages;
}
%>
<%--end of get requested page--%>
<BR>目前显示第<%=currentPage%>页
<%
//光标移动到要显示页的第一行position
int position = (currentPage-1)*pageSize+1;
rs.absolute(position); //设置游标
out.print("光标定位到这里!"+position);
//构造用户信息表格
out.print("<table border>");
out.print("<tr>");
out.print("<th>姓名</th>");
out.print("<th>性别</th>");
out.print("<th>密码</th>");
out.print("<th>出生日期</th>");
out.print("<th>电子信箱</th>");
out.print("<th>地址</th>");
out.print("</tr>");
for(int i=1;i<=pageSize;i++){
out.print("<tr>");
out.print("<td>"+rs.getString("userName")+"</td>");
out.print("<td>"+rs.getString("gender")+"</td>");
out.print("<td>"+rs.getString("psw")+"</td>");
out.print("<td>"+rs.getString("birthday")+"</td>");
out.print("<td>"+rs.getString("email")+"</td>");
out.print("<td>"+rs.getString("addr")+"</td>");
out.print("</tr>");
if(rs.next()==false) break;//此处代码原来为rs.next();
}
out.print("</table>");
rs.close();
//DBConnection.close();
%>
</body>
</html>
posted on 2008-02-28 10:37
一叶孤舟 阅读(1731)
评论(2) 编辑 收藏