Implement paging Using jsp and mysql

Source: Internet
Author: User


<%
// 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.
Int layer;
Int idlayer;
Idlayer = 1;
Java. lang. String outtext;
Outtext = "";
Java. lang. String strPage;
Int j;
Int I;

// Set the number of records displayed on one page
IntPageSize = 10;

// 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
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();

// Connect to the database
SqlCon = java. SQL. DriverManager. getConnection ("jdbc: mysql: // 218.22.156.3/kjeny_db", "kjeny", "cdgame ");

// Create a statement object
SqlStmt = sqlCon. createStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENSITIVE, java. SQL. ResultSet. CONCUR_READ_ONLY );

// Execute the SQL statement and obtain the result set
SqlRst = sqlStmt.exe cuteQuery ("SELECT * FROM dxswlxh ");

// 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">
<Base target = "contents">
<LINK rel = "stylesheet" href?”css.css ">
<Script language = "JavaScript">
Function byteLen (str ){
Var iByteLen = 0;
If (str = null | str = "") {
IByteLen = 0;
} Else {
For (var I = 0; I <str. length; I ++ ){
If (str. charCodeAt (I) <0 × 80 ){
IByteLen ++;
} Else {
IByteLen + = 2;
}
}
}
Return iByteLen;
}

</Script>
</Head>
<Body topmargin = "0" leftmargin = "0" bgcolor = ccffcc>

<Table width = "100%"> <tr> <td bgcolor = "ff66cc">
> Paging
<Form method = post action = "pink. jsp? Page = 1 "target = ttop>
<Input type = submit value = "first page">
</Form>
<% If (intPage> 1 ){
%>
<Form method = post action = "pink. jsp? Page = intPage-1 %> "target = ttop>
<Input type = submit value = Previous Page>
</Form>

<% }%>

<% If (intPage <intPageCount)
{%>

<Form method = post action = "pink. jsp? Page = <% = intPage + 1%> "target = ttop>
<Input type = submit value = next page>
</Form>
<% }%>
<Form method = post action = "pink. jsp? Page = <% = intPageCount %> "target = ttop>
<Input type = submit value = "last page">
</Form>

Page: <% = intPage %>/<% = intPageCount %> page <% = intPageSize %> entries/Page

</Td> </tr> </table>

<Table>
<Td width = "% 100"> <ul> <%

If (intPageCount> 0 ){
SqlRst. absolute (intPage-1) * intPageSize + 1 );
I = 0;
While (I <intPageSize &&! SqlRst. isAfterLast ()){

String names = sqlRst. getString (3 );

String use_xi = sqlRst. getString (4 );

%>
<Li> <% = names %>-[<% = use_xi %>]
<%
SqlRst. next ();
I ++;
}
}
%>
</Ul> </td> </table>

</Body>
</Html>

<%
// Close the result set
SqlRst. close ();

// Close the SQL statement object
SqlStmt. close ();

// Close the database
SqlCon. close ();
%>

------
If the resultset is empty, Resultset. last () will throw an error message. So before that we need to use Resultset. next () to judge if there are some results.

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.