jsp 分页 第二页 怎么就不行了
publicResultSetpageList(intpagesize,Stringtablename,intcurrentePage,Stringsort,String...
public ResultSet pageList(int pagesize, String tablename, int currentePage,String sort, String sortBy) throws ClassNotFoundException, SQLException {
ConnectionDB conn=new ConnectionDB();
ConnectionDB conn1=new ConnectionDB();
String sql = "select * from forum";
ResultSet rs = conn.executeQuery(sql);
if (currentePage == 1) {
String getRs = "select top 5 * from forum where id>0 order by id desc ";
ResultSet pageRs = conn1.executeQuery(getRs);
return pageRs;
} else {
rs.absolute(pagesize * (currentePage - 1));
int lastid = rs.getInt("id");
String getRs = "select * from forum where id>lastid order by id desc";
ResultSet pageRs = conn1.executeQuery(getRs);
return pageRs;
}
} 展开
ConnectionDB conn=new ConnectionDB();
ConnectionDB conn1=new ConnectionDB();
String sql = "select * from forum";
ResultSet rs = conn.executeQuery(sql);
if (currentePage == 1) {
String getRs = "select top 5 * from forum where id>0 order by id desc ";
ResultSet pageRs = conn1.executeQuery(getRs);
return pageRs;
} else {
rs.absolute(pagesize * (currentePage - 1));
int lastid = rs.getInt("id");
String getRs = "select * from forum where id>lastid order by id desc";
ResultSet pageRs = conn1.executeQuery(getRs);
return pageRs;
}
} 展开
展开全部
说实话 没看明白你写的
不过我给你一个我曾经写过的 仅供参考
大概意思 你传给一个 pagenow 得出显示数据
主要是看一下sql语句的意思:从数据中搜索前pagesize条数据,并且去除 前pagesize*(pagenow-1)条数据 也就是你要选择的页数的数据,当时我也是想了很长时间才明白。
private Statement sm=null;
private ResultSet rs=null;
private Connection conn=null;
private int pageSize=4;//每页显示条数
private int pageCount=0;//给只是通过pageSize和pageNow来计算的 显示页数
private int rowCount=0;//通过数据库查询得到
public ArrayList checkAllUserPage(int pageNow){
ArrayList al=new ArrayList();
try {
ConnDB db=new ConnDB();
conn=db.getConn();
//3创建Statement
sm=conn.createStatement();
//查询出显示的记录
rs=sm.executeQuery("select top "+pageSize+" * from users where userId not in(select top "+pageSize*(pageNow-1)+" userId from users)");
while(rs.next()){
UserBean ub=new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUserName(rs.getString(2));
ub.setUserPW(rs.getString(3));
ub.setUserGrade(rs.getString(4));
//讲ub放入到ArrayList中
al.add(ub);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
if(conn!=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return al;
}
不过我给你一个我曾经写过的 仅供参考
大概意思 你传给一个 pagenow 得出显示数据
主要是看一下sql语句的意思:从数据中搜索前pagesize条数据,并且去除 前pagesize*(pagenow-1)条数据 也就是你要选择的页数的数据,当时我也是想了很长时间才明白。
private Statement sm=null;
private ResultSet rs=null;
private Connection conn=null;
private int pageSize=4;//每页显示条数
private int pageCount=0;//给只是通过pageSize和pageNow来计算的 显示页数
private int rowCount=0;//通过数据库查询得到
public ArrayList checkAllUserPage(int pageNow){
ArrayList al=new ArrayList();
try {
ConnDB db=new ConnDB();
conn=db.getConn();
//3创建Statement
sm=conn.createStatement();
//查询出显示的记录
rs=sm.executeQuery("select top "+pageSize+" * from users where userId not in(select top "+pageSize*(pageNow-1)+" userId from users)");
while(rs.next()){
UserBean ub=new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUserName(rs.getString(2));
ub.setUserPW(rs.getString(3));
ub.setUserGrade(rs.getString(4));
//讲ub放入到ArrayList中
al.add(ub);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
if(conn!=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return al;
}
追问
用了你这句 rs=sm.executeQuery("select top "+pageSize+" * from users where userId not in(select top "+pageSize*(pageNow-1)+" userId from users)");
就可以了 你这句写的很好 谢谢啦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询