2012-06-15 62 views
0

我使用此代碼從數據庫獲取值,但只能獲得一條記錄,但我想將所有記錄放入組合框。javafx 2.1 combobox

我的代碼是:

try { 
    stm = db.con.createStatement(); 

    rs = stm.executeQuery("select code from accounts"); 

    while (rs.next()) { 
     for (int i = 1; i <= 5; i++) { 
      ol = rs.getObject(i); 
     } 

    } 
} catch (SQLException sqlException) { 
} 

ObservableList<Object> options = FXCollections.observableArrayList(ol); 

final ComboBox code = new ComboBox(); 

code.getItems().addAll(options); 

回答

1

這個問題似乎是您的變量醇僅僅包含您從ResultsSet提取的最後一個對象。看來你究竟想要做的是類似以下內容:

ArrayList<Object> ol = new ArrayList<Object>(); 
while (rs.next()) { 
    for (int i = 1; i <= 5; i++) { 
     ol.add(rs.getObject(i)); 
    } 

} 

我不知道,如果這不正是你想要什麼,但希望這說明了爲什麼你只得到在組合框中一個對象。