gpt4 book ai didi

java - ResultSet 为包含另一个 POJO 的数组列表的 POJO

转载 作者:行者123 更新时间:2023-12-02 13:07:04 25 4
gpt4 key购买 nike

希望有人能帮助我。我有一个具有以下结构的 POJO:

public class Invoice{

private String docNum;
private String customer;
private ArrayList<InvoiceDetails> invoiceDetails;

/* Getters and setters*/
}

另一个 POJO 具有以下内容

public class InvoiceDetails{

private String taxRate;
private Double taxAmount;
private Double amount;

/* Getters and setters*/
}

我想知道的是从这样的结果集中填写 Pojo Invoice 的最佳方式:

String sql= "SELECT  InvoiceNumber, Customer, TaxRate, TaxAmount,Amount FROM TAX_VIEW WHERE Date=?"
PreparedStatement pst = cn.prepareStatement(Config.SQL_FACTURAS, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
pst.setDate(1, date);
ResultSet rs = pst.executeQuery();

假设ResultSet可以为同一张发票返回多条记录,是否可以一劳永逸,而不需要第二次迭代?非常感谢

最佳答案

在你看来,我猜已经存在一个连接。我想 Invoice.docNum 与 SELECT 中的 InvoiceNumber 相同。 (为什么有不同的名字?)因此,您应该将“ORDER BY InvoiceNumber”添加到您的选择中,并在处理循环中验证它的更改。代码是伪代码,未经测试:

ResultSet rs = pst.executeQuery();
Invoice invoice=new Invoice();
while(rs.next()){
if (invoice.getDocNum().compareTo(rs.getString(1))!=0) {
// This is a new Invoice, so process previous if not the first one
// and create a new instance
invoice=new Invoice();
invoice.setDocNum(rs.getString(1));
invoice.setCustomer(rs.getString(2));
invoice.setInvoiceDetails(new ArrayList<InvoiceDetails>());
}
InvoiceDetails invoiceDetails = new InvoiceDetails();
invoiceDetails.setTaxRate(rs.getString(3));
// ... further member settings, and finally add to list:
invoice.getInvoiceDetails.add(invoiceDetails);
}
// finally process the last Invoice if it is not empty

关于java - ResultSet 为包含另一个 POJO 的数组列表的 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44108030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com