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

[660 byte] By [h2opologirly69a] at [2007-11-27 6:43:34]
# 1
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.
bsampieria at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 2
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
h2opologirly69a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

hunter9000a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 4
> Use a Map with a name for the key, and a List of> account numbers for the value. Too clever! Thanks!
petes1234a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 5
> 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
kevjavaa at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 6

> > 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. :)

hunter9000a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 7

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?

h2opologirly69a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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

Hippolytea at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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?

hunter9000a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 10

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.

h2opologirly69a at 2007-7-12 18:14:29 > top of Java-index,Java Essentials,Java Programming...
# 11
Continued in other thread: http://forum.java.sun.com/thread.jspa?threadID=5181116
Hippolytea at 2007-7-12 18:14:30 > top of Java-index,Java Essentials,Java Programming...
# 12
thanks everyone.
h2opologirly69a at 2007-7-12 18:14:30 > top of Java-index,Java Essentials,Java Programming...