简体   繁体   English

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

[英]How to handle repeated value in jdbc while joining multiple tables

Following is my jdbc code to fetch data from multiple tables 以下是我的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;
    }

Bill.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 
 }

I am fetching data from multiple table and showing in jsp page. 我正在从多个表中获取数据并显示在jsp页面中。 This code is fetching proper data. 这段代码正在获取适当的数据。 Problem is that if productDetails are more that one then it is repeating customerDetails and billDetails as much as productDetails are there. 问题是,如果productDetails billDetails ,那么它将重复productDetailsbillDetails一样多的customerDetailsbillDetails

I want one customerDetails one billDetails and one or more productDetails as available in database 我希望有一个customerDetails一个billDetails和一个或多个productDetails在数据库中可用

在此处输入图片说明

How to resolve this. 如何解决这个问题。

I believe the issue is that you are joining the tables to get your data back from SQL, which means the database will be returning multiple rows: 我认为问题在于您正在联接表以从SQL取回数据,这意味着数据库将返回多行:

customer1 bill1 product1
customer1 bill1 product2
etc

There are a few ways to solve this. 有几种解决方法。 The simplest, depending on if you have enough memory to keep data in memory, is to store your Bill in a hashmap, and as you iterate through the rows, check to see if the Bill is already in the hashmap. 根据您是否有足够的内存将数据保留在内存中,最简单的方法是将Bill储存在哈希图中,并在您遍历各行时,检查Bill是否已经在哈希图中。 If it is, add the product to the product list in the bill, otherwise, create a new Bill object and add to the map. 如果是这样,则将产品添加到清单中的产品列表中,否则,创建一个新的Bill对象并添加到地图中。

Then when you display it on the page, you would display: 然后,当您在页面上显示它时,将显示:

Bill1
  - product1
  - product2

Etc. Hope that helps. 等等,希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM