<% @ Page contentType = "text/html; charset = 8859_1" %>
<%
// Variable Declaration
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 page parameter is not found in QueryString. the first page of data is displayed.
IntPage = 1;
}
Else {// convert a string to an integer
IntPage = java. lang. Integer. parseInt (strPage );
If (intPage <1) intPage = 1;
}
// Load the JDBC driver
Java. SQL. DriverManager. registerDriver (new oracle. jdbc. driver. OracleDriver ());
// Set the database connection string
StrCon = "jdbc: oracle: thin: @ linux: 1521: ora4cweb ";
// Connect to the database
SqlCon = java. SQL. DriverManager. getConnection (strCon, "hzq", "hzq ");
// Create a read-only SQL statement object that can be rolled
SqlStmt = sqlCon. createStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENSITIVE, java. SQL. ResultSet. CONCUR_READ_ONLY );
// Prepare SQL statements
StrSQL = "select name, age from test ";
// 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-Oracle </title>
</Head>
<Body>
<Table border = "1" cellspacing = "0" cellpadding = "0">
<Tr>
<Th> name </th>
<Th> age </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>
</Tr>
<%
SqlRst. next ();
I ++;
}
}
%>
</Table>
Page <% = intPage %> total <% = intPageCount %> page <% if (intPage <intPageCount) {%> <a href = "jdbc20-oracle.jsp? Page = <% = intPage + 1%> "> next page </a> <% }%> <% if (intPage> 1) {%> <a href =" jdbc20-oracle.jsp? Page = <% = intPage-1 %> "> previous page </a> <%} %>
</Body>
</Html>
<%
// Close the result set
SqlRst. close ();
// Close the SQL statement object
SqlStmt. close ();
// Close the database
SqlCon. close ();
%>