/**
* 获取分页查询语句
* @param sql
* @param currIndex 当前页数
* @param pageSize 页面大小
* @param count 总记录数
* @return
*/
private String getPageSql(String sql,int currIndex,int pageSize,int count){
String pageSql = null;
int endNum = 10;
int beginNum = 0;
int totalPage = getTotalPage(pageSize,count);
if(currIndex != 0){
beginNum = (currIndex -1)*pageSize;
if(currIndex < totalPage){
endNum = currIndex*pageSize;
}
else{
endNum = count;
}
}
pageSql = "select * from (select a.*,rownum rn1 from ("+sql+")a where rownum <="+endNum+")where rn1>="+beginNum;
return pageSql;
}
/**
* 获取总页数
* @param pageSize 页面大小
* @param count 总记录数
* @return
*/
private int getTotalPage(int pageSize,int count){
int totalPage = 0;
if(pageSize != 0 && count != 0){
totalPage = count%pageSize == 0 ? count/pageSize : count/pageSize +1;
}
return totalPage;
}
/**
* 获取查询记录数
* @param talbeName 表名
* @param condition 查询条件
* @param conn 数据库连接
* @return
*/
private int getCount(String tableName ,String condition,Connection conn){
String sql = "select count(*) from "+tableName+" "+condition;
Statement st = null;
ResultSet rs = null;
int count = 0;
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
oralce分页查询工具代码
最新推荐文章于 2024-08-19 08:45:16 发布