⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fenye_bean.java

📁 模仿Taobao购物网
💻 JAVA
字号:
package taobaoproject;
import java.io.*;
import java.sql.*;
import java.util.*;

public class FenYe_Bean {
    private Connection conn;
    private PreparedStatement past;
    private ResultSet rst;
    private long p_start;//开始纪录
    private long p_end;//结束纪录
    private long p_curpage;//当前页数
    private long p_totalnum;//总记录数
    private long int_num = 9;//每页5条
    private long p_totalpage;//总的页数
    private long k_start;//显示开始纪录
    private long k_end;//显示结束纪录
    private ArrayList goodsList;//商品列表
    private int classID;//分类ID
    private String likeSt;
    public Connection getConn() {
        DB_Bean dbBean = new DB_Bean();
        conn = dbBean.getConn();
        return conn;
    }

//计算总行数
    public long getRowCount(){
        getConn();
        String sqlRow = "select count(*) from tb_GoodsInfo where 1=1";
        if(classID != -1)
            sqlRow += " and ClassID= " + classID;
        sqlRow += " and  GoodsName like '%" +likeSt + "%'";
        java.lang.System.out.println(sqlRow );
        try {
            past = conn.prepareStatement(sqlRow);
            rst = past.executeQuery();
            rst.next();
            p_totalnum = rst.getInt(1);
            p_totalpage = (long)Math.ceil((double)rst.getInt(1)/(double)int_num);
            return p_totalpage;
        } catch (Exception ex) {
            return 0;
        }
    }

    public String getSqlBySqlServer2000(String sql,long pageNo,String sortType) {
        String descType;
        String adcType;
        if(sortType == "time"){
            descType = " order by AddDate desc";
            adcType = " order by AddDate asc";
        }
        else if(sortType.equals("dptime")) {
            descType = " order by AddDate asc";
            adcType = " order by AddDate desc";
        }
        else if(sortType.equals("price")) {
            descType = " order by MemberPrice desc";
            adcType = " order by MemberPrice asc";
        }
        else if(sortType.equals("dprice")) {
            descType = " order by MemberPrice asc";
            adcType = " order by MemberPrice desc";
        }
        else {
            descType = " order by t1.autoid asc";
            adcType = " order by t1.autoid desc";
        }
        p_curpage = pageNo;
        p_totalpage =this.getRowCount();//获取总页数
        p_start = p_totalnum - (p_curpage - 1)*int_num;
        //p_start = (p_totalpage - p_curpage) * int_num;
        p_curpage = p_curpage > p_totalpage ? p_totalpage : p_curpage;
        k_start = (p_curpage - 1) * int_num + 1;
        k_end = (p_curpage * int_num) >= p_totalnum ? p_totalnum : (p_curpage * int_num) ;
        if(p_curpage*int_num>=p_totalnum) {
            int_num = int_num - (p_curpage*int_num - p_totalnum);
            p_start = int_num;
        }
        String strSql = "Select top " + int_num + " t1.* from (select top "+ p_start
                        + " * from ("+ sql+ ") as t1 " + descType + ")as t1 " + adcType;
        System.out.println(strSql);
        System.out.println(p_totalpage);
        return strSql;
    }

    public ArrayList getGoodsList(int id, String likestr,String sortType) {
        getConn();
        classID = id;
        likeSt = likestr;
        String sqlSch = "select t.GoodsID as autoid,t.* from tb_GoodsInfo t where 1=1 ";
        if(id != -1)
            sqlSch += " and ClassID = " + classID;
        if(!likeSt.equals(""))
            sqlSch += " and  GoodsName like '%" +likeSt + "%'";
        String sql = getSqlBySqlServer2000(sqlSch ,p_curpage,sortType);
        try {
            past = conn.prepareStatement(sql);
            rst = past.executeQuery();
            goodsList = new ArrayList();
            while(rst.next())
            {
                GoodsBean goods = new GoodsBean();
                goods.setGoodsID(rst.getInt("GoodsID"));
                goods.setClassID(rst.getInt("ClassID"));
                goods.setGoodsName(rst.getString("GoodsName"));
                goods.setGoodsUrl(rst.getString("GoodsUrl"));
                goods.setMarketPrice(rst.getFloat("MarketPrice"));
                goods.setMemberPrice(rst.getFloat("MemberPrice"));
                goodsList.add(goods);
            }
        } catch (SQLException ex) {
        }
        try {
            rst.close();
            past.close();
            conn.close();
        } catch (SQLException ex1) {
        }
        return goodsList;
    }

//上一页
    public long getPrepage()
    {
        if (p_curpage - 1 >= 1) {
            return p_curpage - 1;
        } else {
            return 1;
        }
    }

//下一页
    public long getNextpage()
    {
        if (p_curpage + 1 <= p_totalpage) {
            return p_curpage + 1;
        } else {
            return p_totalpage;
        }
    }

    public PreparedStatement getPast() {
        return past;
    }

    public ResultSet getRst() {
        return rst;
    }

    public long getP_start() {
        return p_start;
    }

    public long getP_end() {
        return p_end;
    }

    public long getP_curpage() {
        return p_curpage;
    }

    public long getP_totalnum() {
        return p_totalnum;
    }

    public long getInt_num() {
        return int_num;
    }

    public long getP_totalpage() {
        return p_totalpage;
    }

    public long getK_end() {
        return k_end;
    }

    public long getK_start() {
        return k_start;
    }

    public void setP_start(long p_start) {
        this.p_start = p_start;
    }

    public void setP_end(long p_end) {
        this.p_end = p_end;
    }

    public void setP_curpage(long p_curpage) {
        this.p_curpage = p_curpage;
    }

    public void setP_totalnum(long p_totalnum) {
        this.p_totalnum = p_totalnum;
    }

    public void setInt_num(int int_num) {
        this.int_num = int_num;
    }

    public void setP_totalpage(long p_totalpage) {
        this.p_totalpage = p_totalpage;
    }

    public String getGBK(String str)
    {
        try {
            String ff = new String(str.getBytes("ISO-8859-1"),"GBK");
            return ff;
        } catch (UnsupportedEncodingException ex) {
            return "";
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -