初学jsp心得(2)
7。在jsp中实现分页。
page是关键字,不能当变量。
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.*" %> [来源www.iocblog.net]
<%@ 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='我不傻'";
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);
// 此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。
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>");
}
}
}
%>
[来源www.iocblog.net]
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。