JSP data paging instance: MySQL Paging

Source: Internet
Author: User

JSP database operations enable pagination of JSP data. This article provides an example of MySQL paging.

I. Preparations before running

Download a jar file of the mysql jdbc driver) and load it in CLASSPATH.

Create a MySQL database test

The database has a table: note with the field name (varchar)

Ii. Download and install

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%> 
  2. <% Java. SQL. Connection sqlCon; // database Connection object
  3. Java. SQL. Statement sqlStmt; // SQL Statement object
  4. Java. SQL. ResultSet sqlRst; // result set object
  5. Java. lang. String strCon; // database connection String
  6. Java. lang. String strSQL; // SQL statement
  7. Int intPageSize; // number of records displayed on one page
  8. Int intRowCount; // The total number of records.
  9. Int intPageCount; // the total number of pages.
  10. Int intPage; // the page number to be displayed.
  11. Java. lang. String strPage;
  12. Int I;
  13. // Set the number of records displayed on one page
  14. IntPageSize=2;
  15. // Obtain the page number to be displayed
  16. StrPage=Request. GetParameter ("page ");
  17. If (StrPage= Null ){
  18. // Indicates that the QueryString parameter does not contain the page parameter. The first page of data is displayed.
  19. IntPage=1;
  20. } Else {
  21. // Converts a string to an integer.
  22. IntPage=Java. Lang. Integer. parseInt (strPage );
  23. If (intPage< 1)IntPage=1;
  24. }
  25. // Load the JDBC driver
  26. Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
  27. // Connect to the database
  28. SqlCon=Java. SQL. DriverManager. getConnection ("jdbc: mysql: // localhost/test ");
  29. // Create a statement object
  30. SqlStmt=SqlCon. CreateStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENSITIVE, java. SQL.
  31. ResultSet. CONCUR_READ_ONLY); // execute the SQL statement
  32. StrSQL="Select name from note";
  33. // Execute the SQL statement and obtain the result set
  34. SqlRst=SqlStmt. ExecuteQuery (strSQL );
  35. // Obtain the total number of records
  36. SqlRst. last ();
  37. IntRowCount=SqlRst. GetRow ();
  38. // Calculate the total number of pages
  39. IntPageCount= (IntRowCount + intPageSize-1)/intPageSize;
  40. // Adjust the page number to be displayed
  41. If (intPage>IntPageCount)IntPage=IntPageCount;
  42. %> 
  43. < Html> 
  44. < Head> 
  45. < Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312"> 
  46. < Title>JSP database operation routine-JSP data pagination-JDBC 2.0-mysql</Title> 
  47. </Head> 
  48. < Body> 
  49. < Table Border="1" Cellspacing="0" Cellpadding="0"> 
  50. < Tr> 
  51. < Th>Name</Th> 
  52. </Tr> 
  53. <% If (intPageCount>0)
  54. {
  55. // Locate the record pointer to the first record on the page to be displayed.
  56. SqlRst. absolute (intPage-1) * intPageSize + 1 );
  57. // Display data
  58. I=0;
  59. While (I< IntPageSize&&! SqlRst. isAfterLast () {%> 
  60. < Tr> 
  61. < Td> 
  62. <% = SqlRst. getString (1) %> 
  63. </Td> 
  64. </Tr> 
  65. <% SqlRst. next ();
  66. I ++;
  67. }
  68. }
  69. %> 
  70. </Table> 
  71. The<% = IntPage %>Total pages<% = IntPageCount %>Page
  72. <% If (intPage< IntPageCount) {%>< A Href="Mysqlpage. jsp? Page = <% = intPage + 1%>">Next Page</><%} %> 
  73. <% If (intPage>1) {%>< A Href="Mysqlpage. jsp? Page = <% = intPage-1 %>">Previous Page</><%} %> 
  74. </Body> 
  75. </Html> 
  76. <%
  77. // Close the result set
  78. SqlRst. close ();
  79. // Close the SQL statement object
  80. SqlStmt. close ();
  81. // Close the database
  82. SqlCon. close ();
  83. %> 

How to run JSP data paging?

Save the code as a file test. jsp

Under Orion Application Server:

Copy to the default-web-app directory of orion, and run the following command:

 
 
  1. http://localhost:port/test.jsp 

Access Test

You can run Resin, Tomcat, JWS, and so on. This section describes the implementation of JSP data paging.

  1. JSP Database Operations routine JDBC-ODBC)
  2. Basic preparation for learning JSP: JDK installation and configuration
  3. Java Servlet Introduction: Perfect Combination with JSP
  4. Integrate JSP and PHP in Apache
  5. Java Servlets (JSP) Development Environment

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.