1、Math.ceil();
Math.floor():舍去小数,仅取整数部分
Math.ceil():将小数部分一律向整数部分进位
示例:
Math.ceil(3.1)=4
Math.floor(3.1)=3
int pageSize = 4;
int count = productDao.count(category);//得到总记录数 5
int totalPage = (int)Math.ceil(count*1.0/pageSize); //求出总页数 2
2、ScalarHandler用法:返回值类型为long类型,不能直接强转
new ScalarHandler(1)
示例:
//查询商品总记录数-根据商品类别
public int count(String cg) throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql = "SELECT count(*) FROM products";
//如果category不是空,就把条件加上
if (!"".equals(cg)){
sql += "where category = '"+cg+"'";
}
long l = (Long)qr.query(sql, new ScalarHandler(1));
return (int)l;
}