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>
<a href= "?curpage=<%=cp-1%>" > Prev </a>
<%
}
%>
<%
if (CP < LastPage) {
%>
<a href= "? CURPAGE=<%=CP + 1%>" > Next </a>
<a href= "?curpage=<%=lastpage%>" > last page </a>
<%
}
%>