Getting multiple table schema and returning them, using one helper method












0












$begingroup$


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<ArrayList<?>> getAttributesAndGroups() {
ArrayList<ArrayList<?>> retList = new ArrayList<ArrayList<?>>();

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

ArrayList<Attribute> listAttrs = new ArrayList<Attribute>();
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<Group> listGroups = new ArrayList<Group>();
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<ArrayList<?>> lists = getAttributesAndGroups();

ArrayList<Attribute> listAttrs = (ArrayList<Attribute>) lists.get(ATTRIBUTES);
request.setAttribute("listAttrs", listAttrs);

ArrayList<Group> listGroups = (ArrayList<Group>) 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<capture#1-of ?> to ArrayList<Attribute> (ditto for the other line).



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









share







New contributor




user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    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<ArrayList<?>> getAttributesAndGroups() {
    ArrayList<ArrayList<?>> retList = new ArrayList<ArrayList<?>>();

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

    ArrayList<Attribute> listAttrs = new ArrayList<Attribute>();
    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<Group> listGroups = new ArrayList<Group>();
    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<ArrayList<?>> lists = getAttributesAndGroups();

    ArrayList<Attribute> listAttrs = (ArrayList<Attribute>) lists.get(ATTRIBUTES);
    request.setAttribute("listAttrs", listAttrs);

    ArrayList<Group> listGroups = (ArrayList<Group>) 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<capture#1-of ?> to ArrayList<Attribute> (ditto for the other line).



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









    share







    New contributor




    user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      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<ArrayList<?>> getAttributesAndGroups() {
      ArrayList<ArrayList<?>> retList = new ArrayList<ArrayList<?>>();

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

      ArrayList<Attribute> listAttrs = new ArrayList<Attribute>();
      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<Group> listGroups = new ArrayList<Group>();
      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<ArrayList<?>> lists = getAttributesAndGroups();

      ArrayList<Attribute> listAttrs = (ArrayList<Attribute>) lists.get(ATTRIBUTES);
      request.setAttribute("listAttrs", listAttrs);

      ArrayList<Group> listGroups = (ArrayList<Group>) 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<capture#1-of ?> to ArrayList<Attribute> (ditto for the other line).



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









      share







      New contributor




      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      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<ArrayList<?>> getAttributesAndGroups() {
      ArrayList<ArrayList<?>> retList = new ArrayList<ArrayList<?>>();

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

      ArrayList<Attribute> listAttrs = new ArrayList<Attribute>();
      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<Group> listGroups = new ArrayList<Group>();
      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<ArrayList<?>> lists = getAttributesAndGroups();

      ArrayList<Attribute> listAttrs = (ArrayList<Attribute>) lists.get(ATTRIBUTES);
      request.setAttribute("listAttrs", listAttrs);

      ArrayList<Group> listGroups = (ArrayList<Group>) 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<capture#1-of ?> to ArrayList<Attribute> (ditto for the other line).



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







      java





      share







      New contributor




      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      user10931326user10931326

      1




      1




      New contributor




      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user10931326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          user10931326 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211890%2fgetting-multiple-table-schema-and-returning-them-using-one-helper-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user10931326 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user10931326 is a new contributor. Be nice, and check out our Code of Conduct.













          user10931326 is a new contributor. Be nice, and check out our Code of Conduct.












          user10931326 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211890%2fgetting-multiple-table-schema-and-returning-them-using-one-helper-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin