Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Setting Array values then loop through and set object field equal to array values
    text
    copied!<p>I have a pretty introductory question that I hope can get answered rather easily. </p> <p>Currently I am trying to write something that queries my DB, returns result set (list of account id's) and then sets those account id's into an array so that I can populate an object with the values.</p> <p>My object is going to be used as input into another method I have written but I can't figure out how I go about populating my account_id field on it with the values in my array that I returned in the result set. There doesn't seem to be any "setter" methods for lack of a better term on my array of type String.</p> <p>I was able to get the array to populate with acct_id's from the result set and print them out so I know that I do have information coming back.</p> <p>Below is the snippet I currently have, any help/improvements I could make would be greatly appreciated!</p> <hr> <pre><code>try { connection = DriverManager.getConnection(url, user, password); st = connection.createStatement(); rs = st.executeQuery(sql); List&lt;Long&gt; array = new ArrayList&lt;Long&gt;(); while (rs.next()) { array.add((long) rs.getLong("acct_id")); for (Integer i = 0; i &lt; array.size(); i++) { System.out.println(array.get(i)); GetSummaryRequest request = new GetSummaryRequest(); request.accountKey = new AccountDTO(array[i]); } } } catch (SQLException e) { System.out.println("Connection failed."); e.printStackTrace(); return; } if (rs.next()) { System.out.println(rs.getString(1)); } else System.out.print("Failed. Try again"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload