Querying by page in Mysql and Oracle databases

Source: Internet
Author: User

Mysql and Oracle Database paging Query Method 1: mysql database paging <% // defines the records displayed on each page int pageSize = 3; String strPageNo = request. getParameter ("pageNo"); // record the int pageNo of the current page; // if the input current page number is null, it stays on the first page if (strPageNo = null | strPageNo. equals ("") {pageNo = 1;} else {try // convert the passed string to a number {pageNo = Integer. parseInt (strPageNo. trim ();} catch (NumberFormatException e) {pageNo = 1;} // if the page number is smaller than or equal to 0, it stays on the first page if (pageNo <= 0) {pageNo = 1 ;}/// connect to the database Class. forName ("Com. mysql. jdbc. Driver"); Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost/bbs? User = root & password = mingming "); Statement stCount = conn. createStatement (); ResultSet rsCount = stCount.exe cuteQuery ("select count (*) from article where pid = 0"); // query the total number of root nodes rsCount. next (); int totalRecord = rsCount. getInt (1); // obtain all the subnodes and calculate the total number of records. // The first method is used to calculate the total number of pages. If an integer can be used, the number of pages is the quotient, otherwise, it is Shang + 1int totalPage = totalRecord % pageSize = 0? TotalRecord/pageSize: totalRecord/pageSize + 1; // The second method is used to calculate the total number of pages, add a pageSize to the total number of records, and then subtract the size of 1 page. pageSize, int totalRecords = rsCount. getInt (1); totalPages = (totalRecords + PAGE_SIZE-1)/PAGE_SIZE; // if the current page number is greater than the total page number, stop at the last page if (pageNo> totalPage) {pageNo = totalPage;} // calculate the start position of each page, note that the start position starts from 0; int startPos = (pageNo-1) * pageSize; Statement st = conn. createStatement (); // query by page. startPos is the starting position of each page. pageSize Yes this page displays the record size ResultSet rs = st.exe cuteQuery ("select * from article where pid = 0 order by pdate desc limit" + startPos + "," + pageSize ); %> different display methods on the page after pagination: Method 1: normal display in a table: <table border = "1"> <% while (rs. next () {%> <tr> <td> <% = rs. getString ("title") %> // only display the title of each record </td> </tr> <%} // close the stream rs. close (); st. close (); conn. close (); %> </table> homepage <% = 1%> & nbsp; total page <% = totalPage %> & nbsp; page <% = pageNo %> & nbsp; last page <% = totalPage %> <A href = "ShowArticleFlat. jsp? PageNo = <% = pageNo-1 %> "> previous page </a> & nbsp; <a href =" ShowArticleFlat. jsp? PageNo = <% = pageNo + 1%> "> next page </a> Method 2: available display: you can jump to <form name = "form1"> <select name = "pageNo" onchange = "document. form1.submit () "> <% for (int I = 1; I <= totalPage; I ++) {%> <option value = <% = I %> <% = pageNo = I? "Selected": "" % >>> page <% = I %> <% }%> </select> </form> method 3: You can directly search for the page: // submit to the current page <form name = "fom2" action = "ShowArticleFlat. jsp "> <input type = text size = 4 name =" pageNo "value = <% = pageNo %>/> <input type =" submit "value =" go "/> </form> Method 2: <body>

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.