jsp实现页面分页功能代码
核心代码:
<%@pagecontentType="text/html"pageEncoding="GB2312"language="java"%> <%@pageimport="java.sql.*"%>hello <%! publicstaticfinalStringDRIVER="com.mysql.jdbc.Driver"; publicstaticfinalStringUSER="root"; publicstaticfinalStringPASS=""; publicstaticfinalStringURL="jdbc:mysql://localhost:3306/teachinfo"; publicstaticfinalintPAGESIZE=5; intpageCount; intcurPage=1; %> <% //一页放5个 Stringuser=null; Stringpass=null; try{ Class.forName(DRIVER); Connectioncon=DriverManager.getConnection(URL,USER,PASS); Stringsql="SELECT*FROMdepartment"; PreparedStatementstat=con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); ResultSetrs=stat.executeQuery(); rs.last(); intsize=rs.getRow(); pageCount=(size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1); Stringtmp=request.getParameter("curPage"); if(tmp==null){ tmp="1"; } curPage=Integer.parseInt(tmp); if(curPage>=pageCount)curPage=pageCount; booleanflag=rs.absolute((curPage-1)*PAGESIZE+1); out.println(curPage);//输出到屏幕上 intcount=0; do{ if(count>=PAGESIZE)break; intdepartmentid=rs.getInt(1); Stringdepartmentname=rs.getString(2); count++; %> <% }while(rs.next()); con.close(); } catch(Exceptione){ } %> <%=departmentid%> <%=departmentname%> 首页 ">上一页 ">下一页 ">尾页 第<%=curPage%>页/共<%=pageCount%>页
本篇代码希望各位朋友喜欢!