Getting multiple table schema and returning them, using one helper method I want to get two distinct sets of schema from a database without having to create multiple Connection/PreparedStatement/ResultSet objects.

Currently, I have a helper method that queries the database, gets each individual schema, and then returns them together in one ArrayList - please refer to the code below.

private ArrayList&lt;ArrayList&lt;?&gt;&gt; getAttributesAndGroups() {
    ArrayList&lt;ArrayList&lt;?&gt;&gt; retList = new ArrayList&lt;ArrayList&lt;?&gt;&gt;();

    Connection conn = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(jdbcUrl, userId, password);

        ArrayList&lt;Attribute&gt; listAttrs = new ArrayList&lt;Attribute&gt;();
        String query = "select aid, aname from tbl_attributes order by aid";
        PreparedStatement ps = conn.prepareStatement(query);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int aid = rs.getInt(1);
            String aname = rs.getString(2);
            Attribute attr = new Attribute(aid, aname);
            listAttrs.add(attr);
        }
        retList.add(listAttrs);

        ArrayList&lt;Group&gt; listGroups = new ArrayList&lt;Group&gt;();
        query = "select gid, gname from tbl_groups order by gid";
        ps = conn.prepareStatement(query);
        rs = ps.executeQuery();
        while (rs.next()) {
            int gid = rs.getInt(1);
            String gname = rs.getString(2);
            Group grp = new Group(gid, gname);
            listGroups.add(grp);
        }
        retList.add(listGroups);

        rs.close();
        ps.close();

    } catch (Exception e) {
        log.error("Exception", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                //
            }
        }
    }

    return retList;
}


Where I call my helper method, I do the following:

    ArrayList&lt;ArrayList&lt;?&gt;&gt; lists = getAttributesAndGroups();

    ArrayList&lt;Attribute&gt; listAttrs = (ArrayList&lt;Attribute&gt;) lists.get(ATTRIBUTES);
    request.setAttribute("listAttrs", listAttrs);

    ArrayList&lt;Group&gt; listGroups = (ArrayList&lt;Group&gt;) lists.get(GROUPS);
    request.setAttribute("listGroups", listGroups);

    request.getRequestDispatcher("addUser.jsp").forward(request, response);


(ATTRIBUTES and GROUPS are simple integer constants representing 0 and 1 respectively.)

However, my code is giving me warnings of Type safety: Unchecked cast from ArrayList&lt;capture#1-of ?&gt; to ArrayList&lt;Attribute&gt; (ditto for the other line).

Is there a better approach for what I want to do?
Generics

When you say, ArrayList&lt;ArrayList&lt;?&gt;&gt;, you are saying that there is a type but that you don't know what it is.  So you write it as a ?.  

But in your code, you are putting ArrayList&lt;Attribute&gt; and ArrayList&lt;Group&gt; into the larger list.  These are not one type.  So it has to find a type that covers both Attribute and Group.  Absent both extending or implementing some common class or interface, that will be Object.  So ? is actually Object in your code.  

It is natural to think that ? is a wildcard that can match multiple things simultaneously.  But it is not.  It represents exactly one type at a time.  You just avoid having to say which type.  As such, I don't think that it is what you want to use here.  

Cast by element not collection

The general way to fix this warning on something like 


    ArrayList&lt;Attribute&gt; listAttrs = (ArrayList&lt;Attribute&gt;) lists.get(ATTRIBUTES);



would be something like 

    List&lt;Attribute&gt; listAttrs = new ArrayList&lt;&gt;();
    for (Object attr : lists.get(ATTRIBUTES)) {
        if (attr instanceof Attribute) {
            listAttrs.add((Attribute) attr);
        }
    }


You can often leave off the instanceof check because you know what the result should be.  

I changed from the implementation (ArrayList) to the interface (List), as is customary in Java when you aren't using implementation specific methods unavailable to the interface.  

But I wouldn't recommend that code for this specific problem.  You don't really need to cast here.  You control the code at both ends and in between.  Just write it without casts.  

Custom return type

The most direct way to fix this specific problem is to replace your return value with a custom class.  

class AttributesAndGroups {

    private List&lt;Attribute&gt; attributes;

    private List&lt;Group&gt; groups;

    public void setAttributes(List&lt;Attribute&gt; attributes) {
        this.attributes = attributes;
    }

    public List&lt;Attribute&gt; getAttributes() {
        return attributes;
    }

    public void setGroups(List&lt;Group&gt; groups) {
        this.groups = groups;
    }

    public List&lt;Group&gt; getGroups() {
        return groups;
    }

}


You can use this like 

        AttributesAndGroups result = new AttributesAndGroups();
        result.setAttributes(listAttrs);
        result.setGroups(listGroups);
        return result;


and in the caller 

        AttributesAndGroups lists = getAttributesAndGroups();
        List&lt;Attribute&gt; listAttrs = lists.getAttributes();
        List&lt;Group&gt; listGroups = lists.getGroups();


No casts (unchecked or otherwise).  

There's a lot of boilerplate code in the class.  It's possible to avoid that here.  

Pass to the method

Another alternative would be to pass the lists into the helper method rather than have the method initialize them.  

        List&lt;Attribute&gt; listAttrs = new ArrayList&lt;&gt;();
        List&lt;Group&gt; listGroups = new ArrayList&lt;&gt;();
        fillAttributesAndGroups(listAttrs, listGroups);
        request.setAttribute("listAttrs", listAttrs);
        request.setAttribute("listGroups", listGroups);


I changed the name to be more representative of the revised implementation.  

And then the method signature would be 

private void fillAttributesAndGroups(List&lt;Attribute&gt; listAttrs, List&lt;Group&gt; listGroups) {


Now just delete 


    ArrayList&lt;ArrayList&lt;?&gt;&gt; retList = new ArrayList&lt;ArrayList&lt;?&gt;&gt;();
        ArrayList&lt;Attribute&gt; listAttrs = new ArrayList&lt;Attribute&gt;();
        retList.add(listAttrs);
        ArrayList&lt;Group&gt; listGroups = new ArrayList&lt;Group&gt;();
        retList.add(listGroups);
    return retList;



And the remainder of your code should function normally.  

Again, no casts needed.  And this code is much terser, particularly if this is the only time you call it.  

Another alternative would be to pass request into the method, but I won't try to implement that here.  That would allow you to make the lists inside the method again.