0

I have a pretty introductory question that I hope can get answered rather easily.

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.

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.

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.

Below is the snippet I currently have, any help/improvements I could make would be greatly appreciated!


try {

    connection = DriverManager.getConnection(url, user, password);
    st = connection.createStatement();
    rs = st.executeQuery(sql);

     List<Long> array = new ArrayList<Long>();


    while (rs.next()) {

        array.add((long) rs.getLong("acct_id"));  

        for (Integer i = 0; i < 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");

}  
9
  • Are you looking for something like this? request.accountKey = array[i]; Commented Sep 30, 2013 at 16:52
  • I tried this first but I am getting an issue with it saying it cannot convery the string type array to the object that my "GetSummaryRequest" is. Commented Sep 30, 2013 at 16:53
  • Is acct_id just one id or a list of ids separated by \n? Commented Sep 30, 2013 at 16:54
  • Well the query I am executing returns back acct_id's from a table, so I am not sure the default form in which that comes back, but yes it is a list of id's Commented Sep 30, 2013 at 16:56
  • If GetSummaryRequest.accountKey is a String, the sentence i put in the first comment is valid. Each position of array is a String, and you can use that to set/get the value: array[i] = "str"; or String otherStr = array[i]; Commented Sep 30, 2013 at 16:57

4 Answers 4

1

If my understanding is correct you may need the code below which is used to store all the account id's inside an array and you can use this to pass as a parameter to another method.

 ArrayList<GetSummaryRequest> array1=new ArrayList<GetSummaryRequest>(); 
 GetSummaryRequest request = new GetSummaryRequest();  

  while (rs.next()) 
  {
        request=new GetSummaryRequest();
        request.accountKey=rs.getString("acct_id");
        array1.add(request);            

    }

Now you have ArrayList of GetSummaryRequest with accountKey for each object.

Sign up to request clarification or add additional context in comments.

1 Comment

Placing the request=new GetSummaryRequest(); is what makes it work. If it's outside it just stores the first request.setItem("acct_id"); and repeats that over and over. Good answer
0

if i understand correctly accountkey is integer right? you can use Integer.parseint('your string!') class to convert the string to int. i hope it helps

1 Comment

just remember to pass a member of the array to parseint function. any way what kinda of object is accountkey?
0
  1. First of all , storing newline separated values in one column is not a good practice. This is against atomicity principle and will lead you to problems soon - ex., total number of accounts? how do you find it?

  2. Once you convince yourself with this, you can use only one loop like this

    List<GetSummaryRequest> summaryRequests = new ArrayList<GetSummaryRequest>();
    while (rs.next()) {
    
       String em = rs.getString("acct_id");
       GetSummaryRequest request = new GetSummaryRequest();
       request.accountKey = em; 
       summaryRequests.add(request);
    }
    return summaryRequests; 
    

Comments

0

Probably you need something like that:

// First you get all ids
List<String> accountsIds = new ArrayList<String>();
while (rs.next()) {
    accountsIds.add(rs.getString("acct_id"));
}

// Then iterate ids
for (String id : accountsIds) {
    GetSummaryRequest request = new GetSummaryRequest();
    request.accountKey = id;
}

Comments

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.