Java 如何在连接多个表时处理jdbc中的重复值

Java 如何在连接多个表时处理jdbc中的重复值,java,sql,jdbc,Java,Sql,Jdbc,下面是我从多个表中获取数据的jdbc代码 public Set<Bill> fetch(long billNo) { System.out.println(" BillProductDetailsBusiness fetch()"); status = "success"; Set st = new HashSet(); con = ConnectionFactory.getConnection();

下面是我从多个表中获取数据的jdbc代码

public Set<Bill> fetch(long billNo) {

        System.out.println(" BillProductDetailsBusiness fetch()");
        status = "success";

        Set st = new HashSet();
        con = ConnectionFactory.getConnection();

        try {

            String query = "select distinct bpd.product_id,bpd.bill_no,"
                    + "bpd.qty, bpd.unit_price as up,bpd.selling_price sp,"
                    + "bpd.discount_percent as dp, "
                    + "bd.*,cu.*,pr_dl.*, "
                    + " colors.color,pmodeld.model_no, ptype.product_type,  "
                    + "sizes.size, pbrand.brand,suppd.name  "
                    + "from bill_product_details as bpd  "
                    + "left join bill_details as bd "
                    + "on  bd.bill_no=bpd.bill_no "
                    + "left join product_details as pr_dl "
                    + "on bpd.product_id=pr_dl.barcode "
                    + "left join  "
                    + "colors  "
                    + "on pr_dl.color_id=colors.color_id  "
                    + "left join  "
                    + "product_model_details as pmodeld  "
                    + "on pr_dl.model_id=pmodeld.model_no_id  "
                    + "left join  "
                    + " product_brand as pbrand  "
                    + "on pr_dl.brand_id=pbrand.id  "
                    + "left join  "
                    + "product_types as ptype  "
                    + "on pr_dl.product_type_id=ptype.product_type_id  "
                    + "left join  "
                    + "sizes  "
                    + "on pr_dl.size_id=sizes.size_id  "
                    + "left join  "
                    + "supplier_details as suppd  "
                    + " on pr_dl.supplier_id=suppd.id   "
                    + "left join customer_details as cu "
                    + "on bd.customer_id=cu.id "
                    + "where bpd.bill_no=? ";

            ps = con.prepareStatement(query);
            ps.setLong(1, billNo);

            System.out.println("Before execution");
            res = ps.executeQuery();
            Bill b;
            ProductDetailsAction pb = null;
            while (res.next()) {
                b = new Bill();
                long product_id = res.getLong("product_id");
                int qty = res.getInt("qty");
                float unit_price_s = res.getFloat("up");
                float sell_price_s = res.getFloat("sp");
                int dis_per_s = res.getInt("dp");

                long bill_no = res.getLong("bill_no");
                long customer_id = res.getLong("customer_id");
                float sub_total = res.getFloat("sub_total");
                float vat = res.getFloat("vat");
                float total = res.getFloat("total");
                String payment_type = res.getString("payment_type");
                Timestamp add_date = res.getTimestamp("add_date");

                long bar_code = res.getLong("barcode");
                String color = res.getString("color");
                String model_no = res.getString("model_no");
                String brand = res.getString("brand");
                String product_type = res.getString("product_type");
                String size = res.getString("size");
                String supplier_name = res.getString("name");
                long quntity = res.getLong("quntity");
                float unit_price = res.getLong("unit_price");
                float selling_price = res.getLong("selling_price");
                int discount_percent = res.getInt("discount_percent");

                long id = res.getLong("id");
                String name = res.getString("name");
                String address = res.getString("address");
                String mobno = res.getString("mobno");
                Date dob = res.getDate("dob");
                Date anniversery = res.getDate("anniversery");
                Timestamp adddate = res.getTimestamp("add_date");
                //setting bill bean
                System.out.println(bill_no + "  " + sub_total);
                b.getBillDetails().setBillNo(bill_no);
                b.getBillDetails().setSubTotal(sub_total);
                b.getBillDetails().setVat(vat);
                b.getBillDetails().setTotal(total);
                b.getBillDetails().setPaymentType(payment_type);

                SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yy hh:mm aaa");
                String pur_date = DATE_FORMAT.format(add_date);
                b.getBillDetails().setDispDate(pur_date);

                b.getCustomerDetails().setCustomerName(name);
                b.getCustomerDetails().setAddress(address);
                b.getCustomerDetails().setMobno(mobno);
                pb = new ProductDetailsAction();
                pb.setBarCode(bar_code);
                pb.setProductType(product_type);
                pb.setProductSize(size);
                pb.setQuantity(qty);
                pb.setUnitPrice(unit_price_s);
                pb.setSellingPrice(sell_price_s);
                pb.setTotalSellingPrice(sell_price_s * qty);
                pb.setDiscountPercentage(dis_per_s);
                b.getProductDetails().add(pb);
                st.add(b);

            }

            con.close();
        } catch (SQLException s) {
            status = "failure";
            System.out.println("SQL code does not execute." + s);
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException sqe) {
                status = "failure";
                System.out.println("SQLException " + sqe);
            }
        }
        System.out.println("Returning from BillProductDetailsBusiness fetch() ");

        return st;
    }
