目录
1,ajax(无刷新分页)
获取数据库中大量的信息显示在页面上,必然要使用到分页查询;若不使用Ajax,而是用其他的方法,肯定是要刷新页面的,用户体检很不好,所以最好使用Ajax的方法写分页查询;
2,导入jar包&js库
3,方法&实体类
public class Goods {
private int gid; // 商品编号
private String gname; // 商品名称
private double gprice;// 商品单价
private String ginfo; // 商品描述信息
private String gpath; // 商品图片路径
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public double getGprice() {
return gprice;
}
public void setGprice(double gprice) {
this.gprice = gprice;
}
public String getGinfo() {
return ginfo;
}
public void setGinfo(String ginfo) {
this.ginfo = ginfo;
}
public String getGpath() {
return gpath;
}
public void setGpath(String gpath) {
this.gpath = gpath;
}
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(int gid, String gname, double gprice, String ginfo, String gpath) {
this.gid = gid;
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
}
public Goods(String gname, double gprice, String ginfo, String gpath) {
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
}
}
public class Nb {
private List<Goods> listGoods = new ArrayList<Goods>();
private int maxPage;
public List<Goods> getListGoods() {
return listGoods;
}
public void setListGoods(List<Goods> listGoods) {
this.listGoods = listGoods;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public Nb() {
// TODO Auto-generated constructor stub
}
public Nb(List<Goods> listGoods, int maxPage) {
super();
this.listGoods = listGoods;
this.maxPage = maxPage;
}
}
订单项
public class OrderItem {
private Goods goods;
private int gcount;
private double gsum;
//计算订单项的价格
public void calc() {
this.gsum = this.goods.getGprice() * this.gcount;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getGcount() {
return gcount;
}
public void setGcount(int gcount) {
this.gcount = gcount;
}
public double getGsum() {
return gsum;
}
public void setGsum(double gsum) {
this.gsum = gsum;
}
public OrderItem() {
// TODO Auto-generated constructor stub
}
public OrderItem(Goods goods, int gcount, double gsum) {
this.goods = goods;
this.gcount = gcount;
this.gsum = gsum;
}
public OrderItem(int gcount, double gsum) {
this.gcount = gcount;
this.gsum = gsum;
}
@Override
public String toString() {
return "OrderItem [goods=" + goods + ", gcount=" + gcount + ", gsum=" + gsum + "]";
}
}
GoodsDao 方法
public class GoodsDao implements IGoodsDao{
private List<Goods> listGoods = new ArrayList<Goods>();
private Connection con = null;
private PreparedStatement ps;
private ResultSet rs;
private Goods goods;
private int maxPage;
private String sql;
private int n;
@Override
public List<Goods> listGoods(String pageIndex, String str) {
int pageSize = 5;
int start = (Integer.parseInt(pageIndex) -1) * pageSize +1;
int end = Integer.parseInt(pageIndex) * pageSize;
try {
con = DBhelper.getCon();
sql = "select * from (select a.*,rownum myrid from tb_goods a where gname like '%"+str+"%') b where myrid between ? and ?";
ps = con.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while(rs.next()) {
goods = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
listGoods.add(goods);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBhelper.closeObj(con, ps, rs);
}
return listGoods;
}
@Override
public int getMaxPage(String str) {
try {
con = DBhelper.getCon();
sql = "select count(*) from tb_goods where gname like '%"+str+"%'";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
maxPage = rs.getInt(1) / 5;
if(rs.getInt(1) % 5 != 0) {
maxPage ++;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBhelper.closeObj(con, ps, rs);
}
return maxPage;
}
@Override
public int delGoods(String gid) {
try {
con = DBhelper.getCon();
sql = "delete from tb_goods where gid = "+gid;
ps = con.prepareStatement(sql);
n = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBhelper.closeObj(con, ps, rs);
}
return n;
}
@Override
public int updateGoods(Goods goods) {
try {
con = DBhelper.getCon();
// sql = "update tb_goods(gname,)";
ps = con.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBhelper.closeObj(con, ps, rs);
}
return 0;
}
@Override
public Goods list(String gid) {
try {
con = DBhelper.getCon();
sql = "select * from tb_goods where gid = "+gid;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
goods = new Goods(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBhelper.closeObj(con, ps, rs);
}
return goods;
}
}