Java web development paging function mysql database

Source: Internet
Author: User

List. jsp:

<Div class = "right_content">
<H2 class = "bulletin_h2"> system announcement <Div class = "bulletin_content">
<Ul>
<C: forEach items = "$ {notices}" var = "notice">
<Li> <em >>></em> <a href = "getnecontent? Id =$ {notice. id} ">$ {notice. title} </a>
<Span> Delete </span>
<Span> edit </span>
<Span >$ {notice. pubtime} </span>
</Li>
</C: forEach>
</Ul>
<C: if test = "$ {currentPage! = 1} ">
<A href = "noticelist? Page =$ {currentPage-1} "> Previous </a>
</C: if>
<C: forEach begin = "1" end = "$ {noOfPages}" var = "I">
<C: choose>
<C: when test = "$ {currentPage eq I}">
<Td >$ {I} </td>
</C: when>
<C: otherwise>
<Td> <a href = "noticelist? Page =$ {I} ">$ {I} </a>
</Td>
</C: otherwise>
</C: choose>
</C: forEach>
<C: if test = "$ {currentPage lt noOfPages}">
<Td> <a href = "noticelist? Page =$ {currentPage + 1} "> Next </a>
</Td>
</C: if>
</Div>
</Div>

List. java:

Public ArrayList <Notice> getNotices (int offset, int recordsPerPage ){
ResultSet rs = null;
PreparedStatement ps = null;
ArrayList <Notice> notices = new ArrayList <Notice> ();
Connection conn = JdbcUtils. getConnection ();
String SQL = "select SQL _CALC_FOUND_ROWS id, title, content, person, filepath, pubtime from notice order by id desc limit ?,? ";
Try {
Ps = conn. prepareStatement (SQL );
Ps. setInt (1, offset );
Ps. setInt (2, recordsPerPage );
Rs = ps.exe cuteQuery ();
While (rs. next ()){
Notice notice = new Notice ();
Notice. setId (rs. getInt ("id "));
Notice. setTitle (rs. getString ("title "));
Notice. setContent (rs. getString ("content "));
Notice. setPerson (rs. getString ("person "));
Notice. setFilepath (rs. getString ("filepath "));
Notice. setPubtime (rs. getString ("pubtime "));
Notices. add (notice );
}
Rs. close ();
Rs = ps.exe cuteQuery ("SELECT FOUND_ROWS ()");
If (rs. next ())
This. noOfRecords = rs. getInt (1 );
} Catch (Exception e ){
E. printStackTrace ();
}
Return notices;
}

Servlet:

// Handle garbled characters
Request. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = gbk ");

Int page = 1;
Int recordsPerPage = 10;
If (request. getParameter ("page ")! = Null)
Page = Integer. parseInt (request. getParameter ("page "));

NoticeDAO nd = new NoticeDAOImpl ();
ArrayList <Notice> notices = nd. getNotices (page-1) * recordsPerPage, recordsPerPage );
Int noOfRecords = nd. getNoOfRecords ();
Int noOfPages = (int) Math. ceil (noOfRecords * 1.0/recordsPerPage );

Request. setAttribute ("notices", notices );
Request. setAttribute ("noOfPages", noOfPages );
Request. setAttribute ("currentPage", page );
Request. getRequestDispatcher ("noticelist. jsp"). forward (request, response );

References: http://www.cnblogs.com/super119/archive/2011/01/13/1934997.html

QQ group: 127402101. Modify the remarks when you enter the group.

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.