sqlServer中使用sql语句实现固定分页功能
时间:2021-07-01 10:21:17
帮助过:18人阅读
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;
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
selectProductPage(int page) {
List list = new ArrayList();
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;
}
}