Reputation: 18594
I want to fill several JTextFields with data retrieved from a database. I know, I could do something like:
while (rs.next()) {
tfName.setText(rs.getString("name"));
tfAge.setText(rs.getString("age"));
}
But is there a smarter approach with only one return
at the end of the select method?
Upvotes: 0
Views: 709
Reputation: 285403
You could put your JTextFields in a Map<String, JTextField>
and then use the database column key strings as keys for the text field map (say called fieldMap) and also have an array of these key strings. then you could do something like:
while (rs.next()) {
for (String key: KEY_STRINGS) {
fieldMap.get(key).setText(rs.getString(key));
}
}
Having said this, the while (rs.next())
bugs me a bit as I fear that the while will quickly loop through the database rows and will only truly display the last row of the database. This would need to be changed.
Upvotes: 1