Pagination of JSP Database Access

Source: Internet
Author: User

<% @ Page C Import = "Java. SQL. * "%> <br/> <% <br/> // variable declaration <br/> connection sqlcon = NULL; // database connection object <br/> statement sqlstmt; // SQL statement object <br/> resultset sqlrst; // result set object <br/> string strsql; // SQL statement </P> <p> int intpagesize; // The number of records displayed on one page <br/> int introwcount; // The total number of records <br/> int intpagecount; // the total number of pages <br/> int intpage; // page number to be displayed <br/> string strpage; <br/> int I; <br/> // set the number of records displayed on one page <br/> intpagesize = 8; <br/> // Obtain the page number to be displayed <br/> strpage = request. getparameter ("page"); <br/> If (strpage = NULL) {// indicates that the page parameter is not found in querystring, the first page of Data <br/> intpage = 1; <br/>}< br/> else {// converts the string to an integer <br/> intpage = integer. parseint (strpage); <br/> If (intpage <1) intpage = 1; <br/>}< br/> try <br/>{< br/> class. forname ("com. microsoft. JDBC. sqlserver. sqlserverdriver "); <br/> sqlcon = drivermanager. getconnection ("JDBC: Microsoft: sqlse Rver: // 127.0.0.1: 1433; databasename = jspdev "," sa "," "); <br/>}< br/> catch (exception E) <br/>{< br/> E. printstacktrace (); <br/>}< br/> // create a rolling read-only SQL statement object <br/> sqlstmt = sqlcon. createstatement (Java. SQL. resultset. type_scroll_insensitive, Java. SQL. resultset. concur_read_only); <br/> // prepare an SQL statement <br/> strsql = "select ID, name from personinfo order by ID "; </P> <p> // execute an SQL statement and obtain the result set. <br/> sqlrst = sqlstm T.exe cutequery (strsql); <br/> // obtain the total number of records <br/> sqlrst. last (); <br/> introwcount = sqlrst. getrow (); <br/> // count the total number of pages <br/> intpagecount = (introwcount + intPageSize-1)/intpagesize; <br/> // adjust the page number to be displayed <br/> If (intpage> intpagecount) intpage = intpagecount; <br/> %> </P> <p> <HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" C> <br/> <title> JSP database operation routine-data paging-jdbc 2.0-sqlserver2000 </title> <br/> </p EAD> </P> <p> <body> </P> <p> <Table border = "1" cellspacing = "0" cellpadding = "0"> <br/> <tr> <br/> <TH> id </Th> <br/> <TH> name </Th> <br/> </tr> <br/> <% <br/> If (intpagecount> 0) {<br/> // locate the record pointer to the first record on the page to be displayed <br/> sqlrst. absolute (intPage-1) * intpagesize + 1); <br/> // display data <br/> I = 0; <br/> while (I <intpagesize &&! Sqlrst. isafterlast () {<br/>%> <br/> <tr> <br/> <TD> <% = sqlrst. getstring (1) %> </TD> <br/> <TD> <% = sqlrst. getstring (2) %> </TD> <br/> </tr> <br/> <% <br/> sqlrst. next (); <br/> I ++; <br/>}< br/>%> </P> <p> </table> <br/> rows on each page <% = intpagesize %> <br/> page <% = intpage %> total <% = intpagecount %> <br/> <% IF (intpage = 1) {out. print ("Homepage"); %> <br/> <% Out. print ("Previous Page");} else {%> <br/> <a href = "storetest. JSP? Page = 1 "mce_href =" storetest. jsp? Page = 1 "> homepage </a> <br/> <a href =" storetest. jsp? Page = <% = intPage-1 %> "> previous page </a> <br/> <% }%> <br/> <% IF (intpage = intpagecount) {out. print ("next page"); %> <br/> <% Out. print ("last page");} else {%> <br/> <a href = "storetest. JSP? Page = <% = intpage + 1%> "> next page </a> <br/> <a href =" storetest. jsp? Page = <% = intpagecount %> "> last page </a> <br/> <% }%> <br/> to the <select name =" jumppage "onchange = 'window. location = "http: // localhost: 8080/chj/storetest. JSP? Page = "+ this. value '> <br/> <% <br/> for (Int J = 1; j <= intpagecount; j ++) <br/>{< br/> If (j = intpage) {<br/> %> <br/> <option selected value = <% = J %> </option> <br/> <%} else {%> <br/> <option value = <% = J %> </option> <br/> <% }%> <br/> </SELECT> page <br/> </body> <br/> </ptml> </P> <p> <% <br/> // close result set <br/> sqlrst. close (); </P> <p> // close the SQL statement object <br/> sqlstmt. close (); </P> <p> // close the database <br/> sqlcon. close (); <br/>%> 

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.