Get your CodeRanch badge!*
The moose likes JDBC and the fly likes hashmap and jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "hashmap and jdbc" Watch "hashmap and jdbc" New topic

hashmap and jdbc

peter brews

Joined: Feb 11, 2002
Posts: 20
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( {
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());
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 565
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.

I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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 ]
I agree. Here's the link:
subject: hashmap and jdbc
Similar Threads
Trouble connecting to MS Access via ODBC
data mismatch error
hashmap and jdbc
an easy test question relating to Bean Management Persistent
Returning a 0 value error