Quick Hashtable question
Hello all, before hand thank you if you are able to assist. My question is that I have a two DB (SQL) strings 1 and 2. 1 has people and 2 has the account of the person. so data is like this...
personaccount
alex20001
alex56547
maria879787
maria 65464
maria 789789
I want to put then in a hashtable where with the associated string alex then his accounts pop up or maria her accounts pop up (and if i get this then i can do a reverse hash of that).
Is there way to put the in easily in a method or do I have to do...
table.put("alex","accountId"); ?
Message was edited by:
h2opologirly69
You have to call table.put(key, value);You can certainly make a method that takes the result set and calls that repeatedly to put the values. But Hashtable doesn't have a way to dump DB results into it.
well maybe the better question is a JDBC how can i parse that data to have only one alex be associated to many accounts ?Message was edited by: h2opologirly69
> well maybe the better question is a JDBC how can i
> parse that data to have only one alex be associated
> to many accounts ?
>
> Message was edited by:
> h2opologirly69
Use a Map with a name for the key, and a List of account numbers for the value. When you add a database record to the map, first check if the key already exists in the map. If it doesn't, then put the key and value (a new List with the account number added to it) in the map. If it does, retreive the List associated with the key, and add the account to it.
> Use a Map with a name for the key, and a List of> account numbers for the value. Too clever! Thanks!
> Use a Map with a name for the key, and a List of> account numbers for the value. Here's a tutorial. Toward the botttom of the page, it says "Multimaps": http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html
> > Use a Map with a name for the key, and a List of
> > account numbers for the value.
>
> Here's a tutorial. Toward the botttom of the page,
> it says "Multimaps":
> http://java.sun.com/docs/books/tutorial/collections/in
> terfaces/map.html
Cool, I didn't know there was a name for that, thanks. :)
so basicaly i will have to
/some methodname
String query=" exec procudre that brings back name and accountnumber";
ResultSet results=statement.executeQuery(query);
while (results.next())
{
traderID=results.getString(1);
String personID=results.getString(2);
System.out.println(personID);
System.out.println(accountID);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
return//what;
what I do next?
> what I do next?
Get cracking on that multimap?
And I would break the problem into smaller parts:
1. Verify that your SQL works, that you can execute that
query and just print the results out to see it's working.
2. With no database in sight, get your multimap working,
by feeding in some canned values and seeing if it builds
the data structure correctly.
After I and 2 have been tested separately and are working,
*then* think about putting them together. This way may
seem like more work, but it's really less work and the
smart way to go.
Message was edited by:
Hippolyte
> so basicaly i will have to
>
> /> some methodname
> String query=" exec procudre that brings back name
> and accountnumber";
>
> ultSet results=statement.executeQuery(query);
>
>while (results.next())
>
>{
>traderID=results.getString(1);
>String personID=results.getString(2);
>
> System.out.println(personID);
> System.out.println(accountID);
> // now that you have the key and value, add them to a Map.
> // The link kevjava posted has a good example
>}
> catch (SQLException e1) {
>
> e1.printStackTrace();
> }
>
> return//what;
>
>
>
> what I do next?
I get an error in my code for some reason on this any idea?
static HashMap<ArrayList[], ArrayList[]> idents=new HashMap<ArrayList[],ArrayList[]>();
public static //what i put here? fillSymbol()
{
Connection con=getConnection();
ArrayList listofsymbols = null;
try {
statement = con.createStatement();
String query=" exec a query @flag='id'";
ArrayList listofaccntid=null;
listofaccntid=new ArrayList();
ResultSet results=statement.executeQuery(query);
// System.out.println(results);
//listofsymbols=new ArrayList();
while (results.next())
{
traderID=results.getString(1);
String accountID=results.getString(2);
//System.out.println(traderID);
//System.out.println(accountID);
listofsymbols.add(traderID);
listofaccntid.add(accountID);
idents.put(listofsymbols,listofaccntid);
//get error here?
}
} catch (SQLException e1) {
e1.printStackTrace();
}
return //somthing here.
Continued in other thread: http://forum.java.sun.com/thread.jspa?threadID=5181116