Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashmap and jdbc

 
peter brews
Greenhorn
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 {

hm_getname.clear();
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection( "localhostxyz", "scott", "tiger");
Statement stmt = con.createStatement();
querysql=" SELECT id,name FROM emp" ;

rs=stmt.executeQuery(querysql);
while(rs.next()) {
id= rs.getString("id");
name =rs.getString("name");
hm_getname.put(id,name);
}
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
}
return((String)hm_getname.get(id));
}
Thanks,
peter
 
Adam Hardy
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.
Adam
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate 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{
super.load(rs);
if(sysIDCache != null)
sysIDCache.clear();
else
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic