分页分为两种分页,一是数据库分页。二是,java程序分页,这两种的其别的,数据库分页可以操作大量数据。而java程序分页只能操作小量数据,因此,在大多数情况下我们都用数据库分页,SQL我用top进行分页
select top 5 from t_user where not in (select top 5 from t_user
);其实我们还需要查询出我需要的数据一共有多少条,然后来进行计算,得出总页数,然后我才根据页数来进行查询数据库
数据库操作代码
package com.lovo.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.lovo.po.UserInfoPO;
public class TestDao extends BaseDao implements InterUserInfo
{
public List cutePage(int currentPage, int
pageCount) throws SQLException {
List
list = new
ArrayList();
setConnection();
String sql = "select top " +
pageCount
+
" * from jobs where job_id not in(select top " + currentPage
+
" job_id from jobs)";
System.out.println(sql);
this.ps =
this.conn.prepareStatement(sql);
this.rs =
this.ps.executeQuery();
while (this.rs.next())
{
UserInfoPO po
= new UserInfoPO();
po.setId(rs.getInt("job_id"));
po.setUserName(rs.getString("job_desc"));
po.setUserpwd(rs.getString("min_lvl"));
list.add(po);
}
System.out.println(list.get(1).getUserName());
colseConnection();
return list;
}
public int getTcotalPage(int pageCount) throws
SQLException {
int in = 0;
setConnection();
this.ps = this.conn
.prepareStatement("select
count(job_id) aa from jobs");
this.rs =
this.ps.executeQuery();
if (rs.next()) {
in =
rs.getInt("aa");
System.out.println(in);
}
if (in % pageCount == 0)
{
colseConnection();
return in /
pageCount;
} else {
colseConnection();
return in /
pageCount + 1;
}
}
public static void main(String[] args) {
TestDao test = new
TestDao();
try {
test.cutePage(3,
4);
System.out.println(test.getTcotalPage(4));
} catch (SQLException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
}