我在javafx中有一个tableview,我希望它在tableview中显示来自数据库的所有数据但是当我运行程序时它只显示1行信息而不是6行,我需要的信息是我的代码
idTextField.setOnKeyPressed(new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent event)
{
if(event.getCode().equals(KeyCode.ENTER))
{
try
{
// Get a result set containing all data from test_table
PreparedStatement st = connection.prepareStatement("SELECT * FROM abrasaldo where grade_No = ?");
// For each row of the result set ...
int getData = Integer.parseInt(idTextField.getText());
st.setInt(1 , getData);
ResultSet rs = st.executeQuery();
if (rs.next())
{
ObservableList<StudentClass> datatable = FXCollections.observableArrayList();
for(int i = 1; 1 < rs.getMetaData().getColumnCount(); i++)
{
datatable.add(new StudentClass(rs.getInt("grade_No") , rs.getString("grade_Code"),
rs.getString("grade_Title"), rs.getString("grade_SubjectDesc"),
rs.getDouble("grade_Grade"), rs.getString("grade_DaySched"),
rs.getString("grade_TimeSched"), rs.getString("grade_Room"),
rs.getString("grade_Term"), rs.getDouble("grade_Unit"), rs.getString("grade_Faculty")));
}
table.setItems(datatable);
noDataLabel.setText("");
}
else
{
ObservableList<StudentClass> datatable = FXCollections.observableArrayList();
table.setItems(datatable);
noDataLabel.setText("No Data");
noDataLabel.setTextFill(Color.RED);
}
}
catch (NumberFormatException e)
{
ObservableList<StudentClass> datatable = FXCollections.observableArrayList();
table.setItems(datatable);
noDataLabel.setText("No Data");
noDataLabel.setTextFill(Color.RED);
}
catch (SQLException e)
{
noDataLabel.setText("No Data");
noDataLabel.setTextFill(Color.RED);
}
}
}
});
答案 0 :(得分:0)
也许尝试循环遍历结果集而不是使用单个if语句。结果集指针仅指向第一个元素,不会遍历所有记录
while(rs.next()){
datatable.add(new StudentClass(rs.getInt("grade_No") , rs.getString("grade_Code"),
rs.getString("grade_Title"), rs.getString("grade_SubjectDesc"),
rs.getDouble("grade_Grade"), rs.getString("grade_DaySched"),
rs.getString("grade_TimeSched"), rs.getString("grade_Room"),
rs.getString("grade_Term"), rs.getDouble("grade_Unit"), rs.getString("grade_Faculty")));
}
table.setItems(数据表);
答案 1 :(得分:0)
实际上,每次您进入rs.next()循环时,都会创建一个新的“数据表”。
ObservableList<StudentClass> datatable = FXCollections.observableArrayList();
只需将上述行移动到{if(rs.next())}循环之外,您就可以开始使用
。