2015-04-14 204 views
0

我試圖從數據庫連接創建此數據,並使用結果填充數組。我只需要幫助從下面的SQL填充「DestinationItem []」。從Oracle數據庫填充數組

//DestinationBean.java 
// Manual Array works but I need this to be populated from DB using the below Query and DB Connection info. 
    private DestinationItem[] destinationResults = new DestinationItem[]{ 
      new DestinationItem("58285", "Dodge Grand Caravan"), 
      new DestinationItem("57605", "Dodge SX 2.0"), 
      new DestinationItem("58265", "Chrysler 300 Touring") 
    }; 

    public DestinationItem[] getdestinationResults() { 
     return destinationResults; 
    } 

    public class DestinationItem { 
     String destid; 
     String commdefid; 

     public DestinationItem(String destid, String commdefid) { 
      this.destid = destid; 
      this.commdefid = commdefid; 
     } 
// Getter/Setter below 

// END 

我需要採取這種數據庫連接邏輯和上面的「DestinationItem []」數組填充,我需要幫助。

//DBConnection 
public static ArrayList<CustomerBean> getCustomer() { 
    try { 
     Class.forName("oracle.jdbc.OracleDriver").newInstance(); 

     Connection con = DriverManager.getConnection("jdbc:oracle:thin:", "BLAH", "BLAH"); 
     PreparedStatement ps = con.prepareStatement("select destination_id, commdef_id from BLAH.destination"); 

     ArrayList<CustomerBean> al = new ArrayList<CustomerBean>(); 

     ResultSet rs = ps.executeQuery(); 

     boolean found = false; 

     while (rs.next()) { 
      CustomerBean e = new CustomerBean(); 
      e.setDestId(rs.getString("destination_id")); 
      e.setDestId(rs.getString("commdef_id")); 
      al.add(e); 
      found = true; 
     } 

     rs.close(); 
     if (found) { 
      return al; 
     } else { 
      return null; // no entires found 
     } 
    } catch (Exception e) { 
     System.out.println("Error In getCustomer() -->" + .getMessage()); 
     return (null); 
    } 
} 
+0

你的問題到底是什麼? – Mureinik

+0

填充 「DestinationItem []」 使用,我提供 目前我已填充了 「DestinationItem []」 手動與數據庫的連接信息數據庫: 「私人DestinationItem [] destinationResults =新DestinationItem [] { 新DestinationItem ( 「58285」, 「道奇大捷龍」), 新DestinationItem( 「57605」, 「道奇SX 2.0」), 新DestinationItem( 「58265」, 「克萊斯勒300旅行車」) }; 「 但我需要從「從BLAH.destination中選擇destination_id,commdef_id」填充它,我不知道該怎麼做。 – djcajun

回答

0

這裏有一個片段,它應該做你需要的東西:

List<DestinationItem> destinationsList = new ArrayList<DestinationItem>(); 

while (rs.next()) { 
    destinationsList.add(new DestinationItem(
     rs.getString("destination_id"), rs.getString("commdef_id"))); 
} 
rs.close(); 

// if you need to convert the list into an array, you can do it like this: 
DestinationItem[] destinationsArray = destinationsList.toArray(
              new DestinationItem[destinationsList.size()]); 

一些注意事項:

這是最容易使用的動態數據結構,如ListResultSet檢索數據,因爲這樣你不需要關心迭代之前從數據庫返回的行數。

從返回List s的方法(如您的getCustomer()方法)決不會返回null被認爲是最佳做法。如果沒有找到數據,只需返回一個空的List。當不需要檢查null時,這將使客戶端代碼更簡單。

您應該關閉在該方法中打開的Connection conPreparedStatement ps資源。一個常見的成語是在數據訪問方法,這樣的結構:

Connection con = null; 
PreparedStatement ps = null; 
try { 
    // initialize and use 'con' and 'ps' 
} finally { 
    if (ps != null) { 
     ps.close(); 
    } 
    if (con != null) { 
     con.close(); 
    } 
} 

如果您對Java 7的或以上,你可以做同樣的與try-with-resources statement更優雅的方式。

最後,而不是catch (Exception e),你應該考慮無論是從方法拋出一個可能的(檢查)SQLException,或者如果你不想改變方法簽名,包裹SQLExceptionRuntimeException。一般來說,你不應該捕獲你不能(或不)處理你的方法的Exception。在這些情況下,最好讓問題冒泡到客戶端代碼而不是默默地忽略它。

+0

謝謝soooooo - 我會嘗試。我認爲我需要將它轉換爲一個字符串數組,因爲我有它的工作方式,但它抱怨說它是一個「List」而不是String Array。 – djcajun

+0

你可能意味着你需要一個'DestinationItem'數組(不是'String'數組)? –