我想执行一个查询并将这些值添加到循环中的文本字段中。我想要这样的东西
String query = "select student_id,String firstName,lastName,address,phoneNumber,mobileNumber,fathersName,fathersOccupation,mothersName,nationality,qualification from student where student_id='" + jTextField5.getText().toString() + "'";
ResultSet rs = DBOptions.executeSQLQuery(query);
rs.next();
for(int i=0; i < 11; i++){
String s = rs.getString(i+1);
String field = "jTextField"+i+".setText(s)";//can you do something like this and use the string to add values to individual textFeilds?
}
我试过看ArrayList和其他方法,但他们似乎令人困惑......
将所有jtextField
存储在ArrayList
中,但确保它们按顺序插入
List<JTextField> fieldList = new ArrayList<JTextField>();
fieldList.add(jtextField1); //gets stored at index 0
fieldList.add(jtextField2); //gets stored at index 1
...
fieldList.add(jtextField10); //gets stored at index 10
然后在你的方法
for(int i=0; i < 11; i++){
String s = rs.getString(i+1);
fieldList.get(i).setText(s);
}