aspose file tools*
The moose likes Beginning Java and the fly likes how to append the values retrieved from the database to the hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to append the values retrieved from the database to the hashmap" Watch "how to append the values retrieved from the database to the hashmap" New topic
Author

how to append the values retrieved from the database to the hashmap

Abhijit Ajansondkar
Greenhorn

Joined: Jul 13, 2012
Posts: 10
Hi, I have retrieved values from database .Now I want to append those values to the hashmap. So please suggest me the logic. This is my code .
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


A Hashmap stores values as [key - value] pairs, where distinct keys act as 'references' for the values stored.

You should decide how you want to store the values. Usually for storing an object in a Map we store it as the value along with the ID for this element as the key.
i.e. hashmap.add(id, object);

Why is the requirement that the elements have to be stored in a Map? As they appear to be a heterogeneous collection I would think of a different collection (unless they are connected in some different way).

Cheers,
Rajkamal.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
You can't append to a HashMap. They have no order so appending is meaningless.
You can add to a HashMap. The best way to do this would be to create a class to hold all the data you retrieve from the database, create an instance of this class for each batch of data that you retrieve and then put this instance into the HashMap using a suitable key value which needs to be unique.


Joanne
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

A couple of tips. Don't return a HashMap, return a Map. This makes it easier to change the implementation later to say, a TreeMap.

You are currently creating a new HashMap for every record. I'm pretty sure that's not what you want. Move that outside of the loop.

Move your return outside the loop unless you want to return after only one record.

Finally, I would create a bean to represent your records. Populate a bean for each record and then add those beans to a List or Map, whichever is appropriate for your requirements.


"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Jk Robbins wrote: . . . create a bean to represent your records. Populate a bean for each record and then add those beans to a List or Map, whichever is appropriate for your requirements.
Good idea, but I think maybe you should explain a little about what a bean is; OP might not have heard of it.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

Good point. Joanne mentioned creating a class to hold the data. That's what a bean (JavaBean) is. It contains fields and setters and getters, nothing else. In this case it would look something like this:



In the loop you'll do something like this:


I may have already told you more than I should have, so I'll stop here and let you take your next shot at it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to append the values retrieved from the database to the hashmap
 
Similar Threads
Navigating data using JDBC and SQLServer 2005
Kindly help me with this checkbox issue
hashmap and jdbc
How to take MySql DB backup from java
Retrieve values using hashmap