MySQL paging example 1. preparation before running
Download the mysql jdbc driver (a jar file) and load it in CLASSPATH (for the method, see one of the JAVA/JSP Learning Series (JDK installation).
(If not found, download from this site)
Create a MySQL database test
The database has a table: note with the field name (varchar)
II. download and install
<% @ Page contentType = "text/html; charset = gb2312" %>
<% Java. SQL. Connection sqlCon; // database Connection object
Java. SQL. Statement sqlStmt; // SQL Statement object
Java. SQL. ResultSet sqlRst; // result set object
Java. lang. String strCon; // database connection String
Java. lang. 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.
Java. lang. String strPage;
Int I;
// Set the number of records displayed on one page
IntPageSize = 2;
// Obtain the page number to be displayed
StrPage = request. getParameter ("page ");
If (strPage = null ){
// Indicates that the QueryString parameter does not contain the page parameter. The first page of data is displayed.
IntPage = 1;
} Else {
// Converts a string to an integer.
IntPage = java. lang. Integer. parseInt (strPage );
If (intPage <1) intPage = 1;
}
// Load the JDBC driver
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// Connect to the database
SqlCon = java. SQL. DriverManager. getConnection ("jdbc: mysql: // localhost/test ");
// Create a statement object
SqlStmt = sqlCon. createStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENS99vIVE, java. SQL. ResultSet. CONCUR_READ_ONLY); // execute the SQL statement
StrSQL = "select name from note ";
// 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;
%>
JSP database operation routine-data paging-JDBC 2.0-mysql
<% 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
<% SqlRst. next ();
I ++;
}
}
%>
| Name |
|
<% = SqlRst. getString (1) %>
|
Page <% = intPage %> Total page <% = intPageCount %>
<% If (intPage "> Next page <% }%>
<% If (intPage> 1) {%> "> Previous Page <%} %>
<%
// Close the result set
SqlRst. close ();
// Close the SQL statement object
SqlStmt. close ();
// Close the database
SqlCon. close ();
%>
3. how to run it?
See the previous article "JAVA/JSP Learning Series Five (JDBC-ODBC flip example)".