sqlserver数据库分页问题,急、、、

zhaosy 2005-11-03 11:04:18
各位兄弟姐妹,这个程序再oracle、mysql上执行没任何问题
其中:$where为条件,currentPage为当前页码,totalRecords为记录总数,numPerPage为页记录数
在oracle、mysql上可以返回正确的记录总数,但在sqlserver上返回记录总数为-3,是何原因啊?
应该如何去改正呢?谢谢各位了!
public static List query(String $where, Integer currentPage,
AIInteger totalRecords, int numPerPage) {
List templist = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = MyDBHelper.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
rs.last(); //1>处
totalRecords.setValue(rs.getRow()); //2>处
System.out.println("current page:= " + currentPage.toString() +
"; total:= " +
totalRecords + "; numPerPage:= " + numPerPage);
if (currentPage.intValue() > 1)
rs.absolute( (currentPage.intValue() - 1) * numPerPage);
else
rs.beforeFirst();
rs.setFetchSize(numPerPage);

while (rs.next()) {



tempList.add(temp);
numPerPage--;
}
rs.close();
stmt.close();
}
catch (SQLException ex) {
while (ex != null) {
System.out.println("Message :" + ex.getMessage());
}
}
catch (java.lang.Exception ex) {
ex.printStackTrace();
}
//释放一个连接到连接池
finally {
try {
conn.close();
}
catch (SQLException sqle) {
}
}
return tempList;
}
...全文
188 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2005-11-03
  • 打赏
  • 举报
回复
获得resultset之后
int counter = 0;
while(resultset.next())
counter++;

来获得总记录数,或者也可以参考楼上的方法
last、first等方法经常会有问题
柯本 2005-11-03
  • 打赏
  • 举报
回复
csdn有多个关于rs.first(), rs.last()问题的
它有时总不能得到期望的结果(与数据库有关,与数据库返回类型表关,如表或光标)
这个好像都没有一个合理的解释或解决方案
要解决你的问题,可用
select count(*) recc form 表 where 条件
来先预查记录数
zhaosy 2005-11-03
  • 打赏
  • 举报
回复
兄弟,能说的具体些吗?怎么改正呢?
believefym 2005-11-03
  • 打赏
  • 举报
回复
有可能是rs.last();的问题
你试试next循环计数
treeroot 2005-11-03
  • 打赏
  • 举报
回复
sqlserver uses top clause!
mysql uses limit clause!
oracle uses row_num function

using cursor is unefficient

62,614

社区成员

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

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