Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to get each value in array list MVC

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to get each value in array list MVC

    Hi Everyone!

    the log shows the correct values to my query in SQL Script and store it to my array list.
    But when I tried to retrieve the data in the controller, only the last value repeatedly displayed.

    My SQL Query script:
    select distinct (descr) from los_mca where status='A'

    Send back to my DAO:
    public List<RepPlanSummaryVO> getListAreas(RepPlanSummaryVO vo) {
    ....
    List<RepPlanSummaryVO> lst = new ArrayList<RepPlanSummaryVO>();

    .....
    rs = stmt.executeQuery();
    RepPlanSummaryVO areavo = new RepPlanSummaryVO();
    while (rs.next()) {
    logger.info("result: "+ rs.getString("DESCR"));
    areavo.setArea_abbv(rs.getString("DESCR"));
    lst.add(areavo);
    }
    ....

    Console logs:
    result: Antelope Valley
    result: Baldwin Park
    result: Downey
    result: Kern County
    result: Los Angeles
    result: Orange County
    result: Panorama City
    result: Riverside
    result: San Bernardino County
    result: San Diego
    result: South Bay
    result: West LA
    result: Woodland Hills

    Retrieve the "lst" in the controller:
    List<RepPlanSummaryVO> list = repByPlanSER.getListAreas(planVo);
    for(int i=0;i<list.size();i++) {
    logger.info("lstHeaderAreas " +i);
    RepPlanSummaryVO lblStr = list.get(i);
    logger.info("Areas --- " + lblStr);
    }

    Console logs:
    lstHeaderAreas 0
    Areas --- Woodland Hills
    lstHeaderAreas 1
    Areas --- Woodland Hills
    lstHeaderAreas 2
    Areas --- Woodland Hills
    lstHeaderAreas 3
    Areas --- Woodland Hills
    lstHeaderAreas 4
    Areas --- Woodland Hills
    lstHeaderAreas 5
    Areas --- Woodland Hills
    lstHeaderAreas 6
    Areas --- Woodland Hills
    lstHeaderAreas 7
    Areas --- Woodland Hills
    lstHeaderAreas 8
    Areas --- Woodland Hills
    lstHeaderAreas 9
    Areas --- Woodland Hills
    lstHeaderAreas 10
    Areas --- Woodland Hills
    lstHeaderAreas 11
    Areas --- Woodland Hills
    lstHeaderAreas 12
    Areas --- Woodland Hills

    I really can not pin point my mistake so, if someone every encounter this problem of mine, please help me. :'(

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to get each value in array list MVC

    It looks like the list contains multiple references to the same object that contains the last value it was set to.
    The list needs to have a new object for each value added to it. Create a new areavo for each new value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: how to get each value in array list MVC

    hahahahaha ... thanks for the advise!
    I found the culprit

    I should have put the areavo inside the while loop.

    rs = stmt.executeQuery();
    while (rs.next()) {
    RepPlanSummaryVO areavo = new RepPlanSummaryVO();
    logger.info("result: "+ rs.getString("DESCR"));
    areavo.setArea_abbv(rs.getString("DESCR"));
    lst.add(areavo);
    }
    Thanks Norm

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to get each value in array list MVC

    When posting code be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to get each value in array list MVC

    okay thanks noted

Similar Threads

  1. 3d list array
    By spoX in forum Object Oriented Programming
    Replies: 1
    Last Post: September 14th, 2014, 12:57 PM
  2. ARRAY LIST
    By stresstedout in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 9th, 2014, 01:56 PM
  3. Array List help
    By popnfresh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2013, 07:41 PM
  4. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  5. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM