Efficient and simple jsp paging code

Source: Internet
Author: User

<% @ Page import = "java. SQL. *, java. io. *, java. util. *" %>
<% @ Page language = "java" pageEncoding = "Big5" %>
<%! String au_id, au_lname, au_fname, phone, address, city, state; %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> ArticleList </TITLE>
<Link rel = "stylesheet" href = "style.css">
</Head>
<Body bgcolor = "# FFFFFF"> <table width = "760" border = "0" cellspacing = "0" cellpadding = "0" align = "center">
<Tr>
<Td>
<Table width = "750" border = "0" cellspacing = "2" cellpadding = "2" align = "center">
<Tr align = "center">
<Td> & nbsp; </td>
</Tr>
<Tr>
<Td>
<Table width = "740" border = "0" cellspacing = "1" cellpadding = "1" align = "center">
<Tr align = "center" bgcolor = "# CCCCCC">
<Td> <B> title </B> </td>
<Td> <B> author </B> </td>
<Td> <B> type </B> </td>
<Td> <B> click </B> </td>
<Td> <B> join time </B> </td>
</Tr>
<%
Int I;
Int ipage = 5; // page unit
Int allpage; // the total number of pages.
Int pages; // The accepted page number variable.
Int cpage = 1; // current page
Int spage; // start page
Int allCol = 0;
Vector v = null; // database connection Declaration
Connection conn = null;
Java. lang. String SQL, pagesql;
Java. SQL. ResultSet rs, pagers;
Statement statement = null;
String kmsdb = "jdbc: microsoft: sqlserver: // 127.0.0.1: 1433; DatabaseName = DBName ";
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
Conn = DriverManager. getConnection (kmsdb, "user", "pwd ");
 
Pagesql = "select count (*) FROM Authors"; // retrieve the total number of articles
Statement = conn. createStatement ();
Pagers = statement.exe cuteQuery (pagesql );
While (pagers. next ())
{
AllCol = pagers. getInt (1 );
System. out. println (allCol );}
 
// Obtain the total number of pages
Allpage = (int) Math. ceil (allCol + ipage-1)/ipage );
// Determine whether the parameter pages is null
If (request. getParameter ("pages") = null ){
Pages = 1;
} Else {
Pages = new Integer (request. getParameter ("pages"). intValue ();
System. out. println (pages );
}
// Determine the current page
If (pages> allpage | pages = 0)
{Cpage = 1 ;}
Else
{
Cpage = pages;
}
// Determine the start page
SQL = "select Top" + ipage + "au_id, au_lname, au_fname, address, phone from authors where au_id not in (select top" + (cpage-1) * ipage + "au_id from authors order by au_id desc) order by au_id desc ";
Rs = statement.exe cuteQuery (SQL );
While (rs. next ())
{
Au_id = rs. getString ("au_id ");
Au_lname = rs. getString ("au_lname ");
Au_fname = rs. getString ("au_fname ");
Phone = rs. getString ("phone ");
Address = rs. getString ("address"); %>
<Td width = "300"> <% = au_id %> </a> </td>
<Td width = "300"> <% = au_lname %> </a> </td>
<Td width = "300"> <% = au_fname %> </a> </td>
<Td width = "300"> <% = phone %> </a> </td>
<Td width = "300"> <% = address %> </a> </td>
</Tr>
<%
} %>
<Tr bgcolor = "# CCCCCC">
<Td colspan = "5" align = "right" valign = "middle"> total <% = allCol %> records <% = allpage %> page <% if (cpage> 1) {%> <a href = "fy. jsp? Pages = <% = cpage-1 %> "> </a> <%} if (cpage <allpage) {%> <a href = "fy. jsp? Pages = <% = cpage + 1%> </a> <% }%>/<% for (I = 1; I <= allpage; I ++) {if (I! = Pages) {%> <a href = "fy. jsp? Pages = <% = I %> "> <% = I %> </a> & nbsp; <% }%> </td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
</Body>
</Html>

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.