Jsp paging technology and jsp paging technology

Source: Internet
Author: User

Jsp paging technology and jsp paging technology

In jsp development, paging technology is often used. In addition, pure paging takes up a lot of space, so it is necessary to use javabean.

Based on the actual code, this article shows how to use javabean in jsp to implement paging.

The directory structure is as follows:


The Java Bean code is as follows:

Package lytjb;
Import java. SQL. ResultSet;
Import javax. servlet. http. HttpServletRequest;
Public class fenye {
ResultSet CountTopicrs = null; // The Rs variable that initializes the total number of records
ResultSet Pagirs = null; // Rs variable during page Initialization
Public int intCountTopic = 0; // total number of topics
Public int intPageSize; // number of topics displayed on each page
Public int intPageCount; // total number of pages
Public int intPage = 1; // current page number
Public String nowPage; // int I;
Public String HttpFile; // initialize the intPage variable of the current page to obtain the current page accurately. // File in the current address bar
DB db; // defines an object of the Linkdb class.


Public fenye (){
IntPageSize = 40; // number of records displayed on each page
Db = new DB ();
}


// Countsql: the Query string of the total record. [Form: select count (*) from tablename]
// Pagisql: the Query string to be paged. [Form: select * from tablename where...]
// Request: the variable in the parameter passing process. [Used to control the pages variable during page turning]
Public ResultSet querySql (String Countsql, String Pagisql,
HttpServletRequest request) throws Exception {
HttpFile = request. getRequestURI (); // obtain the current file name.
NowPage = request. getParameter ("pages"); // obtain the current page and assign the value to the intPage variable. [The pages column must contain the pages parameter]
If (nowPage = null ){
IntPage = 1;
} Else {
IntPage = Integer. parseInt (nowPage );
If (intPage <1)
IntPage = 1;
}
CountTopicrs = db.exe cuteQuery (Countsql); // @ obtain the result set of the total number of records.
If (CountTopicrs. next ()){
IntCountTopic = CountTopicrs. getInt (1 );
}
IntPageCount = (intCountTopic + intPageSize-1)/intPageSize; // obtain the total number of pages.
If (intPage> intPageCount) // if the current page is greater than the total page number, the current page is equal to the total page number.
{
IntPage = intPageCount;
}
CountTopicrs. close (); // close the dataset of the total number of topics.
Db. close_all ();
Pagirs = db.exe cuteQuery (Pagisql); // @ obtain the result set of the execution pagination.
Return Pagirs;
} // End querySql function.


Public int getCountTopic () // obtain the total number of records.
{
Return intCountTopic;
}


Public int getPageCount () // obtain the total number of pages.
{
Return intPageCount;
}


Public int getIntPage () // obtain the current page number.
{
Return intPage;
}


Public String PageFooter (){
String str = "";
Int next, prev;
Prev = intPage-1;
Next = intPage + 1;
Str + = "query <font color = red>" + getCountTopic () + "</font> records"
+ "Total <font color = red>" + getPageCount () + "</font> page ";
Str + = "page <font color = red>" + getIntPage () + "</font> ";
If (intPage> 1)
Str + = "<A href =" + HttpFile + "? Pages = 1 "+"> homepage </A> ";
Else
Str + = "Homepage ";
If (intPage> 1)
Str + = "<A href =" + HttpFile + "? Pages = "+ prev +"> previous page </A> ";
Else
Str + = "Previous Page ";
If (intPage <intPageCount)
Str + = "<A href =" + HttpFile + "? Pages = "+ next +"> next page </A> ";
Else
Str + = "next page ";
If (intPageCount> 1 & intPage! = IntPageCount)
Str + = "<A href =" + HttpFile + "? Pages = "+ intPageCount
+ "> Last page </A> ";
Else
Str + = "last page ";
Return str;
}


Public void close_all (){
Db. close_all ();
}


}

DB files:

