Monlystar's profileMyonlystar的共享空间BlogGuestbookNetwork Tools Help

Blog


    September 18

    jsp页面实现分页显示数据库查询内容

         对于一个中等以上项目来说,庞大的表信息无法实现在一个jsp页面中预览,分页显示势在必行。本程序主要实现jsp页中查询数据分页显示功能:
         <%
     String sql=null;
     ResultSet rs=null;
     int PageSize=5;
     int Page=1;
     int totalPage=1;
     int totalRecord=0;
     Connection con=splBean.getConnet();
     Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
     //算出总行数tatalRecord
     sql="select count(*) as recordcount from student";
     rs=stmt.executeQuery(sql);
     if(rs.next()) totalRecord=rs.getInt("recordcount");
     System.out.println(totalRecord);
     //算出总页数totalPage
     if(totalRecord%PageSize==0) totalPage=totalRecord/PageSize;
     else totalPage=(int)Math.floor(totalRecord/PageSize)+1;//最后还空余一页情况,Math.floor(x):比x小的最大值。
     if(totalPage==0) totalPage=1;//数据库中一条数据也没有的处理情况;
     //捕捉form表单提交的Page元素
     if(request.getParameter("Page")==null||request.getParameter("Page")=="") Page=1;
     else try{
     Page=Integer.parseInt(request.getParameter("Page"));
     }catch(java.lang.NumberFormatException e){
     Page=1;
     }
     //如果Page非常规数值处理
     if(Page>totalPage) Page=totalPage;
     if(Page<1) Page=1;
     sql="select * from student";
     rs=stmt.executeQuery(sql);
     rs.absolute((Page-1)*PageSize+1);
     ResultSetMetaData meta=rs.getMetaData();
     int col=meta.getColumnCount();//得到表头有多少列
     out.println("<table border='1'>");
     //输出表头信息
     out.println("<tr>");
     for(int i=1;i<col+1;i++)
     {
      out.println("<td>");
      out.print("列名:");
      out.print(meta.getColumnName(i)+"<br>");
      out.print("类型:");
      out.print(typetoString(meta.getColumnType(i)));//检索并返回指定列的sql类型
      out.println("</td>");
     }
     out.println("<td>更改</td>");
     out.println("<td>删除</td>");
     out.println("</tr>");
     for(int iPage=1;iPage<PageSize+1;iPage++)
     { 
     out.print("<tr><td>"+rs.getString(1)+"</td>");
     out.print("<td>"+rs.getString(2)+"</td>");
     out.print("<td>"+rs.getString(3)+"</td>");
     out.print("<td>"+rs.getString(4)+"</td>");
     out.print("<td>"+rs.getString(5)+"</td>");
     out.print("<td>"+rs.getString(6)+"</td>");
     out.print("<td><a href=\"change.jsp?Xuehao="+rs.getString(1)+"\">修改</a></td>");
     out.print("<td><a href=\"del.jsp?Xuehao="+rs.getString(1)+"\">删除</a></td>");
     if(!rs.next()) break;
     }
     out.println("</table>");
     stmt.close();
     con.close();
    %>
    其中的typetoString()函数的作用是转换得到sql相应的结果类型.如typetoString(1)的得到的结果伟integer
    截图:
    2

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://cid-b02da37102052119.spaces.live.com/blog/cns!B02DA37102052119!151.trak
    Weblogs that reference this entry
    • None