Pagination of JSP database operation data

Source: Internet
Author: User


<% @ 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 Program
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 ();
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.