Package lytjb;
Import java. SQL .*;
// A tool class used to find data sources.
Public class DB {
Private Connection con = null;
Private Statement stmt = null;
ResultSet rs = null;


Public ResultSet executeQuery (String SQL) throws Exception {

Try {
Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver ");
Con = DriverManager. getConnection ("jdbc: sqlserver: // localhost: 1433; DatabaseName = SHB", "sa", "1234567 ");
// Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Con = DriverManager. getConnection ("jdbc: odbc: JDBCSQLDemo_JSPTest ");
Stmt = con. createStatement ();
Rs1_stmt.exe cuteQuery (SQL );
} Catch (Exception e ){}
Return rs;
}


// Execute the Insert and Update statements
Public void executeUpdate (String SQL) throws Exception {
Try {
Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver ");
Con = DriverManager. getConnection ("jdbc: sqlserver: // localhost: 1433; DatabaseName = SHB", "sa", "1234567 ");
// Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Con = DriverManager. getConnection ("jdbc: odbc: JDBCSQLDemo_JSPTest ");
Stmt = con. createStatement ();
Int rs1_stmt.exe cuteUpdate (SQL );

} Catch (Exception e ){}
}


// Close stmt and Connection
Public void close_all (){
Try {
Stmt. close ();
Con. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}


}

The jsp display code is as follows:

<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "GBK" %>
<% @ Page import = "lytjb. fenye" %>




<Html>
<Head>
<Link rel = "stylesheet" type = "text/css" href = "style.css">
</Head>

<Body bgcolor = "c0ffc0">
<Jsp: useBean id = "pagi" scope = "page" class = "lytjb. fenye"> </jsp: useBean>
 

<%
String college = "Politics and Law ";
String user = "";
User = (String) session. getAttribute ("username_session ");
If (user = null | user. equals ("")){
User = "Tourist ";
}
Else
;
// Out. println (user );
%>
<Table align = "center" border = '0'>
<Tr> <td> </td> </tr>
</Table>
<Table align = "center" border = '1' bordercolor = "c0ff00" bgcolor = "" rules = rows>
<Tr>
<Td> Hello <a href = "modi_person_info.jsp"> <% = user %> </a>, welcome! Please </td>
<Td> <table> <tr> <td> <input type = "button" value = "login" onclick = "Login configuration.href}'denglu.html '"> </td> </tr> </table> </td>
<Td> <table> <tr> <td> <input type = "button" onclick = "window. location. href = 'zhuceqian. jsp '"value =" register "/> </td> </tr> </table> </td>
<Td> <input type = "button" value = "exit" name = "logout" onclick = "window. location. href = 'tuichu. jsp '"/> </td>
<Td> <table> <tr> <td width = "360"> <input type = "button" value = "Administrator Logon" onclick = "too many location.href='admin_denglu.html '"> </td> </tr> </table> </td>
<Td>
<Table align = "right"> <tr> <td> <input type = "button" onclick = "window. location. href = 'shopcar. jsp '"value =" my shopping cart "id =" shopCar "/> </td> </tr> </table>
</Td>
</Tr>
</Table>
<Table width = "800" align = "center" border = 1 height = "40" bordercolor = "c0ff00" bgcolor = "ffffff">
<Tr> <td width = "200" align = "center"> <a href = "index. jsp "> homepage </a> </td> <td width =" 200 "align =" center "> <a href =" er_shou_shu.jsp "> second-hand books </a> </ td> <td width = "200" align = "center"> <a href = "liu_yan_ban.jsp"> message board </a> </td> <td width = "200" align = "center"> <a href = "lian_xi_wo_men.jsp"> Contact Us </a> </td> </tr>
</Table>

<P> </p>
<Table align = "center">
<Tr> <td align = "center"> <form name = "serach_college_book" action = "sousuoshuji. jsp? College = <% = college %> "method =" get ">
<Input type = "text" name = "college_book_search" id = "college_book_text"/>
& Nbsp; <input type = "submit" name = "college_book_search" id = "college_book_submit" value = "Search"/>
</Form> </td> </tr>
</Table>
<! -- Table starts displaying books -->

<Table align = "center">
<Tr>


<%
String CountQuery = "select count (*) from college_info where college = 'political & Legal '"; // query the total number of records
String SQL = "select book_picture, book_name, book_out_price, book_stock from college_info where college = ''"; // query the result set
ResultSet rs = pagi. querySql (CountQuery, SQL, request); // The request is a built-in jsp variable.
String footer = pagi. PageFooter (); // This sentence must be written before the while statement. Otherwise, the number of records obtained on the page is 0.
If (pagi. intPageCount> 0 ){
Int I = 0;
While (rs. next ()){
I ++;
If (I> (pagi. intPage-1) * pagi. intPageSize)
& (I <= pagi. intPage * pagi. intPageSize )){
String picture = rs. getString ("book_picture ");
String bookname = rs. getString ("book_name ");
String price = rs. getString ("book_out_price ");
String no = rs. getString ("book_stock ");
Int no_int = Integer. parseInt (no );
If (no_int <= 0)
No = "out of stock ";



%>
<Td> <table align = "center"> <tr> <td align = "center" width = "163"> . jpg "/> </td> </tr> <tr width =" 100 "> <td align =" center "width =" 100 "> <a href =" detail. jsp? Bookid = <% = picture %> "> <% = bookname %> </a> </td> </tr> <td align =" center "> price: <% = price %> </td> </tr> <td align = "center"> remaining amount: <% = no %> </td> </tr> </table> </td>

<%
I = I + 1;
If (I % 5 = 0 ){
Out. println ("</tr> <tr> ");
}
%>

<%
}
}
}
Out. println ("<table width = '000000' align =" + "right"
+ "> <Tr> <td colspan = 2>" + footer + "</td> </tr> </table> "); // The page turning display must be written before the database is closed
Rs. close ();
// Pagi. close_all ();
%>
</Tr>
</Table>

</Body>
</Html>

As follows:


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.