0
以下是我的JDBC代碼,以從多個表如何處理在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
}
我從多個表中獲取數據在jsp頁面中顯示。 這段代碼獲取正確的數據。 問題是,如果productDetails
多於一個,那麼它重複customerDetails
和billDetails
多達productDetails
。
我想一個customerDetails
一個billDetails
和一個或多個productDetails
數據庫
可用如何解決這個問題。
什麼是其他方式,只有選定的行可以被提取而不需要重複的行加入 – xrcwrn