<%@ 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.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection Con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database05","sa","");
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>
              <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 ClassList_Tab";
             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++;
             Integer n = (CurrPage-1)*5+1;
             SQL = "select top 5 * from ClassList_Tab where CID>="+
"("+
"Select Max(CID) From (Select top "+n.toString()+" * From ClassList_Tab) as Class"+
")";
             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("CID")%></span></td>
               <td nowrap><span class="style3"><%=Rs.getString("CName")%></span></td>
               <td nowrap><span class="style3"><%=Rs.getString("CStartDate")%></span></td>
               <td nowrap><span class="style3"><%=Rs.getString("CPeriod")%></span></td>
               <td nowrap><span class="style3"><%=Rs.getString("CSchooling")%></span></td>
               <td nowrap><span class="style3"><%=Rs.getString("CType")%></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>