jsp实现读取数据库数据分页显示
程序员文章站
2022-04-15 13:29:36
[html]
<%@ page language="java" pageencoding="gb2312" %>
<...
[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"></head>
<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);//通过参数来定当前页面
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();
rs=stmt.executequery("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>
页次:<%=curpage %>/<%=pagecount %>页 <%=pagesize %>条/页</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><td><%=rs.getstring(2)%></td>
<td><%=rs.getstring(3)%></td><td><%=rs.getstring(4)%></td></tr>
<%
rs.next();
i++;
} %>
</table>
<%if(curpage>1){ %>
<a href="myjsp.?page=<%=curpage-1%>">上一页</a><%} %>
<%if(curpage<pagecount){ %>
<a href="myjsp.jsp?page=<%=curpage+1%>">下一页</a><%} %>
</body></html>
<%rs.close();
stmt.close();
con.close();
%>
<%@ 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"></head>
<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);//通过参数来定当前页面
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();
rs=stmt.executequery("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>
页次:<%=curpage %>/<%=pagecount %>页 <%=pagesize %>条/页</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><td><%=rs.getstring(2)%></td>
<td><%=rs.getstring(3)%></td><td><%=rs.getstring(4)%></td></tr>
<%
rs.next();
i++;
} %>
</table>
<%if(curpage>1){ %>
<a href="myjsp.?page=<%=curpage-1%>">上一页</a><%} %>
<%if(curpage<pagecount){ %>
<a href="myjsp.jsp?page=<%=curpage+1%>">下一页</a><%} %>
</body></html>
<%rs.close();
stmt.close();
con.close();
%>
上一篇: PS通道+滤镜制作漂亮的放射光线
下一篇: 数据库寻找一个string所在的表