We use a beans to implement the paging display of the database record, and there are many ways. This example is a cursor of the ResultSet class to achieve paging.
This example is: page JSP, page display record
Page Beans
Database connection beans
This example is tested under Tomcat.
Key ideology:
Suppose the total number of records is m, the number of records shown per page is N, then the calculation formula of the total number of pages is:
Total page = (m% n) == 0? (M / n): (m / n 1);
If you are ready to display the contents of the P page, the cursor should be moved to (P-1) * n 1 record.
The source code is as follows:
1, page beans
Pagenumber.java
Package bean;
Public Class Pagenumber
{
Int rowcount = 1, // total record number
PageSize = 1, // Number of records displayed per page
ShowPage = 1, // Set the number of page numbers pre-displayed
PageCount = 1; // Total number after page
Public void setRowcount (int N) {
Rowcount = N;
}
Public int getRowcount () {
Return rowcount;
}
Public void setPageCount (int R, int P) {
RowCount = R;
Pagesize = P;
PageCount = (RowCount% PageSize) == 0? (RowCount / Pagesize) :( RowCount / Pagesize 1);
/ / Calculate the number of pages after paging
}
Public int getPagecount () {
Return PageCount;
}
Public void setshowpage (int N) {
Showpage = n;
}
Public int getShowpage () {
Return Showpage;
}
Public void setpagesize (int N) {
Pagesize = n;
}
Public int getpagesize () {
Return PageSize;
}
}
2, database connection beans
Package bean;
Import java.sql. *;
Import java.io. *;
Public class myconnection {
Connection con = NULL; // Declare a shared connection object
Statement stat = NULL;
ResultSet RS = NULL;
INT rowcount = 0; // Total number of records
Public myconnection () {
Try {
Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver);
} catch (classnotfoundexception e) {
System.out.print (e);
}
Try {
Con = DriverManager.getConnection ("JDBC: Microsoft: SQLServer: // localhost: 1433;
DatabaseName = student; "" SA "," ");
} catch (sqlexception e) {
System.out.print (e);
}
}
Public ResultSet MyQuery (String SQL) THROWS SQLEXCEPTION {
/ / The cursor of the result set can be moved up and down, but when the database changes, the result set is constant, // cannot use the result set to update the table in the database
Stat = con.createstatement (ResultSet.Type_Scroll_Sensitive,
ResultSet.concur_read_only);
/ / Return the rolling result set
RS = stat.executequery (SQL);
Return RS;
}
Public void myclose () throws sqlexception {
C. close ();
}
}
3, page display recorded JSP page
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.sql. *"%>
<% @ Page Import = "bean.pagenumber"%>
<% @ Page Import = "bean.myconnection"%>
<% @ Page Import = "java.io. *"%>
<%!
// Display the method of database record in form
Public void showlist (ResultSet RS, JSPWriter Out, INT N) {
Try {
Out.print ("
Learn | Name | Mathematics | English | Physical TR>");
For (INT i = 1; i <= n; i ) { OUT.PRINT (" |
---|---|---|---|---|
" Number " TD>");
String name = rs.getstring (2); Out.print (" | " Name " TD>");
INT MATH = rs.getint (3); Out.print (" | " MATH " TD>");
Int english = rs.getint (4); Out.print (" | " ENGLISH " TD>");
INT Physics = rs.getint (5); Out.print (" | " Physics " TD>"); OUT.PRINT (" TR>");
rs.next (); } Out.print (" Table>"); } catch (exception e) { System.out.println (e); } } %> <% ResultSet RS = NULL; INT rowcount = 0; // Total number of records Try { / / Return the rolling result set RS = Mycon.MyQuery ("Select * from students"); // Move the cursor to the last line Rs.last (); // Get the total number of records Rowcount = rs.getrow (); / / Set the number of records displayed per page Handlepage.SetPageSize (2); / / Calculate the total number of pages Handlepage.SetPageCount (Rowcount); Out.print ("Again" HandlePage.getPageCount () "page,"); Out.print ("Display" Handlepage.getPageSize () "Sum Record"); } catch (sqlexception e) { OUT.PRINT (E); } %> <% - Select the form of a page -%> <% String str = response.EncoderedirectURL ("showlist.jsp"); // Get the URL% of showlist.jsp> |