鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 网站制作教程 > JSP > >

JSP实例三、sql分页(1)

来源:互联网 作者:佚名 时间:2015-10-26 08:37
index.jsp %@ page language="java" import="java.sql.*" import="java.lang.*" import="java.util.*" pageEncoding="GB2312"% % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+reques

index.jsp

<%@ page language="java" import="java.sql.*" import="java.lang.*" import="java.util.*" pageEncoding="GB2312"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%!

    int CountPage = 0;

    int CurrPage = 1;

    int PageSize = 5;

    int CountRow = 0;

    public Connection Con() {

        try

        {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Connection Con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb?user=root&password=zhz&useUnicode=true&characterEncoding=gb2312");

            return Con;

        }

        catch(Exception e)

        {

            return null;

        }

    }

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>利用 SQL 语句实现分页</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">

    -->

 

    <style type="text/css">

<!--

.style1 {

    color: #6633CC;

    font-weight: bold;

    font-size: 16px;

}

.style3 {font-size: 12}

-->

    </style>

  </head>

<body>

    <div align="center" class="style1">

      <h1> 利用 SQL 语句实现分页 </h1>

      <table width="200" border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bordercolordark="#999999" bordercolorlight="#FFFFFF">

        <tr>

          <td colspan="2">

          <table width="511" border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bordercolordark="#999999" bordercolorlight="#FFFFFF">

            <tr>

              <td nowrap><span class="style3">编号</span></td>

              <td nowrap><span class="style3">姓名</span></td>

              <td nowrap><span class="style3">学号</span></td>

            </tr>

            <!--获取表记录-->

            <%

              String StrPage = request.getParameter("Page");

              if (StrPage == null)

              {

                  CurrPage = 1;

              }

              else

              {

                  CurrPage = Integer.parseInt(StrPage);

              }

              Connection Con = Con();

              Statement Smt;

              ResultSet Rs;

              if (Con != null)

              {

                  try

                  {

                      String SQL = "Select * From user";

                      Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

                      Rs = Smt.executeQuery(SQL);

                      Rs.last();

                      int i = 0;

                      CountRow = Rs.getRow();

                      CountPage = (CountRow/PageSize);

                      if (CountRow%PageSize>0)

                          CountPage++;

                      int posion = (CurrPage-1)*5+1;

                      SQL = "select * from user limit  "+(posion-1)+",5";

                        Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

                      Rs = Smt.executeQuery(SQL);

 

                      while (Rs.next())

                      {

                          %><tr>

                              <td nowrap><span class="style3"><%=Rs.getString("id")%></span></td>

                              <td nowrap><span class="style3"><%=Rs.getString("userName")%></span></td>

                              <td nowrap><span class="style3"><%=Rs.getString("userPwd")%></span></td>

                          </tr><%

                      }

                  }

                  catch (SQLException e)

                  {

                      out.println(e.getMessage());

                  }

                  finally

                  {

                      Con.close();

                  }

              }

            %>

          </table></td>

        </tr>

        <tr>

         

          <td width="251">[<%=CurrPage%>/<%=CountPage %>] 每页5条 共<%=CountRow%>条记录<%=(CurrPage-1)*5+1%></td>

          <td width="260"><div align="right">

          <%if (CurrPage>1){%>

          <a href="index.jsp?Page=<%=CurrPage-1%>">上一页</a>

          <%

                

             }

          %>

          <%if (CurrPage<CountPage){%>

          <a href="index.jsp?Page=<%=CurrPage+1%>">下一页</a>

          <%

                

             }

          %>

          <a href="index.jsp?Page=<%=CountPage%>">最后一页</a>

          </div></td>

        </tr>

      </table>

     

      <h1 align="center"><br>

      </h1>

</div>

</body>

</html>

 

 

 

网友评论
<