6 of the JAVA/JSP Learning Series (MySQL paging example)

Source: Internet
Author: User

I. Preparations 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_INSENSITIVE, 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;

%>

<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> name </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>

</Tr>

<% SqlRst. next ();

I ++;

}

}

%>

</Table>

Page <% = intPage %> total page <% = intPageCount %>

<% If (intPage <intPageCount) {%> <a href = "mysqlpage. jsp? Page = <% = intPage + 1%> "> next page </a> <%} %>

<% If (intPage> 1) {%> <a href = "mysqlpage. 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 ();

%>

3. How to run it?

See the previous article "JAVA/JSP learning series five (JDBC-ODBC flip example)".

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.