for (String s: subjList)
// test if one entry of subjList matches the current row’s “subject_name” value,
// repeatedly asking for “subject_name” from current rs
if (s.contains(rs.getString("subject_name")))
// if so add current value to list being iterated
subjList.add(rs.getString("subject_name"));
newSub.addAll(subjList);
// If I follow the intent correctly (match subjects to tutor) I believe the following would be more effective
String rsSubject = rs.getString("subject_name");
if (subjList.contains(rsSubject)) {
newSub.add(rsSubject);
}
// However your sql seems to be asking for all tutors in a given zipcode and what subjects they handle. You might want to at least order the return set by tutor to deal with one tutor over several result rows.
Alternatively you could group subjects into an array per tutor:
String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, array_agg(t2.subject_name) FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?)) group by t.tutor_contact_no,t.zipcode,t.tutor_id”;
and grab the array of tutor’s subjects.
Further your responce doesn’t tie specific tutor to specific subjects. Perhaps you can add List<String>subjects to your “tutor” class?
}
myList.add(t);
request.setAttribute("tutors", myList);
request.setAttribute("sub", newSub);
}
I hope this helps.