Results 1 to 11 of 11
  1. #1
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default MySQL results to ArrayLisy

    Hi folks,

    I'm writing a reasonabily basic program which uses a basic MVC pattern.

    In my model I'm retreiving information (email and password) from a MySQL database which I'd like to store in an ArrayList (I'm under the impression that it's better to store the data in an ArrayList rather than anything else), this array list is then going to be accessed from the Controller which will update a JTable within the View.

    I'm unsure as to how to add the data to the ArrayList as I'm looping through the data and wondered whether anyone here could help me?

    My code looks like this:

    Java Code:
    ArrayList resList = new ArrayList();
    
    while(rs.next()){
    
     String email = rs.getString(2);
     String password = rs.getString(3);
    				
     /*Unsure as to how to add the above email and password to an ArrayList*/
    
    }
    Thank you in advance!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by Jojomofoman View Post
    /*Unsure as to how to add the above email and password to an ArrayList*/
    You would write a class that can hold the data. Create an instance of that class and add that to the ArrayList.

  3. #3
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You would write a class that can hold the data. Create an instance of that class and add that to the ArrayList.
    Ah, I see; so it's not as straight forward as something like:

    Java Code:
    resultList.add(email, password);
    Instead it's:

    Java Code:
    resultList.add(new Object[]{email, password});
    Last edited by Jojomofoman; 02-20-2011 at 10:59 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    You could try it but way are you trying to create an array?

  5. #5
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You could try it but way are you trying to create an array?
    Do you mean 'why'? If so, I'm adding it to an array because I need to access it from the Controller part of the program (I'm using MVC pattern for it). I can't loop through the data populating the JTable as the JTable is in another class which is inaccessible from the aforementioned piece of code.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Yes I did mean "why". Stupid fingers. But why do you add the data to an array and then add the array to the ArrayList. I would think you only need the array or ArrayList not both.

  7. #7
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Yes I did mean "why". Stupid fingers. But why do you add the data to an array and then add the array to the ArrayList. I would think you only need the array or ArrayList not both.
    I suffer from the fat finger curse sometimes too :P

    Where do I add the data to an array? Do you mean the MySQL result set? If so, I thought it would easiert to put the data into an ArrayList as I've never really tried the other way around as I'm quite new to Java.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Java Code:
    resultList.add(new Object[]{email, password});
    Do you see those square brackets?

  9. #9
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    resultList.add(new Object[]{email, password});
    Do you see those square brackets?

    Oh, I see what you mean.

    I did it like that because I thought that was how you added data (in the format that it is currently in, ie: 2 columns per row) to an ArrayList.

    I've obviously completely misunderstood you when you said:

    You would write a class that can hold the data. Create an instance of that class and add that to the ArrayList.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Java Code:
    class Foo {
        // holds email and password
    }
    
    list.add(new Foo(.....));

  11. #11
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    I see, thanks a lot for spending the time to help me Junky, it's appreciated. I've got it all working exactly how you mentioned and have learnt a great deal about ArrayLists in the process :) *rep*

Similar Threads

  1. Output to results file help!
    By javanator in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 01:59 AM
  2. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 01:03 AM
  3. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  4. how to take query results to a jlist!!
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-07-2008, 10:45 AM
  5. date and calender not getting the right results
    By valoyivd in forum New To Java
    Replies: 4
    Last Post: 04-14-2008, 11:51 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
  •