jsp-------------之分页技术(一)
2015-01-08 21:52
197 查看
jsp分页技术之:
如下图:百度的喵
View Code
下面是效果图:
![](http://images.cnitblog.com/blog/539316/201501/082111176406271.jpg)
如下图:百度的喵
<%@page import="java.sql.ResultSetMetaData"%> <%@page import="java.sql.DriverManager"%> <%@page import = "java.sql.PreparedStatement"%> <%@page import = "java.sql.Connection"%> <%@page import = "java.sql.ResultSet" %> <%@ page language = "java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <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> <% int default_size =3; int pageCount=0; //页面总数 int rowCount=0; //行数统计 int pagenow=0; //当前页面 int pageSize=default_size; //页面的显示数目 String Driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/myshop"; String sql = ""; Connection ct=null ; PreparedStatement pst=null; ResultSet rs=null; ResultSetMetaData rsmd =null; Class.forName(Driver); ct = DriverManager.getConnection(url,"root","123"); //第一次: 统计书本的种类数 sql = "select count(*) from book ;"; pst = ct.prepareStatement(sql); rs = pst.executeQuery(); //或的网页总数 while(rs.next()) pageCount=rs.getInt(1); String usernow = request.getParameter("pagenow"); if(usernow != null && !usernow.isEmpty() ){ pagenow = Integer.parseInt(usernow); } sql ="select * from book limit "+pagenow*pageSize+","+pageSize; pst = ct.prepareStatement(sql); rs = pst.executeQuery(); rsmd = rs.getMetaData(); %> <%-- 将所有的页面全部用一个页面来显示 --%> <hr> <body> <table border="2" > <tr> <td> 书籍编号 </td><td> 书籍名称 </td> <td> 书籍作者 </td><td> 书籍出版社 </td> <td> 价 格 </td><td> 书籍数量 </td> </tr> <% int col = rsmd.getColumnCount(); while(rs.next()){ %> <tr> <% for(int i=1; i<=col ;i++){ %> <td><%=rs.getString(i)%></td> <% } %> </tr> <% } %> </table> <hr> <% if(pagenow>0) { %> <%System.out.println("pagenow_1"+pagenow); %> <a href=wahaha.jsp?pagenow=<%=pagenow-1%> >【 上一页 】 </a> <% }else{ %> <a href=wahaha.jsp?pagenow=<%=pagenow%> >【 上一页 】 </a> <% } %> <% int useID=0; int pagenum = pageCount/pageSize ; if( pageCount % pageSize != 0 ) pagenum++ ; for( useID=1 ; useID<=pagenum ; useID++ ){ %> <a href=wahaha.jsp?pagenow=<%=useID-1 %> >【<%=useID %>】</a> <% } if( pagenow +1<pagenum ){ %> <a href=wahaha.jsp?pagenow=<%=1+pagenow%> >【 下一页 】 </a> <% }else{ %> <a href=wahaha.jsp?pagenow=<%=pagenow%> >【 下一页 】 </a> <% }%> </body> </html>
View Code
下面是效果图:
![](http://images.cnitblog.com/blog/539316/201501/082111176406271.jpg)
![](http://images.cnitblog.com/blog/539316/201501/082111309061719.jpg)