I have sorted out a classic paging program (JSP)

Source: Internet
Author: User
Tags mysql
js| Program | pagination

<%
Variable declaration
Java.sql.Connection Sqlcon; Database Connection objects
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; Total Records
int intpagecount; Total pages
int intpage; Page number to display
int layer;
int idlayer;
Idlayer=1;
Java.lang.String Outtext;
Outtext= "";
Java.lang.String strpage;
Int J;
int i;


Set the number of records to display on a page
Intpagesize = 10;

Get the number to display
Strpage = request.getparameter ("page");
if (strpage==null) {//indicates that there is no page this parameter in QueryString, the first page data is now displayed
Intpage = 1;
}
else{//converts a string to an integral type
Intpage = Java.lang.Integer.parseInt (strpage);
if (intpage<1) intpage = 1;
}

Load JDBC Driver
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();

Connecting to a database
Sqlcon = Java.sql.DriverManager.getConnection ("jdbc:mysql://218.22.156.3/kjeny_db", "Kjeny", "cdgame");

To create a statement object
sqlstmt = Sqlcon.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only) ;

Execute the SQL statement and get the result set
Sqlrst = Sqlstmt.executequery ("SELECT * from Dxswlxh");


Get Total Records
Sqlrst.last ();
Introwcount = Sqlrst.getrow ();

Count Total Pages
Intpagecount = (introwcount+intpagesize-1)/intpagesize;

Adjust the number of pages to be displayed
if (intpage>intpagecount) intpage = Intpagecount;
%>

<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) < 0x80) {
ibytelen++;
} else {
Ibytelen + 2;
}
}
}
return Ibytelen;
}
</script>
<body topmargin= "0" leftmargin= "0" bgcolor=ccffcc>


<table width= "100%" ><tr><td bgcolor= "ff66cc" >
>> pagination
<form method=post action= "pink.jsp?page=1" target=ttop>
<input type=submit value= "Home" >
</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" >
</form>


Page:<%=intpage%>/<%=intpagecount%> page <%=intPageSize%> article/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>

<%
Close result set
Sqlrst.close ();

Close an SQL statement object
Sqlstmt.close ();

Close 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.