sqlServer 中使用sql语句实现固定分页功能
发布时间:2020-12-12 14:10:18 所属栏目:MsSql教程 来源:网络整理
导读:package javabean; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import dao.ConDB; import entity.Product; import impl.ProductImp; public class DealProduct extends ConDB implements ProductImp { ?? ?int coun
package javabean;
import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import dao.ConDB; import entity.Product; import impl.ProductImp; public class DealProduct extends ConDB implements ProductImp { ?? ?int count =4;// 确定每页显示几行数据 ??? /**? ???? *查询分页信息 ???? *page 是前台的当前页 ??? **/ ?? ?public List<Product> selectProductPage(int page) {?? ??? ?List<Product> list = new ArrayList<Product>(); ?? ??? ?Product product=null; ?? ??? ?int rowBegin =0; ?? ??? ?if(page>1){ ?? ??? ??? ?rowBegin = (page-1)*count; ?? ??? ?} ?? ??? ?try { ?? ??? ??? ?conn = getConDB(); ?? ??? ?? ? ?String sql ="select top "+count+" * from [company].[dbo].[PRODUCT] where productId not in(select top "+rowBegin+" productId " + ?? ??? ??? ??? ??? ?"from [company].[dbo].[PRODUCT] order by price desc) order by price desc"; ?? ??? ??? ?pst = conn.prepareStatement(sql); ?? ??? ??? ?rs = pst.executeQuery(); ?? ??? ??? ??? ?while(rs.next()){ ?? ??? ??? ??? ??? ?product = new Product(); ?? ??? ??? ??? ??? ?product.setProductID(Integer.parseInt(rs.getString(1))); ?? ??? ??? ??? ??? ?product.setSerialNumber(rs.getString(2)); ?? ??? ??? ??? ??? ?product.setName(rs.getString(3)); ?? ??? ??? ??? ??? ?product.setBrand(rs.getString(4)); ?? ??? ??? ??? ??? ?product.setModel(rs.getString(5)); ?? ??? ??? ??? ??? ?product.setPrice(Float.parseFloat(rs.getString(6))); ?? ??? ??? ??? ??? ?product.setPicture(rs.getString(7)); ?? ??? ??? ??? ??? ?product.setDescription(rs.getString(8)); ?? ??? ??? ??? ??? ?list.add(product); ?? ??? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} catch (SQLException e1) { ?? ??? ??? ?e1.printStackTrace();?? ? ?? ??? ?}finally{ ?? ??? ??? ?closeAll(conn,pst,rs); ?? ??? ?} ?? ??? ?return list; ?? ?} } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |