Pagination implemented by JSP program

Source: Internet
Author: User

Obtain the data in the database through the jsp (preferred for SUN enterprise-level applications) program, and then control the display mode through the program. This obvious disadvantage is that if the data volume is large, there will be significant performance degradation.

   Now I will present the test code, although I try to limit the length of the Code

   Let's talk about the development steps first. The DB script is used to test whether the obtained data is correctly displayed in a table and add the functions of displaying those that are not supposed to be displayed, add the current location tag, for example, 4/5. Add the function to jump directly to a page to handle some bugs ..

1. First, write the database test script:

-- Delete a table
Drop table person;

-- Create a table
Create table person (
 Id int auto_increment not null Prima (the most perfect VM management system) ry key,
 UidVarchar (32 ),
 Name varchar (32 ),
 Password varchar (20)
);
-- Insert data? Chinese characters are not supported. projection must be specified in the table. More than 30 records are inserted for testing pages.
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
Insert into person (uid, name, password) values (uid1, testname, 123456 );
-- Transaction commit

2. The entire program section.

<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Head>
   <Title> jsp (preferred for SUN Enterprise Applications) controls pages through programs </title>
   <Script type = "text/javascript">
           Function openPage (currentpage ){
               Document. getElementById ("cp"). value = currentpage;
               Document. getElementById ("frm"). submit ();

           }
           Function changePage (){
           Document. getElementById ("cp"). value = document. getElementById ("selNumber"). value;
           Document. getElementById ("frm"). submit ();
           }
   </Script>
</Head>
<Body>
     
     <%
       Int pageSize = 10;
       Int currentPage = 1;

       Int allRecorder = 0;
       Int pageNumber;

       Try {
           CurrentPage = Integer. parseInt (request. getParameter ("cp "));
       } Catch (Exception ex ){
           
       }
       // The processing of database connection is specified based on your own database.
       Final String DBDriver = "org. gjt. mm. MySQL (the best combination with PHP). Driver ";
  Final String DbUrl = "jdbc: MySQL (the best combination with PHP): // localhost/test ";
  Final String DBUser = "root ";
  Final String DBPSW = "root ";
  Connection conn = null;
  Try {
   Class. forName (DBDriver );
   Conn = DriverManager. getConnection (DbUrl, DBUser, DBPSW );
           String SQL = "select count (id) from person ";
           PreparedStatement pst = conn. prepareStatement (SQL );
           ResultSet rs = pst.exe cuteQuery ();
           If (rs. next ()){
                 AllRecorder = rs. getInt (1 );
           }
           PageNumber = (allRecorder + pageSize-1)/pageSize; // get the last page
           Rs. close ();
           Pst. close ();

           SQL = "select id, uid, name, password from person ";
           Pst = conn. prepareStatement (SQL );
           Rs = pst.exe cuteQuery ();
     %>
     <Form action = "test. jsp (preferred for SUN Enterprise Applications)" method = "get" id = "frm">
         <Input type = "button" value = "Homepage" Onclick = "openPage (1);" <% = currentPage = 1? "Disabled": "" %>/>
        

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.