公共集获取(长billNo){
System.out.println(“BillProductDetailsBusiness fetch()”;
status=“success”;
Set st=新的HashSet();
con=ConnectionFactory.getConnection();
试一试{
String query=“选择不同的bpd.产品标识,bpd.票据编号,”
+bpd.qty,bpd.unit\u price as up,bpd.salling\u price sp
+“bpd.折扣率作为dp,”
+bd.*,cu.*,pr_dl.*
+颜色。颜色,pmodeld.model\u编号,ptype.product\u类型
+“尺码.尺码,品牌,辅助名称”
+“来自账单\产品\详细信息,作为bpd”
+“以bd的形式左键连接账单\u详细信息”
+“在bd.bill\u no=bpd.bill\u no上”
+“将产品详细信息作为pr\U dl左连接”
+“在bpd.product\u id=pr\u dl.barcode上”
+“左连接”
+“颜色”
+“在pr\u dl.color\u id=colors.color\u id上”
+“左连接”
+“产品型号详细信息为pmodeld”
+“在pr_dl.model_id=pmodeld.model_no_id上”
+“左连接”
+“产品品牌为pbrand”
+“在pr_dl.brand_id=pbrand.id上”
+“左连接”
+“产品类型为ptype”
+“在pr_dl.product_type_id=ptype.product_type_id上”
+“左连接”
+“尺寸”
+“在pr\u dl.size\u id=size.size\u id上”
+“左连接”
+“供应商的详细信息作为支持”
+“在请购单上供应商id=供应商id”
+“作为cu左加入客户_详细信息”
+“在bd.customer_id=cu.id上”
+“其中bpd.bill_no=?”;
ps=合同准备陈述(查询);
ps.setLong(1,比尔诺);
System.out.println(“执行前”);
res=ps.executeQuery();
法案b;
ProductDetailsAction pb=null;
while(res.next()){
b=新账单();
long product_id=res.getLong(“product_id”);
整数数量=res.getInt(“数量”);
浮动单位价格=res.getFloat(“上涨”);
浮动卖出价格=res.getFloat(“sp”);
int dis_per_s=res.getInt(“dp”);
长期票据编号=res.getLong(“票据编号”);
long customer_id=res.getLong(“customer_id”);
浮动小计=res.getFloat(“小计”);
浮动增值税=res.getFloat(“增值税”);
浮动总计=res.getFloat(“总计”);
String payment_type=res.getString(“payment_type”);
Timestamp add_date=res.getTimestamp(“add_date”);
长条形码=res.getLong(“条形码”);
字符串颜色=res.getString(“颜色”);
String model_no=res.getString(“model_no”);
字符串品牌=res.getString(“品牌”);
字符串产品类型=res.getString(“产品类型”);
字符串大小=res.getString(“大小”);
字符串供应商名称=res.getString(“名称”);
长质量=res.getLong(“质量”);
浮动单价=res.getLong(“单价”);
浮动销售价格=res.getLong(“销售价格”);
整数折扣百分比=res.getInt(“折扣百分比”);
long id=res.getLong(“id”);
字符串名称=res.getString(“名称”);
字符串地址=res.getString(“地址”);
String mobno=res.getString(“mobno”);
日期dob=res.getDate(“dob”);
日期anniversery=res.getDate(“anniversery”);
Timestamp adddate=res.getTimestamp(“添加日期”);
//设置比尔豆
系统输出打印项次(工程量清单编号+“”+小计);
b、 getBillDetails().setBillNo(账单编号);
b、 getBillDetails().setSubTotal(小计);
b、 getBillDetails().setVat(增值税);
b、 getBillDetails().setTotal(总计);
b、 getBillDetails().setPaymentType(付款类型);
SimpleDateFormat日期_格式=新的SimpleDateFormat(“dd-MMM-yy-hh:mm-aaa”);
字符串pur\u date=date\u FORMAT.FORMAT(添加日期);
b、 getBillDetails().setDispDate(购买日期);
b、 getCustomerDetails().setCustomerName(名称);
b、 getCustomerDetails().setAddress(地址);
b、 getCustomerDetails().setMobno(mobno);
pb=新产品详细信息操作();
pb.设置条码(条形码);
pb.setProductType(产品类型);
pb.setProductSize(尺寸);
pb.设定数量(数量);
pb.设定单价(单价);
pb.设定销售价格(销售价格);
pb.设置总售价(售价×数量);
pb.设定折扣百分比(每单位折扣);
b、 getProductDetails().add(pb);
st.add(b);
}
con.close();
}捕获(SQLS异常){
status=“失败”;
System.out.println(“SQL代码不执行。”+s);
}最后{
试一试{
如果(con!=null){
con.close();
}
}捕获(SQLException sqe){
status=“失败”;
System.out.println(“SQLException”+sqe);
}
}
System.out.println(“从BillProductDetailsBusiness fetch()返回”);
返回st;
}
java

 public class Bill {

        private CustomerAction customerDetails = new CustomerAction();
        private Set<ProductDetailsAction> productDetails = new          HashSet<ProductDetailsAction>(0);
        private BillAction billDetails = new BillAction();
 //getter and setter 
 }
公共类法案{
私人顾客行为
customer1 bill1 product1
customer1 bill1 product2
etc
Bill1
  - product1
  - product2