One, write two methods in the DAO Class: Get the total number of pages and the data of the specified page
Get Total Pages
Public intGetpagecount (intPageSize)throwsexception{Try{conn=dbconnection.getconnection (); String SQL= "SELECT count (*) from car"; Stat=conn.preparestatement (SQL); RS=Stat.executequery (); Rs.next (); intRowscount=rs.getint (1); intPagecount= (int) Math.ceil (1.0*rowscount/pagesize);//figure out how many pages are needed in total returnPageCount; } finally{conn.close (); }
Get data for a specified page
PublicArraylist<car> Getpagecar (intPageNo,intPageSize)throwsexception{//two parameters are current page, number of rows per pageArraylist<car> list=NewArraylist<car>(); Try{conn=dbconnection.getconnection (); String SQL= "SELECT * FROM car limit?,?"; Stat=conn.preparestatement (SQL); Stat.setint (1, (pageNo-1) *pageSize); the first row of data from this page, and how many rows of data are in front of it stat.setint (2, pageSize);//how many rows per pagers=Stat.executequery (); while(Rs.next ()) {Car data=NewCar (); Data.setcode (Rs.getstring (1)); Data.setname (Rs.getstring (2)); Data.setbrand (Rs.getstring (3)); Data.settime (Rs.getdate (4)); Data.setoil (Rs.getdouble (5)); Data.setpowers (Rs.getint (6)); Data.setexhaust (Rs.getint (7)); Data.setprice (Rs.getdouble (8)); Data.setimage (Rs.getstring (8)); List.add (data); } } finally{conn.close (); } returnlist; }
Ii. servlet Processing of data
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Get Parameters intPageno=1; String s=request.getparameter ("Pgno"); if(s!=NULL) {PageNo=Integer.parseint (s); } //working with Data Try { intPagecount=NewCardao (). Getpagecount (PAGESIZE);//Get Total PagesArraylist<car> list=NewCardao (). Getpagecar (PageNo, PAGESIZE);//get the specified page data intCurrentpage=PageNo; Request.setattribute ("CurrentPage", currentpage); Request.setattribute ("PageCount", PageCount); Request.setattribute ("Cars", list); intpageprev=pageno>1?pageno-1:1;//Previous Page intpagenext=pageno<pagecount?pageno+1:pagecount;//Next PageRequest.setattribute ("Pagenow", PageNo); Request.setattribute ("Pageprev", Pageprev); Request.setattribute ("Pagenext", Pagenext); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); } //JumpRequest.getrequestdispatcher ("home.jsp"). Forward (request, response); }
Third, in the JSP page output
<%@page Import="Com.itnba.maya.bean.Car"%><%@page Import="java.util.ArrayList"%><%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title><Script>functionaaa () {document.getElementById ("F1"). Submit ();}</Script></Head><Body><H1>Car paging</H1><C:foreachItems= "${cars}"var= "C"><Div><spanstyle= "Width:100px;display:inline-block;">${c.code}</span><spanstyle= "Width:300px;display:inline-block;">${c.name}</span></Div></C:foreach><!--The following is a paging link--><ahref= "Show?pgno=1">Home</a><ahref= "Show?pgno=${pageprev}">Previous page</a><C:foreachbegin= "1"End= "${pagecount}"var= "I"><ahref= "Show?pgno=${i}">${i}</a> </C:foreach><ahref= "Show?pgno=${pagenext}">Next page</a><ahref= "Show?pgno=${pagecount}">Last</a><formID= "F1"Method= "Get"Action= "Show"><Selectname= "Pgno"onchange= "aaa ()"><C:foreachbegin= "1"End= "${pagecount}"var= "I"><C:choose> <C:whenTest= "${pagenow = = i}"> <optionvalue= "${i}"selected= "Selected" >${i}</option> </C:when> <c:otherwise> <optionvalue= "${i}">${i}</option> </c:otherwise></C:choose></C:foreach></Select></form></Body></HTML>
Operation Result:
MySQL Paging query