码迷,mamicode.com
首页 > 数据库 > 详细

sql server和mysql中分别实现分页功能

时间:2016-08-22 18:04:12      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

 MySQL

在MySQL中,可以用 Limit 来查询第 m 列到第 n 列的记录,

例如: select * from tablename limit m, n

sql="select * from users order by userId limit "+(pageNow-1)*pageSize+","+pageSize ; 

获取分页后每页的数据:

//分页显示用户的 信息
public ArrayList getUsersByFenye(int pageSize,int pageNow){

String sql="select * from users order by userId limit "+(pageNow-1)*pageSize+","+pageSize ;
SqlHelper sqlHelper=new SqlHelper();
ArrayList al=new ArrayList();
ResultSet rs=sqlHelper.executeQuery(sql, null);
try {
while(rs.next()){
User user=new User();
user.setUserId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.setPhone(rs.getString(4));
user.setGrade(rs.getInt(5));	
al.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return al;
}

  

获取总页数:

public int getPageCount (int pageSize){
String sql="select count(*) from users";
int pageCount=0;	
SqlHelper sqlHelper=new SqlHelper();
ResultSet rs=sqlHelper.executeQuery(sql, null);
try {
if(rs.next()){
int rowCount=0;
rowCount=rs.getInt(1);
pageCount=(rowCount-1)/pageSize+1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pageCount;
}  

SQL Server

但是,在SQL Server中,不支持 Limit 语句。怎么办呢?
解决方案:
虽然SQL Server不支持 Limit ,但是它支持 TOP

 ps=ct.prepareStatement("select top "+pageSize+" * from users where userId not in (select top "+pageSize*(pageNow-1)+" userId from users)");

sql server和mysql中分别实现分页功能

标签:

原文地址:http://www.cnblogs.com/xiangkejin/p/5796374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!