JSP---Paging

Source: Internet
Author: User

1. Building Java Classes

public class Studentmanage {
int pageSize = 3;//number of records per page

Get Total Records
private int Gettotalnum (String strwhere) throws SQLException {
DbAccess DbAccess = new DbAccess ();
int total = 0;
String sql = "SELECT COUNT (*) from student where 1=1 and" + strwhere;
Dbaccess.excutequery (SQL);
ResultSet rs = Dbaccess.getrs ();
if (Rs.next ()) {
Total = Rs.getint (1);
}
Dbaccess.close ();
return total;
}

Get total pages (that is, the largest page number)
public int Getmaxpageno (String strwhere) throws SQLException {
int Maxpageno;
if (Gettotalnum (strwhere)%pagesize==0)
Maxpageno=gettotalnum (strwhere)/pagesize;
else {
Maxpageno=gettotalnum (strwhere)/pagesize+1;
}
return Maxpageno;
}

Gets the record for the specified page
Public list<student> studlist (String strwhere, int page)
Throws SQLException {
DbAccess DbAccess = new DbAccess ();
list<student> list = new arraylist<student> ();
int Total=gettotalnum (strwhere);
String sql = "SELECT * from student where 1=1 and" + strwhere;
Dbaccess.excutequery (SQL);
ResultSet rs = Dbaccess.getrs ();
int t = (page-1) * pageSize + 1;//The first record number of the current page
if (t <= total) {
Rs.absolute (t);//need to modify the DAL layerst = conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_read_only);
do {
Student Student = new Student ();
STUDENT.SETSTUD_ID (rs.getstring ("stud_id"));
Student.setstud_name (rs.getstring ("Stud_name"));
Student.setstud_sex (rs.getstring ("Stud_sex"));
Student.setstud_class (rs.getstring ("Stud_class"));
Student.setstud_fav (rs.getstring ("stud_fav"));
List.add (student);
} while (Rs.next () && list.size () < pageSize);
Rs.close ();
}
Dbaccess.close ();
return list;
}

2. JSP page Call

<%
String curpage = Request.getparameter ("Curpage");
if (Curpage = = NULL | | curpage = = "") {
Curpage = "1";
}
int cp = Integer.parseint (curpage);
Studentmanage studentmanage = new Studentmanage ();
int lastpage = Studentmanage.getmaxpageno ("1=1");
list<student> list = studentmanage.studlist ("1=1", CP);
%>

Current Section <%=cp%>/<%=lastPage%> page

<%
if (cp > 1) {
%>
<a href= "? curpage=1" > First page </a>&nbsp;
<a href= "?curpage=<%=cp-1%>" > Prev </a>
<%
}
%>
<%
if (CP < LastPage) {
%>
<a href= "? CURPAGE=<%=CP + 1%>" > Next </a>&nbsp;
<a href= "?curpage=<%=lastpage%>" > last page </a>
<%
}
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.