JSP+MSSQL实现数据分页显示

2011年2月19日 发表评论 阅读评论

通过编写JSP代码在网页中将数据库中公园信息按照顺序进行分页显示,每页10条记录。JSP代码程序如下:

<%@page contentType="text/html;charset=utf-8"%>
<%@page import="java.util.*"%>
<%@ page  language="java" import="java.sql.*"%>
<%@include file="inc1.jsp"%>
<getGardenList>
<%!
    public static String returnToBr(String sStr) {
    if (sStr == null || sStr.equals("")) {
    return "";
    }    
    return sStr.trim();
    }
%>
<%   
//    ResultSet rs= null;
     int pagecount=0;
     int count=0;
     int n;
     String sql_query="";
     String see="";
//    Connection conn = null;
//    Statement stmt = null;
//    int li_temp = 0;
    String pages = new            String(request.getParameter("pages").getBytes("ISO8859-1"),"utf-8");
//    if(findString == null)
//        findString = "";
//    System.out.println(findString);
    try
    {
        //连接数据库
        Class.forName(DBDriver);
        Connection conn=DriverManager.getConnection(DBLocation,DBUser,DBPwd);
        Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        sql_query = "select * from yl_garden ";
        ResultSet rs=stmt.executeQuery(sql_query);
//    rs=stmt.executeQuery(sql_query);
%>   
<table width="100%" border="1">
  <tr>
    <td><div align="center">序号</div></td>
    <td><div align="center">名称</div></td>
    <td><div align="center">地址</div></td>
    <td><div align="center">级别</div></td>
  </tr>
  <%    rs.last();
        count=rs.getRow();
        rs.beforeFirst();
        n=(Integer.parseInt(pages)-1)*10;
        while(rs.next()){
            if(n>=rs.getRow())
                continue;   
            out.println("<tr>");
            see=rs.getString("gardenid");
            see= returnToBr(see);   
            out.println("<td>"+ see.trim()+"</td>");
            see=rs.getString("gardenname");
            see= returnToBr(see);   
            out.println("<td>"+ see.trim()+"</td>");
            see=rs.getString("gardenplace");
            see= returnToBr(see);   
            out.println("<td>"+ see.trim()+"</td>");
            see=rs.getString("gardenlevel");
            see= returnToBr(see);   
            out.println("<td>"+ see.trim()+"</td>");
            out.println("</tr>");
            n++;
            if(n%10==0)
                break;
        }
        %>
        </table>
        <center>
            第<%out.println(pages);%>页,
            共<%if(count%10==0)
                    pagecount=count/10;
                else
                    pagecount=count/10+1;
                out.println(pagecount);
            %>页<br>
            <a href="flex_getGardenList.jsp?pages=1">最前一页</a>
            ,<a href="flex_getGardenList.jsp?pages=
            <%if(Integer.parseInt(pages)==1)
                out.println(pages);
              else
                out.println(Integer.parseInt(pages)-1);
            %>">上一页</a>,
            <a href="flex_getGardenList.jsp?pages=
            <%if(Integer.parseInt(pages)==pagecount)
                out.println(pages);
              else
                out.println(Integer.parseInt(pages)+1);
            %>">下一页</a>
            ,<a href="flex_getGardenList.jsp?pages=<%=pagecount%>">最后一页</a>
        </center>
        <%
        rs.close();
        stmt.close();
        conn.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
%>
</getGardenList>

点击查看:图片演示效果


转载请注明来自:[闪电博客]http://shandian.biz/321.html

分类: 电脑知识 标签:
  1. 本文目前尚无任何评论.