0

我使用此代码从数据库中获取值,但只能获取一条记录,但我想将所有记录放入组合框中。

我的代码是:

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);
4

1 回答 1

1

问题似乎是您的变量 ol 仅包含您从 ResultsSet 中提取的最后一个对象。看来您打算做的事情如下:

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

} 

我不确定这是否完全符合您的要求,但希望这能说明为什么您的组合框中只有一个对象。

于 2012-06-15T17:13:42.390 回答