2012-12-30 153 views
0

我有用於生成數據中心標識和數據中心名稱的Java對象。如何從Java對象獲取元素

private List<listDatacentersObj> listDatacenters; 

public void initListDatacenters() throws SQLException { 
    // Generate List of Datacenters 
    listDatacenters = new ArrayList<>(); 

    if (ds == null) { 
     throw new SQLException("Can't get data source"); 
    } 
    /* Initialize a connection to Oracle */ 
    Connection conn = ds.getConnection(); 

    if (conn == null) { 
     throw new SQLException("Can't get database connection"); 
    } 
    /* With SQL statement get all settings and values */ 
    PreparedStatement ps = conn.prepareStatement("select y.componentstatsid, y.name " 
      + "FROM component x, componentstats y where x.componentstatsid = y.componentstatsid and y.componenttypeid = 1000"); 

    try { 
     //get data from database 
     ResultSet result = ps.executeQuery(); 
     while (result.next()) { 
      /* Put the the data from Oracle into Hash Map */ 
      listDatacenters.add(new listDatacentersObj(result.getInt("COMPONENTSTATSID"), result.getString("NAME"))); 

     } 
    } finally { 
     ps.close(); 
     conn.close(); 
    } 
} 

public class listDatacentersObj { 

    private int datacenterid; 
    private String datacentername; 

    public listDatacentersObj(int datacenterid, String datacentername){ 

     this.datacenterid = datacenterid; 
     this.datacentername = datacentername; 

    } 

    public int getDatacenterid() { 
     return datacenterid; 
    } 

    public void setDatacenterid(int datacenterid) { 
     this.datacenterid = datacenterid; 
    } 

    public String getDatacentername() { 
     return datacentername; 
    } 

    public void setDatacentername(String datacentername) { 
     this.datacentername = datacentername; 
    } 
      @Override 
    public String toString() 
    { 
     return datacentername; 
    } 

} 

// Get the list with Datacenters 
public List<listDatacentersObj> getlistDatacenters() throws SQLException { 
    // Get the list of Datacenters from Oracle 

    return listDatacenters; 
} 

的問題是我如何使用數據中心的關鍵數據中心獲取名。與hashmap類似,我想根據key獲取值。

+2

你爲什麼不只是使用一個HashMap呢? – Mads

+2

請遵循Java命名約定,並使類名稱以大寫字母開頭,以便更好地讀取代碼。 – jlordo

回答

2

好吧,如果你的關鍵是componentstatsid,然後就在HashMap檢索listDatacentersObj對象存儲爲:

// private List<listDatacentersObj> listDatacenters; use the below map instead of this list 
private Map<Integer, listDatacentersObj> listDatacenters = 
         new HashMap<Integer, listDatacentersObj>(); 

... 

public void initListDatacenters() throws SQLException { 

    ... 

    try { 
      //get data from database 
      ResultSet result = ps.executeQuery(); 
      while (result.next()) { 
       /* Put the the data from Oracle into Hash Map */ 
       final int id = result.getInt("COMPONENTSTATSID"); 
       listDatacenters.put(id, new listDatacentersObj(id, result.getString("NAME"))); 

      } 
     } finally { 
      ps.close(); 
      conn.close(); 
     } 
    } 
} 
+2

你仍然可以通過使用'map.getValues()' – Bohemian

+0

我有其他更簡單的解決方案嗎? –

+0

它很簡單。你想在一段時間內通過某個鍵引用 - 使用哈希映射。 – yair