Results 1 to 7 of 7
  1. #1
    mayub is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default add to arrayList in resultSet

    Hello Everyone:

    My ResultSet has 1 column and 2 rows with string values (names) ABC and XYZ. I want to add these values in an arrayList, so I did the following. I am showing only the relevant code

    int numberOfColumns = rsmd.getColumnCount();
    while(rs.next())
    {

    multiElement.add(rs.getObject(numberOfColumns));

    }

    when I print the arrayList I see:
    multiElement : [ABC]
    multiElement : [ABC, XYZ]
    multiElement : [ABC, XYZ, ABC]
    multiElement : [ABC, XYZ, ABC, XYZ]

    The correct output should be simply
    multiElement : [ABC, XYZ]

    How could I correct the output. Using multiElement.clear() in the while loop also did not help.

    Thanks for your help!

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    How are you printing the ArrayList?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    mayub is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default add to arrayList in ResultSet

    Within the while loop by using
    System.out.println("multiElement : " + multiElement) after adding to multiElement

  4. #4
    mayub is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default arrayList in ResultSet

    I have improved my output but still see names being appended multiple times. The problem is that the while loop executes for the number of rows it has, so the values keep getting appended. I don't know how to stop it after one iteration.

    while(rs.next())
    {

    names = rs.getString("names")
    multiElement.add(names)

    }

    this returns the output
    multiElement: [ABC, XYZ]
    multiElement: [ABC, XYZ, ABC, XYZ]

    which is much better than before but needs to stop after the first iteration. How can I do that?

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    1 column and 2 rows?, why you have 4 data

    . . col1 col2
    row1 X Y
    row2 A B

    am i wrong?

    i suggest to modify your sql statement... so that you can get 1 row data only,
    but if you sure your always want first row of resultset, you may try

    http://java.sun.com/javase/6/docs/ap...l#absolute(int)
    or
    use if statement instead of while loop
    Last edited by mtyoung; 04-29-2009 at 03:26 AM.

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If your ResultSet really does have 1 column and 2 rows then I see no reason why the while loop wouldn't terminate in the right place. The problem is probably with your queries.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    As long as we are all picking on you, don't use the metadata to determine which columns to retrieve from the ResultSet. Either hard-code the numbers, or hard-code the names. You are going to great lengths to always get the last column in the ResultSet.

    Try simplifying your program temporarily. Instead of putting the results in a list, just use System.out.println to display them. That will help you see if you are getting good data back from your query.

Similar Threads

  1. Help with Resultset
    By xxAlemanxx in forum JDBC
    Replies: 6
    Last Post: 06-24-2008, 12:09 PM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ResultSet to XML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:50 AM
  4. ResultSet to JTable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 10:01 AM
  5. ResultSet example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •