Implementing pagination in a JSP

Source: Internet
Author: User
Tags count db2 getmessage query stmt
js| Paging
Page is a keyword and cannot be a variable.


conn.jsp


<%
String sdbdriver = \ "Com.ibm.db2.jdbc.app.db2driver\";
String sConnStr = \ "Jdbc:db2:faq\";
Connection conn = null;
Statement stmt = null;
ResultSet Rs=null;
try {
Class.forName (Sdbdriver);
}
catch (Java.lang.ClassNotFoundException e) {
Out.print (\ "FAQ (): \" + e.getmessage ());
}

try{
conn = Drivermanager.getconnection (sconnstr,\ "wsdemo\", \ "wsdemo1\");
stmt = Conn.createstatement ();
}catch (SQLException e) {
Out.print (E.tostring ());
}
%>

query.jsp

<%@ page language=\ "java\" import=\ "java.sql.*\"%>
<%@ page contenttype=\ "text/html; Charset=gb2312\ "%>
<%@ include file=\ "Conn.jsp\"%>
<%
......
int pages=0;
int pagesize=10;
ResultSet result = null;
ResultSet rcount = null;

pages = new Integer (Request.getparameter (\ "Pages\")). Intvalue ();

if (pages>0)
{

String sql=\ "State=\ ' I am not silly \";
int count=0;
try {
Rcount = Stmt.executequery (\ "SELECT COUNT (ID) as ID from user where \" +sql);
catch (SQLException ex) {
Out.print (\ "Aq.executequery: \" + ex.getmessage ());
}
if (Rcount.next ())
Count = Rcount.getint (\ "id\");
Rcount.close ();
if (count>0)
{
Sql=\ "SELECT * from user where \" +SQL;
try {
result = Stmt.executequery (SQL);
}
catch (SQLException ex) {
Out.print (\ "Aq.executequery: \" + ex.getmessage ());
}
int i;
String name;
Result.first ();
Result.absolute ((pages-1) *pagesize);
This method jdbc2.0 support. The compiler passed, but did not know if it was related to the driver, so I had to use the following stupid method.
For (i=1;i<= (pages-1) *pagesize;i++)
Result.next ();
for (i=1;i<=pagesize;i++) {
if (Result.next ()) {
Name=result.getstring (\ "name\");
Out.print (name);
}
Result.close ();
int n= (int) (count/pagesize);
if (N*pagesize<count) n++;
if (n>1)
{for (i=1;i<=n;i++)
Out.print (\ "<a href=query.jsp?pages=\" +i+\ ">\" +i+\ "</a>\");

}

}

}

%>


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.