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年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 常州市| 平和县| 固镇县| 绥宁县| 凤翔县| 阜新| 县级市| 阜宁县| 天水市| 崇仁县| 从江县| 比如县| 迭部县| 怀安县| 盘山县| 兴隆县| 黄龙县| 林周县| 湖南省| 新田县| 安远县| 林西县| 绥江县| 元阳县| 新昌县| 两当县| 安陆市| 鱼台县| 津南区| 阳江市| 靖江市| 蕉岭县| 额济纳旗| 新龙县| 北票市| 惠安县| 岳阳县| 湖南省| 肇庆市| 百色市| 化州市|