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 3 of 3

Thread: retrieve data from database using Hashmap (multiple values in one key)

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question retrieve data from database using Hashmap (multiple values in one key)

    hi i want to retrieve all data from the specific field in database using hashmap.. unfortunately, only the last value was retrieve.. what will i do?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: retrieve data from database using Hashmap (multiple values in one key)

    what will i do?
    Fix the logic in your code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: retrieve data from database using Hashmap (multiple values in one key)

    You want to retrieve all data from a table in database, isn't it?
    For example:

    You have a table in database:
    table: Names
    id name
    --------------------------
    1 Adam
    2 Brian
    3 John
    4 Michael

    this code can retrieve all data from this table:

    ...
    Map<Integer, String> names = new HashMap<Integer, String>();
     
    ...
    Statement s = connection.createStatement();
    ResultSet r = s.executeQuery("SELECT * FROM Names");
     
    while(r.next()){
        Integer id = new Integer(r.getInt("id"));
        String name = new String(r.getString("name"));
     
        names.put(id, name);
    }
    ...
    names collection will contain all ids and names from Names table.
    Last edited by gaszabo; April 4th, 2012 at 06:00 AM.

Similar Threads

  1. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  2. Replies: 6
    Last Post: September 5th, 2011, 10:02 AM
  3. multiple access to same data in database
    By the light in forum JDBC & Databases
    Replies: 1
    Last Post: August 23rd, 2011, 10:19 AM
  4. Retrieve data from other table
    By kichkich in forum JDBC & Databases
    Replies: 2
    Last Post: June 23rd, 2011, 07:51 AM
  5. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:44 PM