Pagination of jsp Data Reading database data

Source: Internet
Author: User

[Html]
<% @ Page language = "java" pageEncoding = "gb2312" %>
<% @ Page contentType = "text/html; charset = gb2312" %>
<% Request. setCharacterEncoding ("gb2312"); %>
<% @ Page import = "java. util. *, java. lang. *, java. SQL. *, javax. naming. *, javax. SQL. *" %>
<% @ Page errorPage = "error.html" %>
<Html> www.2cto.com
<Head> <meta http-equiv = Content-Type content = "text/html; charset = gb2312"> <Body>
<% Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Int pageSize, rowCount, pageCount, curPage;
PageSize = 5;
PageCount = 1;
String strPage = request. getParameter ("page ");
If (strPage = null) curPage = 1;
Else {
CurPage = Integer. parseInt (strPage); // set the current page by Parameters
If (curPage <1) curPage = 1;
}
%>
<% Try {
InitialContext ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/MySqlDS ");
Con = ds. getConnection ();
Stmt = con. createStatement ();
Rs1_stmt.exe cuteQuery ("select * from infos ");
Rs. last ();
RowCount = rs. getRow ();
PageCount = (rowCount + pageSize-1)/pageSize;
If (curPage> pageCount) curPage = pageCount;
} Catch (SQLException e ){
System. out. println (e. toString ());
}
%>
<Table width = "100%"> <tr> <td>
Page: <% = curPage %>/<% = pageCount %> page <% = pageSize %> items/page </td> </tr>
</Table>
<Table width = "80%"> <tr>
<Th> Number </th> <th> Name </th> <th> Activity </th> <th> Score </th> </tr>
<% If (pageCount> 0) rs. absolute (curPage-1) * pageSize + 1 );
Int I = 0;
While (I <pageSize &&! Rs. isAfterLast () {%>
<Tr align = "center"> <td> <% = rs. getString (1) %> </td> <% = rs. getString (2) %> </td>
<Td> <% = rs. getString (3) %> </td> <% = rs. getString (4) %> </td> </tr>
<%
Rs. next ();
I ++;
} %>
</Table>
<% If (curPage> 1) {%>
<A href = "MyJsp. jsp? Page = <% = curPage-1 %> "> previous page </a> <%} %>
<% If (curPage <pageCount) {%>
<A href = "MyJsp. jsp? Page = <% = curPage + 1%> "> next page </a> <%} %>
</Body> <% Rs. close ();
Stmt. close ();
Con. close ();
%>

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.