在线急等,求一段jsp+javabean的分页代码
最近在做作业.是一个新闻管理系统遇到了一个分页难题,
这是在javabean中定义的在页面上显示新闻标题的代码
public class UserDenglu {
public ResultSet viewmoreNews(Connection conn, HttpServletRequest request) {
ResultSet rs = null;
try {
Statement smt = conn.createStatement();
String sql = "select newsid,title,riqi from newstable where typeID=1 order by riqi desc ";
rs = smt.executeQuery(sql);
}
catch (Exception e) {
System.out.println(e.toString());
}
return rs;
}
}
请问如何在这个类里面写一段分页代码,然后在页面又怎样调用呢?麻烦高手帮忙解决,在下不胜感激,这是页面的详细代码:
<%@ page import= "java.sql.Connection "%>
<%@ page import= "com.gxjd.zhangl.ConnUtil "%>
<%@ page import= "com.gxjd.zhangl.UserDenglu "%>
<%@ page import= "java.sql.ResultSet "%>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2007-6-16
Time: 12:18:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType= "text/html;charset=gb2312 " language= "java " %>
<%request.setCharacterEncoding( "gb2312 ");
Connection conn= ConnUtil.getuser();
UserDenglu bean=new UserDenglu();
ResultSet rs=bean.viewmoreNews(conn,request);
%>
<html>
<head> <title> Simple jsp page </title> </head>
<body>
<form name= "form1 " method= "post " action= " ">
<table width= "700 " border= "1 " align= "center " bordercolor= " " bordercolorlight= "#FFFFFF " bordercolordark= "#FF0000 " bgcolor= " ">
<tr>
<td colspan= "3 "> 国内新闻: </td>
</tr>
<%while(rs.next()){%>
<tr>
<td colspan= "3 "> <div align= "left "> <a href= "detail.jsp?newsid= <%=rs.getString( "newsid ")%> " target= "_blank "> <%=rs.getString( "title ")%> </a> </div> </td>
</tr>
<%}%>
<tr>
<td width= "55 "> 上一页 </td>
<td width= "69 "> 下一页 </td>
<td width= "554 "> </td>
</tr>
</table>
</form>
</body>
</html>
没办法,刚学JSP.网上的代码又不会改,只能请教各位了,望各位帮忙解决;
[解决办法]
int pageSize = 5;
String pagenum = (String)request.getParameter( "pagenum ");//当前页数
if(pagenum==null || pagenum.equals( " ")) pagenum = "1 ";
int vectLen = vect.size();//vect是从数据库中找到的记录集
int cur = Integer.parseInt(pagenum);
int t=-1;
int s=-1;
int curMaxNum=0;
int sum = (vectLen-1)/pageSize+1;//页数
int tnum=vect.size();//总条数
if(cur < sum)
curMaxNum = cur*pageSize;
else if(cur == sum)
curMaxNum = vectLen;
Hashtable hash = new Hashtable();
for(int index = (cur-1)*pageSize;index < curMaxNum;index++)
{
hash = (Hashtable)vect.elementAt(index);
String bstrXSBH = (String)hash.get( "XSBH ");
String bstrXSXM = ds.toString((String)hash.get( "XSXM "));
String bstrBJBH = (String)hash.get( "BJBH ");
String bstrKSZT = (String)hash.get( "KSZT ");
String bstrSFCX = ds.toString((String)hash.get( "SFCX "));
if(color.equals( "#CCCCCC "))
{
color= "#FFFFFF ";
}
else
{
color= "#CCCCCC ";
}
[解决办法]
顶