0

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!

3
  • Unrelated: you want to read about not using raw types with generics. For example, a much better return type for your method would look like : 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. Commented Dec 23, 2016 at 12:20
  • Do you use any framework with your jsp ? Commented Dec 23, 2016 at 12:22
  • +ChristosLoupasdakis my jsp is nothing more than a file with HTML and some Javascript Commented Dec 23, 2016 at 12:24

1 Answer 1

1

I hope this help:

index.jsp

<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="jvanamerongen.example.Subjects"%>
<%@page import="jvanamerongen.example.Subject"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <% Subjects list = new Subjects(); %>
        <select>
            <% for(Subject s : list.getAllFromSubject()) { %>
                <option value="<% out.print(s.getId()); %>"><% out.print(s.getSubjectAbbr()); %></option>
            <%}%>
        </select>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

8 Comments

I will try this in a minute, but now you're accessing subject.id directly, is it possible to just use a getter?
Thank you for your effort, but unfortunately your code doesn't seem to work. I added the line <%@page import="business.*"%> to include both classes, but without success
<%@page import="business.Subjects"%> <% Subjects obj = new Subjects(); %>
I'm sorry to tell you that this doesn't solve the problem either. My IDE does say that obj is not used.
What is your IDE?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.