2013-02-14 50 views
0

(**忽略大小寫敏感性**)檢索一個信息一對多映射在JDBC

下面是我的父表

Parent Table 

    ID  name  name_id 
    1001  A   1 
    1002  B   2 
    1003  AA   3 

    Child Table 

    Payment Table 

    ID  PaymentName name_id 
    1   Credit   3 
    2   Debit   3 

輸出是

paymentName   name   name_id 
    Credit    AA    3 
    Debit    AA    3 

在我的Java

父POJO是

int nameID; 
    String name; 

    List<Payment> paymentNames = new ArrayList<Payment>(); 

JDBC調用

while (resultSet.next()) {      parent.setNameID(resultSet.getInt("name_ID")); 
      parent.setName(resultSet.getString("name")); 

    } 

我的問題後,我該如何找回paymentNames的名單?

+0

什麼的SQL樣子?你加入這兩張牌桌嗎? – 2013-02-14 20:33:23

+0

SELECT parent.name,parent.name_id,payment.paymentName FROM父 JOIN付款 ON parent.name_id = payment.name_id WHERE parent.name_id = '3' WHERE approval_path.APPROVAL_PATH_ID = '2' – ashlesha 2013-02-14 20:57:10

回答

0

明白了。

創建的列表

Payment payment; 
    List<Payment> paymentList = new ArrayList<Payment>(); 

更新while循環

while(resultset.next()) 
    { 
    payment = new Payment(); 
    parent.setNameID(resultSet.getInt("name_ID")); 
    parent.setName(resultSet.getString("name")); 
    payment.setPaymentName(resultSet.getString("paymentName"); 
    paymentList.add(payment); 
    } 
    parent.setPaymentNames (paymentList)