Currently, the best JSP paging technology

Source: Internet
Author: User
Preface
In the process of using the database, it is inevitable to use the paging function, but the JDBC specification does not solve this well. Many of my friends have their own solutions for this demand. For example, we use a collection class such as Vector to save the retrieved data first and then pagination. However, this method has poor availability, which is completely different from the JDBC interface and does not support different types of fields. A solution with good compatibility with JDBC is provided here.
JDBC and paging
Sun's JDBC specification is sometimes dumbfounded. In JDBC1.0, you can only perform the next () operation on a result set instead of rolling it backwards, this directly leads to the failure to obtain the size of the result set when only one SQL query is executed. Therefore, if you are using the JDBC1.0 driver, paging is almost impossible.
Fortunately, Sun's JDBC2 specification makes up for this deficiency and adds the frontend and backend scrolling operations of the result set. Although it still cannot directly support paging, however, you can write your own ResultSet that supports pagination on this basis.
Implementation methods related to specific databases
Some databases, such as Mysql and Oracle, have their own paging methods. For example, Mysql can use the limit clause, and Oracle can use ROWNUM to limit the size and start position of the result set. Take Mysql as an example. The typical code is as follows:
// Calculate the total number of records
String SQL = "SELECT Count (*) AS total" + this. QueryPart;
Rs = db.exe cuteQuery (SQL );
If (rs. next ())
Total = rs. getInt (1 );
// Set the current page number and total page number
TPages = (int) Math. ceil (double) this. Total/this. MaxLine );
CPages = (int) Math. floor (double) Offset/this. MaxLine + 1 );
// Retrieve the required records based on conditions
If (Total> 0 ){
SQL = Query + "LIMIT" + Offset + "," + MaxLine;
Rs = db.exe cuteQuery (SQL );
    }
Return rs;
  }
Without a doubt, this code will be beautiful when the database is Mysql, but as a general class (in fact, what I want to provide later is part of a general class library ), we need to adapt to different databases, and applications based on this class (database) may also use different databases. Therefore, we will not use this method.
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.