各位大师谁能帮我解释一下这二段代码

来源:百度知道 编辑:UC知道 时间:2024/05/28 20:17:14
public int getPageCount(int pageSize){
int pageCount = 0;
int count = 0;
String sql = "Select Count(*) From SecondLevelTitle";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
conn = ConnectionManager.getConnection();
pstmt = conn.prepareStatement(sql);

//获得结果集
rs = pstmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ConnectionManager.closeAll(rs, pstmt, conn);
}
if(count>0)
{
pageCount = (count % pageSize == 0) ? (count / pageSize) : (count/pageSize + 1);
}
return pageCount;
}

/**
* 获取指定页的数据

SQL Server 的分页。。。

第一个方法表示求 SecondLevelTitle表可以分几页(总页数)
pageCount = (count % pageSize == 0) ? (count / pageSize) : (count/pageSize + 1);可以换为
pageCount = (count + pageSize-1)/pageSize;

第二个方法表示获取当前页的所有数据,封装成对象,放入集合里面。可以在页面迭代这个集合。。

1.第一段是得到表里面所有的记录数 select count(*) from 表.
2.第二段是得到指定页的记录 String sql = "select top "+pageSize+" * from SecondLevelTitle where titleId not in(select top "+rowBegin+" titleId from SecondLevelTitle)";

天啊!就给5个积分