<% @ Page contentType = "text/html; charset = UTF-8" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. io. *" %>
<Title> Jsp page display record </title> <body bgcolor = "# ffffff">
<%
// Variable Declaration
Connection sqlCon; // database Connection object
Statement sqlStmt; // SQL Statement object
ResultSet sqlRst; // result set object
String strCon; // database connection String
String strSQL; // SQL statement
Int intPageSize; // number of records displayed on one page
Int intRowCount; // The total number of records.
Int intPageCount; // the total number of pages.
Int intPage; // the page number to be displayed.
String strPage;
Int I;
// Set the number of records displayed on one page
IntPageSize = 5;
// Obtain the page number to be displayed
StrPage = request. getParameter ("page ");
If (strPage = null) {// indicates that the page parameter is not found in QueryString. the first page of data is displayed.
IntPage = 1;
}
Else {// convert a string to an integer
IntPage = Integer. parseInt (strPage );
If (intPage <1) intPage = 1;
}
// Load the JDBC driver
Try {
Class. forName ("com. mysql. jdbc. Driver ");
}
Catch (ClassNotFoundException e) {out. print ("loading driver error ");}
// Set the database connection string
StrCon = "jdbc: mysql: // localhost: 3306/test ";;
// Connect to the database
SqlCon = DriverManager. getConnection (strCon, "test", "test ");
// Create a read-only SQL statement object that can be rolled
SqlStmt = sqlCon. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY );
// Prepare SQL statements
StrSQL = "select * from account ";
// Execute the SQL statement and obtain the result set
SqlRst = sqlStmt.exe cuteQuery (strSQL );
// Obtain the total number of records
SqlRst. last ();
IntRowCount = sqlRst. getRow ();
// Calculate the total number of pages
IntPageCount = (intRowCount + intPageSize-1)/intPageSize;
// Adjust the page number to be displayed
If (intPage> intPageCount) intPage = intPageCount;
%>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> JSP database operation routine-data paging-JDBC 2.0-MYSQL </title>
</Head>
<Body>
<Table border = "1" cellspacing = "0" cellpadding = "0">
<Tr>
<TH> id </Th>
<TH> name </Th>
<TH> password </Th>
</Tr>
<%
If (intpagecount> 0 ){
// Locate the record pointer to the first record on the page to be displayed.
Sqlrst. Absolute (intPage-1) * intpagesize + 1 );
// Display data
I = 0;
While (I <intpagesize &&! Sqlrst. isafterlast ()){
%>
<Tr>
<TD> <% = sqlrst. getstring (1) %> </TD>
<TD> <% = sqlrst. getstring (2) %> </TD>
<TD> <% = sqlrst. getstring (3) %> </TD>
</Tr>
<%
Sqlrst. Next ();
I ++;
}
}
%>
</Table>
Page <% = intpage %> total pages <% = intpagecount %> <br>
<A href = "fenye. jsp? Page = 1 "> page 1 </a>
<% IF (intpage> 1) {%> <a href = "fenye. jsp? Page = <% = intPage-1 %> "> previous page </a> <%} %>
<% IF (intpage <intpagecount) {%> <a href = "fenye. jsp? Page = <% = intpage + 1%> "> next page </a> <%} %>
<A href = "fenye. jsp? Page = <% = intpagecount %> "> last page </a>
</Body>
</Html>
<%
// Close the result set
Sqlrst. Close ();
// Close the SQL statement object
Sqlstmt. Close ();
// Close the database
Sqlcon. Close ();
%>