在网上还是看到有人因为这个问题提问,我把我当时写出的分页代码粘贴出来.
- <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>分页</title>
- </head>
- <body>
- <%
- try
- {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
- String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
- Connection con=DriverManager.getConnection(url,"sa","sql");
- int i=1;
- int numPages=5;
- String pages = request.getParameter("page");
- int currentPage = 1;
- int intPageCount=1;
- currentPage=((pages==null)? 1 :(Integer.parseInt(pages)));
- if (currentPage<1)
- currentPage=1;
- String sql = "select count(au_id) as id from authors";
- Statement stmt=con.createStatement();
- ResultSet rs=stmt.executeQuery(sql);//
- if(rs.next())
- intPageCount = rs.getInt("id");
- rs.close();
- int PageCount=(intPageCount%numPages==0)?(intPageCount/numPages):(intPageCount/numPages+1);
- int nextPage;
- int upPage;
- nextPage = currentPage+1;
- if (nextPage>=PageCount)
- nextPage=PageCount;
- upPage = currentPage-1;
- if (upPage<=1)
- upPage=1;
- sql="select au_fname,contract,zip from authors";
- stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- rs=stmt.executeQuery(sql);
- i=0;
- while((i<numPages*(currentPage-1)) &&rs.next())
- {
- i++;
- }
- while((i<numPages*currentPage)&&rs.next())
- {
- i++;
- out.println("au_fname:"+rs.getString("au_fname")+" contract"+rs.getString("contract")+" zip:"+rs.getString("zip")+"<br/>");
- }
- rs.close();
- stmt.close(); //(4)关闭
- con.close();
- %><p></p>合计:<%=currentPage%>/<%=PageCount%>
- <a href="fy.jsp?page=1">第一页</a><a href="fy.jsp?page=<%=upPage%>">上一页</a>
- <%
- for(int j=1;j<=PageCount;j++)
- {
- if(currentPage!=j)
- {
- %> <a href="fy.jsp?page=<%=j%>">[<%=j%>]</a> <%
- }
- else
- {
- out.println(j);
- }
- } %>
- <a href="fy.jsp?page=<%=nextPage%>">下一页</a><a href="fy.jsp?page=<%=PageCount%>">最后页 </a>
- <%
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- %>
- </body>
- </html>