关于分页,十万火急求助!!谢谢!

june37 2005-04-15 10:36:46
我现在只是从数据中取得数据直接显示的,我要求现在只能10条数据显示一页,应该怎么实现呢?
<table width="75%" border="1" align="center">
<%
String id=request.getParameter("id");
//out.print(id);
String strSql="select * from pro_info where id="+id;
cxx data;
xxInfo.OpenDb(strSql);
for(int i=0;i<xxInfo.GetRecordCount();i++)
{
data = (cxx)xxInfo.GetRecord(i);
%>
<tr>
<td width="20%" bgcolor="#97F19E">问题编号:</td>
<td><%=data.prono%></td>
</tr>
<tr>
<td width="20%" bgcolor="#97F19E">问题名称:</td>
<td><%=data.proname%></td>
</tr>
<tr>
<td width="20%" bgcolor="#97F19E">出错系统名称:</td>
<td ><%=data.errorname%></td>
</tr> </table>
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
thrive_li 2005-04-15
  • 打赏
  • 举报
回复
/**
* 分页查询方法.可通过列名取值.
* @param:int rowsPerPage 每页显示行数.
* @param:int goToPageNum 将要显示的页码.
* @param:int rowCount 总行数.
* @Author:Thrive_Li
* @改成你自己的数据库连接就行了
*/
public Vector open_withFieldName(String sql, int rowsPerPage,
int goToPageNum) {
Connection conn =null;

Vector vector = new Vector();

boolean flag = false;
try {
conn = getPooledConnection("your connection");
if (conn == null || goToPageNum <= 0)
return vector;
ResultSet resultset = conn.prepareStatement(sql).executeQuery();
ResultSetMetaData resultsetmetadata = resultset.getMetaData();
int colNum = resultsetmetadata.getColumnCount();
int count = 0;
while (resultset.next()) {

if (count >= (goToPageNum - 1) * rowsPerPage &&
count < goToPageNum * rowsPerPage) {
Hashtable hashtable = new Hashtable();
for (int j = 1; j <= colNum; j++){
hashtable.put(resultsetmetadata.getColumnName(j),
Pub_Func.nullToStr(resultset.getString(j)));
}
vector.addElement(hashtable);
}
count++;
//System.out.println("count="+count);
}
vector.add(""+count);
conn.createStatement().close();
resultset.close();
conn.close();
}
catch (Exception exception) {
exception.printStackTrace();
System.out.print("TableCtrl.open_withName " + "error:" +
exception.getMessage());
}
finally {
if(conn != null)
try
{
conn.close();
} catch(SQLException e)
{
e.printStackTrace();
System.out.println("Error : Connection.close-->" + e);
}

}
return vector;
}
j0hnny 2005-04-15
  • 打赏
  • 举报
回复
可以把数据一次都读出来放到resultset里,然后每页读10条,不过这样的效率比较低,数据少的情况下还可以用。
public Collection gotoPage(int curPage)
{
Collection clt=new ArrayList();
String Sql="select * from news order by id desc";
try
{
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery(Sql);
int i=0;
if(curPage==1)
{
while(rs.next()&&i<10)
{
News news=new News();
news.setId(rs.getInt("id"));
news.setTime(rs.getString("time"));
news.setTitle(rs.getString("title"));
news.setDetail(rs.getString("detail"));
clt.add(news);
i++;
}
}
else
{
rs.absolute((curPage-1)*10);
while(rs.next()&&i<10)
{
News news=new News();
news.setId(rs.getInt("id"));
news.setTime(rs.getString("time"));
news.setTitle(rs.getString("title"));
news.setDetail(rs.getString("detail"));
clt.add(news);
i++;
}
}

rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return clt;
}
xiaoqi161816 2005-04-15
  • 打赏
  • 举报
回复
设几个参数,page页数,pagesize记录条数,开始page设为1,取RS中的第一至第pagesize条记录显示,点“下一页”时,将page++传过去刷新本页,取第pagesize*(page-1)到pagesize*page条记录,用rs.absolute()实现,
MARS.nEIL 2005-04-15
  • 打赏
  • 举报
回复
分页的代码很多..搜索...
xiaofeima0101 2005-04-15
  • 打赏
  • 举报
回复
分页问题为什么不用strust,用strust做比较简单,下一些strust的资料,看看就会明白

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