I've got two classes: Subject and Subjects. Subject stores data containing data found in a database (id, name and abbreviation). Subjects makes a database connection and returns an ArrayList containing Subjects.
Subject.java:
public class Subject {
public int id;
public String subjectName;
public String subjectAbbr;
//and some getters and setters
}
Subjects.java:
public class Subjects {
public ArrayList getAllFromSubject() throws SQLException {
Database DB = new Database();
String query = "SELECT * FROM `subject`;";
//DB.exequteQuery returns a ResultSet with the data from the query.
ResultSet result = DB.executeQuery(query);
ArrayList<Subject> output = new ArrayList();
while (result.next()) {
Subject subject = new Subject();
subject.setId(result.getInt("id"));
subject.setSubjectName(result.getString("subjectname"));
subject.setSubjectAbbr(result.getString("subjectabbr"));
output.add(subject);
}
DB.closeExistingConnection();
return output;
}
}
I've got signup.jsp, containing a dropdown select menu and I want the user be able to choose from the abbrevations found in the database. So I can call Subjects.getAllFromSubject() and I get an ArrayList containing a number of Subjects. I'd like to display all those Subjects abbrevations in a <select>-menu. This can be done by a forEach loop, but I'm am not sure how to do that and how to get the abbrevation from a Subject.
Any help would be greatly appreciated!
public List<Subject> getAllFromSubject()or something alike. Really, that are important basics; and unless you understand such things, you better stay away from talking to databases or other advanced topics.