jsp连接access 分页显示

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
 <%! int pageSize=3; //每页显示的记录数。
     int pageCount=0; //分页后的总页数。
 %>
<%-- 客户通过表单提交欲要显示的页码数--%>
<FORM action="" method=get >
 输入页码数<Input Type=text name=showPage size=4 >
   <Input Type=submit name=g value=提交>
 </FORM>
<%
    try
    {
        String dbpath = application.getRealPath("hufan.mdb");//转化成物理路径
        String dbname = "";
        String user = "";
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath);
        Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        //返回可滚动的结果集:
        ResultSet rs=stmt.executeQuery("SELECT * FROM hufan");
        //将游标移动到最后一行:
            rs.last();
        //获取最后一行的行号:
         int lastRow=rs.getRow();
        //计算分页后的总页数:
        pageCount=(lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1);
        //当前显示的初始页数:
        int showPage=1;
        //告知客户总页数:
      %>
        <P> 共有<%=pageCount%>页
        <BR>每页显示<%=pageSize%>条记录.
      <% //获取客户想要显示的页数:
          String integer=request.getParameter("showPage");
               if(integer==null)
                  { integer="1";
                  }
           try {showPage=Integer.parseInt(integer); 
               }
           catch(NumberFormatException e)
               {showPage=1;
               }
          if(showPage<=1)
             {showPage=1;
             }
          if(showPage>=pageCount)
             {showPage=pageCount;
             }
      %>
        <BR>目前显示第<%=showPage%>页 
      <% //如果要显示第showPage页,那么游标应移到posion的值是:
         int posion=(showPage-1)*pageSize+1;
          rs.absolute(posion); // 设置游标的位置
          out.print("<Table Border>");
          out.print("<TR>");
            out.print("<TH width=100>"+"姓名");
            out.print("<TH width=100>"+"年龄");
            out.print("<TH width=50>"+"数学");
            out.print("<TH width=50>"+"英语");
            out.print("<TH width=50>"+"物理");
          out.print("</TR>");
       for (int i=1;i<=pageSize;i++)
        { out.print("<TR>");
             out.print("<TD >"+rs.getString("xingming")+"</TD>");
             out.print("<TD >"+rs.getString("nianling")+"</TD>");
             out.print("<TD >"+rs.getInt("shuxue")+"</TD>");
             out.print("<TD >"+rs.getInt("yingyu")+"</TD>");
             out.print("<TD >"+rs.getInt("wuli")+"</TD>");  
         out.print("</TR>") ;
          rs.next(); 
         }
       out.print("</Table>");
      conn.close();
     }
   catch(SQLException e1) {}
 %>
</BODY>
</HTML> 

你可能感兴趣的:(jsp,String,Microsoft,Integer,Access,scroll)