This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes HashMap and database problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap and database problem" Watch "HashMap and database problem" New topic
Author

HashMap and database problem

lyo Yashnoo
Ranch Hand

Joined: Sep 15, 2003
Posts: 50
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

Joined: Oct 04, 2003
Posts: 541
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


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
oops :roll: , also change this:
Set set=h.entrySet();
to
Set set=h.keySet();
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: HashMap and database problem