posts - 3,  comments - 6,  trackbacks - 0
              今天用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 一葉孤舟 閱讀(1740) 評論(2)  編輯  收藏


          FeedBack:
          # re: Invalid operation for the current cursor position
          2008-09-24 10:21 | wanglihong
          改成if(rs.next==false)還是錯  回復  更多評論
            
          # re: Invalid operation for the current cursor position
          2008-11-14 10:43 | 小美狐
          改成if(rs.next),就對啦。。。  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 民勤县| 丰城市| 合川市| 大宁县| 炉霍县| 瓦房店市| 龙川县| 柘城县| 类乌齐县| 宁国市| 甘德县| 顺昌县| 台前县| 洛扎县| 通渭县| 赤水市| 乃东县| 湘乡市| 衡南县| 来安县| 锦州市| 焉耆| 兴安盟| 安平县| 岳普湖县| 保亭| 新龙县| 辽阳县| 北流市| 肥乡县| 昌宁县| 巢湖市| 海口市| 明星| 龙岩市| 阳朔县| 城市| 黎城县| 武夷山市| 个旧市| 贡山|