jQuery ajax(无刷新分页)

目录

1,ajax(无刷新分页)

2,导入jar包&js库

3,方法&实体类

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;
    }
    }


    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值