Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap and database problem

 
lyo Yashnoo
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone:
In a shopping cart application I place all the things to a HashMap.Finally,I want to place all the things that in HashMap to MySQL database.
I think the code (below) will work but it can't work property:
////////////////////////////////////////////////////////////
Set set=h.entrySet();
Iterator iter=set.iterator();
System.out.println("iter is:"+iter.toString());
while(iter.hasNext()){
String name=iter.next().toString();
String number=iter.next().toString();
System.out.println("Iter are :"+iter.next().toString());
this.insertMySQLdb(username,name,number,request);
}
...........................................................
How to get all the things from HashMap and place them to database? Thks
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, this is your problem:
String name=iter.next().toString();
String number=iter.next().toString();
iter.next returns an object. yes the object has toString but because what your getting is a string it is better to cast it!
String name = (String) iter.next();
in the ITerator are only the key strings! To get the value strings you need to do this:
String number = (String) h.get(name);
hope this helps
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops :roll: , also change this:
Set set=h.entrySet();
to
Set set=h.keySet();
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim's replies notwithstanding, let me see if I can straighten a few things out.
First of all, remember that every time you call next() on an Iterator, you consume one item from the HashMap, so that it's not available via that Iterator anymore. This means that when you call iter.next().toString() and print the result at the bottom of the loop, you're throwing one entry away. I'm pretty sure that's not what you want!
Second, calling entrySet() is a reasonable thing to do. If you do, then the items returned by the Iterator are instances of the Map.Entry class. A Map.Entry contains a single key/value pair. It looks to me as if your HashMap contains the names as the keys, and the numbers as the values, right? So the body of your loop should look like

See how I'm calling iter.next() just once per loop iteration.
Finally, the question of using toString() vs casting to String: I say go ahead and do it. The toString() method of String looks like

The nice thing about calling toString() instead of casting to String is that it's typesafe -- there will never be a ClassCastException. Of course, that's also the bad thing -- if you made a mistake and your Map contains something besides Strings, you wouldn't know it!
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep thats another way of doing it. my first reply i thought he was using a key set which is what i would use, then i realised this and the easiest way to correct it after my first reply was to change to a keyset
and as to the String thing, i think his code shows that he didn't know what he was getting wasn't a string, so if he was casting he would of got an error telling him this instead
i guess he can take his pick now
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this certainly doesn't have anything to do with JSP, and not much to do with JDBC either, so I'm moving this to the Java in General (Intermediate) forum.
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic