0

我想执行一个查询并将这些值添加到循环内的文本字段中。我想要这样的东西

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 和其他方式,但它们似乎令人困惑......

4

1 回答 1

0

把所有的东西都存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);
}
于 2013-05-07T05:18:00.333 回答