• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

hashmap and jdbc

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I my method in ejb I want to replace id with name.These two fileds are in the oracle table and The data of this table does not change very often.So to replace id with name I want to avoid to connect to database for each call.I want to store this information and use it and If i hardcode values there will be 1000 lines of code for hm_getname.put(123,xyz).If later the data in tables changes i want reload the stored data with new one o if the flag is set to true I would get the data from table . The problem with following piece of code is that the data in hashmap does'nt stay in betwween executions.
Could you please suggest how to achieve the result.
public String getName(String id,String fromdb)
HashMap hm_getname = new HashMap();
Connection con = null;
String querysql;
ResultSet rs = null;
String id = "";
String name="";
if( (fromdb.equals("true")) && (fromdb != null) )
try {

con = DriverManager.getConnection( "localhostxyz", "scott", "tiger");
Statement stmt = con.createStatement();
querysql=" SELECT id,name FROM emp" ;

while(rs.next()) {
id= rs.getString("id");
name =rs.getString("name");
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,
your hashmap is instantiated every time you call your method. Since the hashmap is a local variable in the method, as soon as you exit the method, you will lose the hashmap.
You should return the hashmap to the calling code and save it there somewhere, in the session if you're developing a web app.
Are you sure this is an EJB you are dealing with? I wouldn't use EJBs until you've got the basics worked out - there's loads more development overhead with EJB. Check out the EJB forum here.
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to do the same thing.
protected void load(ResultSet rs) throws SQLException{
if(sysIDCache != null)
sysIDCache = new java.util.HashMap();
When the data is reloaded for the class, the hashmap is recreated. but be sure the variable is a class variable and not a method variable. Actually the code where I put the values in the variable is gone I changed my technique a bit i guess.
Anyway, what you want to do is when you call your method first check for the key in the hashmap, if the key is not their, get the data from the database and put it into the hahsmap with the proper key.
Then check again for the key and return the proper object. I used to have code for this but appearantly I removed it.
Found it!

hehe, i notice an error in my code. if valut ==0 I should not put the key in the hashtable because its not in the database...
[ May 28, 2002: Message edited by: CL Gilbert ]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!