Java simple hierarchical implementation, java hierarchical

Source: Internet
Author: User

Java simple hierarchical implementation, java hierarchical

1. Paging implementation

By page, all query results are stored in the session object or set. When pages are turned over, the data displayed on a page is retrieved from the session object or set. However, this method has two major drawbacks: first, users may see expired data; second, if the data volume is large, it takes a long time to query a data set, in addition, the stored data also occupies a large amount of memory, and the effect is significantly reduced.

2. Steps to display data by PAGE

(1) determine the number of data displayed on each page. Based on the design of the page, determine how many records are displayed in the data list each time, that is, the number of records to be queried from the database each time is used for page display, generally, this quantity can be defined during development or determined by the user.

(2) calculate the total number of displayed pages. To display the data by page, you must calculate the total number of pages to be divided based on the number of database records displayed on each page. Because the number of records displayed on the page is fixed and the total number of records stored in the database is unknown, you need to take the following steps to get the total number of pages:

<1> first, you need to query the total number of records in the database. In the SQL Server database, the count () aggregation function is provided () the aggregate function can obtain the total number of records in the database. The Code is as follows:

The code for defining the news interface is as follows.

Public interface NewsDao {

Public int getTotalCount ();

}

The code for getting the total number of records in the database in the News interface implementation class is as follows.

Public class NewsDaoIple extends BaseDao implements NewsDao {

Public int getTotalCount (){

Int count = 0;

String SQL = "select count (*) fro News ";

...... // Omitting the Execution Code

If (rs. next ()){

Count = rs. getInt (1 );

}

... // Omitting the Execution Code

Return count

}

}

From the code, we can see that when you execute an SQL statement using the count () function, you will get the total number of records in the news table and then return the data.

<2> with the total number of database records, you can calculate the total number of pages to be divided based on the number of records displayed on each page and encapsulate the paging data into the page class. The Code is as follows:

Public class Page {

// Total number of pages

Private int totalPageCount = 1;

// Page size, that is, the number of records displayed per page

Private int pageSize = 0;

// Total number of records

Private int totalCount = 0;

// Current page number

Private int currPageNo = 1;

// News set per page

List <News> newsList;

...... // Omit other getter/setter () Methods

Public int getCurrPageNo (){

If (totalPageCount = 0)

Return 0;

Return currPageNo;

}

Public void setCurrPageNo (int currPageNo ){

If (this. currPageNo> 0)

This. currPageNo = currPageNo;

}

Public int getPageSize (){

Return pageSize;

}

Public void setPageSize (int pageSize ){

If (pageSize> 0)

This. pageSize = pageSize;

}

Public int getTotalCount (){

Return totalCount;

}

Public void setTotalCount (int totalCount ){

If (totalCount> 0 ){

This. totalCount = totalCount;

// Calculate the total number of pages

TotalPageCount = this. totalCount % pageSize = 0? (This. totalCount/pageSize): this. totalCount/pageSize + 1;

}

}

}

The above Code sets the setTotalCount () method of the total number of records. Based on the total number of records and the number of records displayed on each page, the formula is used to calculate the total number of pages. It uses the conditional ternary operator "? . If the total number of records can be divisible by the number of records displayed on each page, the total number of pages is the quotient of the two. If the total number of records cannot be divisible, the remaining number of records is separately listed as one page, therefore, the total number of pages is a combination of the two vendors.

(3) Write SQL statements.

1. If three records are displayed on each page, to display the records on the first page:

String SQL = "select top 3 NID, NTITLE, NCREATEDATE from News" + "where NID not in (select top 0 NID from News )";

In the preceding statement, top is used to limit the number of returned rows. This statement uses a two-layer nested query method. The select statement in the inner layer is a common query statement that returns the restricted rows, the execution result is actually a restricted range for the select statement of the outer layer. The number 0 is the subscript of the starting row. If the first page is displayed, the query starts from the first row, that is, the starting subscript is 0. the number of rows restricted by the select statement on the outer layer is actually the number of records to be displayed on each page, the execution result is to retrieve the first three records (the number of records per page) from the query results of the inner statement according to the subscript of the starting row ).

Subscript = (current page number-1) * data volume per page

 

2 String SQL = "select top" + pageSize + "NID, NTITLE, NCREATEDATE from News" + "where NID not in (select top" + (currPageNo-1) * pageSize + "NID from News )";

PageSize indicates the number of records displayed on each page, while the currPageNo variable indicates the code on the current page.

The code for defining the news interface is as follows:

Public interface NewsDao {

Public int getTotalCount ();

Public List <News> getPageNewsList (int pageNo, int pageSize );

}

The code for getting a news set on each page in the News interface implementation class is as follows:

Public class NewsDaoImple extends BaseDao implements NewsDao {

... // Omitting the code for querying the total number of records in the database

Public List <News> getPageNewsList (int pageNo, int pageSize ){

List <News> newslist = new ArrayList <News> ();

Connection con = this. getConnection ();

PreparedStatement ps = null;

ResultSet rs = null;

String SQL = "select top" + pageSize + "NID, NTITLE, NCREATEDATE from News where NID not in" + "(select top" + (pageNo-1) * pageSize + "NID from News" + "order by NCREATEDATE desc) order by NCREATEDATE desc ";

Other Execution Code is omitted.

Return newslist;

}

}

Display Information to the page:

Public class NewsDAoImple extends BaseDao implements NewsDao {

...... // Omit other code implementation

Public static void main (String [] args ){

NewsDaoImple news = new NewsDaoImple ();

Int totalCount = news. getTotalCount ();

Page page = new Page ();

Page. setCurrPageNo (1); // you can specify the current page.

Page. setPageSize (3); // you can specify the number of entries per page.

Page. setTotalCount (totalCount); // you can specify the total number.

System. out. println ("Total number of news:" + page. getTotalCount ());

System. out. println ("the number of entries per page is:" + page. setPageSize (););

System. out. println ("total page number:" page. getTotalPageCount ());

System. out. println ("current is the" + page. getCurrPageNo () "page ");

List <News> newslist = news. getPageNewsList (page. getCurrPageNo (), page. getPageSize ());

Page. setListNews (newslist );

For (News news: page. getListNews ()){

System. out. println (news. getNid () + "\ t" + news. getNtitle () + "\ t" + news. getNcreatedate ());

}

}

}

 

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.