jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Many database-queries or one big vector?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Many database-queries or one big vector??" Watch "Many database-queries or one big vector??" New topic

Many database-queries or one big vector??

Tom Rodrigo

Joined: Dec 07, 2001
Posts: 26
Hi all,
I need to fill 4*4 = 16 JComboBoxes with data out of a DB2 database. I've figured out two ways to do this, but would like to read your suggestions for the better way...
I want to fill some JComboBoxes depending on the selected Items in other JComboBoxes.
To make it easier to understand let's say I want to visit 4 of my friends around the world, but don't know where to go...
So I first select a continent (stored in the first box) and get a list of the countries (in the second box) where I know the people. I select a country and get a list of cities (in the third box) where my friends live. I select a city and get a list of friends (in the fourth box) in this city.
For unknown reasons I want to visit exactly four of my friends, so I need these 4 boxes four times [in total: 16 JComboBoxes].
All JComboBox-items are stored in a database with something about 3000 rows of data in 5 tables. The queried values are of type int, char, string; the number of items will be no more than 10 for a single JComboBox.
I think there are two ways to fill the boxes, but I don't know which one is better:
Solution 1:
I query the database each time after selecting a value in order to get the items for the next JComboBox. By this way I will have to query my db many times, but it is very easy to put the retrieved values in the boxes.
Solution 2:
I query the database only once and put all data in several vectors, which will be added to one big vector, which is used to fill the JComboBoxes. It seems to be complicated to put all data in the right way in the big vector and also to get the items for a specific box, but this keeps my database-traffic low and maybe it is the most common way to do such a thing.

First I tried to fill the JComboBoxes using solution2, as it seems to be the more professional way, but I found out that it is not easy to get a specific value out of the big vector [and sort it] for I have to do something like this:

So now I prefer solution1, although I have to query my db many times, but it's a small db and also the retrieved data is not as that much.
What do you think? [even any better idea?]
Thanks in advance!
[ July 31, 2002: Message edited by: Tom Rodrigo ]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Lets see what the folks in the JDBC forum have to say.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Premkumar N

Joined: Apr 21, 2001
Posts: 22
Well the option that you have to take depends on the following factors

Does the data about locations .. change
frequently ? if so you have to go for
"querying the db at runtime"
Go for storing the values within the application.

Well .. u can use a combo of HashTabele with some sort of identifiable-primary-keys.. and the values as vectors ..
Country_ID country_name
Person_ID & Person_name
etc ...

GUI Logic ??
I guess u need to rework your GUI-showup-logic.. since the following statement doesn't sould like a thought-out-GUI-design
<< For unknown reasons I want to visit exactly four of my friends, so I need these 4 boxes four times [in total: 16 JComboBoxes >>

<a href="mailto:pondyprem@yahoo.com" rel="nofollow">pondyprem@yahoo.com</a> <br />Sr.Programmer Analyst ( Bioinformatics)<br />Monsanto Entrprises
I agree. Here's the link: http://aspose.com/file-tools
subject: Many database-queries or one big vector??