Paging (displaying information in a database to a Web page)

Source: Internet
Author: User

Problem Analysis:

 When we want to retrieve information from a database and display it on a Web page, if there is too much information in the database. On the one hand, the database overhead is very large, performance is reduced, on the other hand, displaying too much data on a single page can degrade the user experience.

Workaround:

1. Since the information displayed on each page after pagination is no longer just data aerial data, it is similar to the following:

So we need to create a page class:

 PackageCom.neuedu.manage.bean; Importjava.util.List;  Public classPage <T>{PrivateList<t> data;//data queried by the database          Private intTotalrecord;//Total record count, database query              Private intPageNumber;//Current page number          Private intPageSize;//number of records per page              PrivateString path;//the address of the JSP page link to jump to//private int index; //current index, calculated by//private int totalpage;//The total number of pages are calculated   
}

Where we create get and set methods for the first 5 variables, and because the last two variables are computed, only the Get method is created for them:

 Public intGetIndex () {/*** Current index value, calculated by*/              return(Getpagenumber ()-1) *pageSize; } Public intGettotalpage () {/*** Total Pages*/              if(totalrecord%pagesize==0) {returntotalrecord/pageSize; }return(Totalrecord/pagesize +1); }

  2. DAO Layer Code: Queries the result based on the current index value and the number of records per page, and then encapsulates it into a user object and saves it to the list of the student class, returning it.

     PublicList<student> Getlimitstulist (intIndexintpageSize) {Jdbcutil Jdbcutil=NewJdbcutil (); Connection Con=jdbcutil.getconnection (); PreparedStatement PST=NULL; ResultSet RSet=NULL; List<student>list=NewArraylist<student>(); String SQL= "SELECT * FROM students limit?,?"; Try{PST=con.preparestatement (SQL); Pst.setint (1, index); Pst.setint (2, pageSize); RSet=Pst.executequery ();  while(Rset.next ()) {intId=rset.getint ("id"); String name=rset.getstring ("name"); String School=rset.getstring ("School"); DoubleScore=rset.getdouble ("Score"); List.add (NewStudent (ID, name, school, score)); }            } Catch(SQLException e) {e.printstacktrace (); } finally{jdbcutil.close (con, PST, rSet);        } System.out.println (list); returnlist; }

Not to be continued ....

Paging (displaying information in a database to a Web page)

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.