aspose file tools*
The moose likes Beginning Java and the fly likes Database to Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Database to Hashmap" Watch "Database to Hashmap" New topic
Author

Database to Hashmap

Mauro Mazzucco
Ranch Hand

Joined: Apr 26, 2013
Posts: 32

Lets say i have a database with 2 tables:
Car
Model

in Car i have the columns:
carID........carName:........modelID:
0001...........Toyota..............123
0002..............GM ...............456

in Model i have:
modelID..........modelName.............. modelYear
123................... Lexus...................... 2002
456....................Passat......................2005

I want to store the modelID as a key and the values will be a List with the other columns of the corresponding modelID, something like
Key=123 Values= 0001, Toyota, Lexus, 2002
Key=456 Values= 0002, GM, Passat, 2005




Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Greetings Mauro,

You can do that , it is possible. Just an idea, you can have a HashMap of keys as ModelID ( integer ) and values as a bean containing the corresponding values. Something like a Map<Integer, Bean> . Here the bean can contain your values.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

I'm not sure what your question is (what have you tried so far?), but I can see a couple of problems with that design.

Firstly, having a list where the first element is the ID, the second is the name, etc, isn't a good idea. A much better idea is to create a class with fields containing the values you want, and then have a map of ID to object.

Secondly, that doesn't seem to match your data design. The way you've described it suggests that for a given model ID there's going to be a single car ID and car name. Is that correct? If so your tables are badly designed. The table design (assuming carID is a primary key of the Car table, and modelID is a primary key of the Model table, which would make sense) suggests that you can have more than one Car for each Model. Which is the right interpretation?

Maybe you can describe what the data is supposed to represent and we can advise you further.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Yes, you can do that but why? Why do you need to drag those data out of the database when you can retrieve them easily as required?
I would suggest the key would be a LONGINT in SQL, so you would use a Long as the “K” in a Map, and a Car object as the “V”.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8159
    
  23

Mauro Mazzucco wrote:I want to store the modelID as a key and the values will be a List with the other columns of the corresponding modelID, something like
Key=123 Values= 0001, Toyota, Lexus, 2002
Key=456 Values= 0002, GM, Passat, 2005

My advice: DON'T.

Create a class for each table that looks like a single row, and then put instances of those into a Map.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mauro Mazzucco
Ranch Hand

Joined: Apr 26, 2013
Posts: 32

Matthew Brown wrote:I'm not sure what your question is (what have you tried so far?), but I can see a couple of problems with that design.

Firstly, having a list where the first element is the ID, the second is the name, etc, isn't a good idea. A much better idea is to create a class with fields containing the values you want, and then have a map of ID to object.

Secondly, that doesn't seem to match your data design. The way you've described it suggests that for a given model ID there's going to be a single car ID and car name. Is that correct? If so your tables are badly designed. The table design (assuming carID is a primary key of the Car table, and modelID is a primary key of the Model table, which would make sense) suggests that you can have more than one Car for each Model. Which is the right interpretation?

Maybe you can describe what the data is supposed to represent and we can advise you further.

Well i tried to simplify the problem but i think i made it worst, let me give you the full thing:

Your program must do:
1 - Read a carinfo txt file with the format
Data: MMddyyyy
ModelID: mmmmmm (Always 6 numbers)
CityID: ccccc (Always 5 numers)
carID: xxxxxxxxx (no fixed size)
eg.
mmDDyyyymmmmmmcccccxxxxxxxxxxxxxxxx
e.g
1005201200012210020565765765453543543
2 – Read a model CSV file
modelID
modelName
modelYear
brand
eg:
234211;A1;2013;AUDI
3 – Read a city file in CSV format
CityID
cityName
state
country
eg:
10220;NYC;NY;USA
4 – Store all info into a database.
5 – Create a window with 4 buttons:
a) By Date
b) By Model
c) By City
d) Details
each window shows the carinfo data grouped by whats on the button, each window must have a table that show all possible information
e.g.
ModelID: mmmmm
ModelName: A1
Model Year: 2013
Brand: Audi
Date: 01/01/1970
carID:xxxxxxxxxx
ModelID: mmmmm
ModelName: Lexus
Model Year: 2001
Brand: GM
Date 01/01/1999
carID:xxxxxxxxxx

So i did 3 tables, one for each file, and now i need to retrieve the data and show like that, so i thought i could use a HashMap to put all values related to a modelID in the Map and then display into a JTable
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
As far as possible, do all that sorting in the SQL. Database programs maintain indices which can sort millions of data in seconds, and they are specially optimised for that sort of work. This tutorial may help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database to Hashmap