JSP 联接MYSQL 数据库 实现分页

JSP 连接MYSQL 数据库 实现分页
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>分页显示记录</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">
-->

  </head>
 
  <body>
   <%
   String driverName="com.mysql.jdbc.Driver";
   String userName="root";
   String userPassword="root";
   String dbName="friend";
   //连接字符串
   String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPassword;
   Class.forName(driverName).newInstance();
   Connection connection=DriverManager.getConnection(url);
   Statement statement=connection.createStatement();
   int PageSize=8;         //每页显示的记录条数
   int StartRow=0;        //开始显示记录的编号
   int PageNo=0;         //需要显示的页数
   int CounterStart=0;   //每页页码的初始值
   int CounterEnd=0; //显示页码的最大值
   int RecordCount=0; //总记录数
   int MaxPage=0; //总页数
   int PrevStart=0; //前一页
   int NextPage=0; //下一页
   int LastRec=0;
   int LastStartRecord=0; //最后一页开始显示记录的编号
  
   //获取需要显示的页数,由用户提交
   if(request.getParameter("PageNo")==null){//如果为空,则表示第一页
   if(StartRow==0){
   PageNo=StartRow+1;//设定为1
   }
   }else{
   PageNo=Integer.parseInt(request.getParameter("PageNo"));//获取用户提交的页数
   StartRow=(PageNo-1)*PageSize; //获取开始显示的记录编号
   }
   //根据当前的页数显示一定数量的页数链接
   //设置显示页码的初始值
   if(PageNo%PageSize==0){
   CounterStart=PageNo-(PageSize-1);
   }else{
   CounterStart=PageNo-(PageNo%PageSize)+1;
   }
   CounterEnd=CounterStart+(PageSize-1);
   %>
//从数据库中取得全部结果集,并根据每页显示的记录数来判断可分为多少也页,然后取得当前页将要显示的结果集,其代码如下:
   <%
   //获取总记录数
   ResultSet rs=statement.executeQuery("select count(*) from friends");
   rs.next();
   RecordCount=rs.getInt(1);
//此处特别声明,sql语句里LIMIT后面要有一个空格,后面的", "也有一个空格,否则会报错(sql语法错误)
   rs=statement.executeQuery("SELECT id,name,tel FROM friends ORDER BY id DESC LIMIT "+StartRow+", "+PageSize);
   //获取总页数
   MaxPage=RecordCount%PageSize;
   if(RecordCount%PageSize==0){
   MaxPage=RecordCount/PageSize;
      }else{
      MaxPage=RecordCount/PageSize+1;
      }
     %>
   <center>
//显示当前页数据,通过一个循环,使用表格将当前页对应的结果集显示出来
   <table width="450" border="1">
   <tr>
   <th>编号</th>
   <th>姓名</th>
   <th>电话</th>
   </tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("name") %></td>
<td><%=rs.getString("tel") %></td>
</tr>
<%
}
  %>
   </table>
//实现分页显示的链接,当前用回单击这些链接的时候会跳转到相应的页面来查看数据
    <%
     out.print("<font size=4>");
     //显示第一页或者前一页的链接
     //若果当前页或是第一页,则显示第一页和前一页的链接
     if(PageNo!=1){
     PrevStart=PageNo-1;
     out.print("<a href=db5.jsp?PageNo=1>第一页</a>:");
     out.print("<a href=db5.jsp?PageNo="+PrevStart+">前一页</a>");
     }
     out.print("[");
     //打印需要显示的页码
     for(int c=CounterStart;c<=CounterEnd;c++){
     if(c<MaxPage){
     if(c==PageNo){
     if(c%PageSize==0){
     out.print(c);
     }else{
     out.print(c+" ,");
     }
     }else if(c%PageSize==0){
     out.print("<a href=db5.jsp?PageNo="+c+">"+c+"</a>");
     }else{
     out.print("<a href=db5.jsp?PageNo="+c+">"+c+"</a>");
     }
     }else{
     if(PageNo==MaxPage){
     out.print(c);
     break;
     }else{
     out.print("<a href=db5.jsp?PageNo="+c+">"+c+"</a>");
     break;
     }
     }
     }
     out.print("]");
     if(PageNo<MaxPage){//如果当前页不是最后一页,则显示下一页链接
     NextPage=PageNo+1;
     out.print("<a href=db5.jsp?PageNo="+NextPage+">下一页</a>");
     }
     //同时如果当前页不是最后一页,要显示最后一页的链接
     if(PageNo<MaxPage){
     LastRec=RecordCount%PageSize;
     if(LastRec==0){
     LastStartRecord=RecordCount-PageSize;
     }else{
     LastStartRecord=RecordCount-LastRec;
     }
     out.print(":");
     out.print("<a href=db5.jsp?PageNo="+MaxPage+">最后一页</a>");
     }
     out.print("</front>");
   %>
//关闭释放资源
   <%
   rs.close();
   statement.close();
   connection.close();
     %>
      </center>
  </body>
</html>