Paging program implemented with JSP

Source: Internet
Author: User
<% @ 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 ();

%>

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.