java 原生分页
来源:互联网 发布:魅族m9软件 编辑:程序博客网 时间:2024/05/30 04:54
public class PageServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String dang1 = req.getParameter("dang");
if(dang1==null) {
dang1 = "1";
}
Integer dang = Integer.valueOf(dang1);
ProService ps = new ProService();
int count = ps.queryCount();
int totalPage = 1;
if(count%3==0) {
totalPage = count/3;
}else {
totalPage = count/3+1;
}
Page p = new Page(3, count, totalPage, dang);
List<Product> list = ps.queryAll(p);
HttpSession session = req.getSession();
session.setAttribute("proList", list);
session.setAttribute("page", p);
resp.sendRedirect("index.jsp");
}
}
public class ProService {
public int queryCount() {
Connection conn = ConnectionFactory.getConnection();
String sql = "select count(*) from product";
PreparedStatement ps;
int count = 0;
try {
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public List<Product> queryAll(Page p) {
List<Product> list =new ArrayList<>();
Connection conn = ConnectionFactory.getConnection();
String sql = "select p.* from(select rownum as rn,id,name,price,count from product)p where rn > ? and rn <= ? ";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, p.getShow()*(p.getDang()-1));
ps.setInt(2, p.getShow()*p.getDang());
ResultSet rs = ps.executeQuery();
while(rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
int count = rs.getInt("count");
int price = rs.getInt("price");
Product pro = new Product(id, name, price, count);
list.add(pro);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
阅读全文
0 0
- java 原生分页
- django分页(原生分页)
- PHP原生分页
- PHP 原生分页
- 原生分页后搜索
- 原生的搜索分页
- php原生分页
- php 原生分页
- js原生 实现分页
- 原生JQUERY html5 分页
- 原生分页代码
- php 原生分页类
- 原生js分页功能
- 原生js分页效果
- 原生JS完成假分页
- MySQL、oracle分页原生查询
- 原生Bootstrap实现动态分页
- YII2原生SQL分页支持排序搜索
- JDBC---连接SQLserver数据库
- application
- 开瑞k50和k50s 导航傻瓜升级方法,其他车载导航也可以参考。
- 高斯滤波器详解
- 二进制求和
- java 原生分页
- lbp特征与均值模式lbp特征
- Python学习_我该怎么读取文件中的数据并操作文件
- vector 声明时长度预设
- 第26章 线程
- Eclipse使用git的Push出现rejected
- spark 对于key/value 排序 取前几 top take(转载)
- Spring Boot集成Redis java实现配置
- ARM与 X86 CPU架构对比