Yogesh,
> In my servlet I am trying to retrieve all the data from the database in the
> init( ) itself ,store the values in a Hashtable and close the connection, so
> that I dont have to make or break connection every time I want to query the
> database. Then I can use the stored data.
>
> My problem is that when I have a key-value pair like code - name, it works
> fine. But I retrieve all employee_code and project_code (var names) from the
> data base for a particular Date. Now I want to get all the employee_codes
> for a particular project_code.
The schema for your data is a bit too vague here for me to give
you specific advice. If you need key->value associations, hashtable
works fine. If you need to associate the key with a variety of
values, a quick hack (although not OO) is to create an object class
that has fields for each of the columns in your records, now store
each employee record as an object in the hashtable.
If you want to store many values (i.e. any employees under one
project code) per key, then store the many values in a vector and
store all of the vectors in the hashtable.
Remember that you can have multiple hashtables and/or vectors
pointing to the same record. So in general, think in terms of
converting the records into objects, but instead of using
primary/foreign key mapping strategies to connect them all together,
the way you would in a database, just store an object reference.
Here's an example; your data probably doesn't look exactly like
this, but it'll help illustrate the approach:
Let's say you have a table of employee records, each record
having employee ID number (which you use as a primary key), name,
salary, phone number, etc.
You also have a project table, which contains some details about
a project: project ID number (used as a primary key), project name,
total budget, currently used budget, status, etc.
You also have a "map" table which contains records that map
employees to projects and projects to employees (most projects have
many employees, and some employees work on several projects). Each
record has an employee ID number and a project ID number. When you
need to figure out which projects an employee is on, you select
records by employee. When you need to figure out which employees are
on a project, you select records by project.
To convert this to a set of java objects, you start by setting up
two classes; employee class and project class. Each class has
instance members equivalent to the record columns. Now you set up a
hashtable of employees keyed by employee ID, and a hashtable of
projects keyed by project ID. Now you can look up the project or
or the employee and get all the columns.
We still need to keep track of which employee belongs to which
projects, and vice versa. Assuming we're not too worried about data
redundancy and the like - we're recreating this set of objects
periodically, based on the data in the table - the easy approach is to
set up arrays of object references in the employee and project
objects.
So we go back and crack open the employee class and add an
instance member that will hold an array. At the last second, we
decide to be a bit sloppy and instead of going through all sorts of
gyrations to construct each array as a vector (which is like an array
but much easier to grow and shrink) we just leave it as a vector. Who
knows, we may later need to add code to dynamically modify these
vectors. Each element in the array will be a reference to a project
object. Then we go back and add a vector of employee objects to the
project class.
Then we add code that selects all the records in the "map" table
and loop through them; we take the employee ID, use it as the key in
the employee hashtable to get the employee object reference, do the
same for the project ID and the project hashtable. Now we have two
object references. We add the employee reference to the project
object's vector of employees, and we add the project reference to the
employee object's vector of projects.
Now, when we need to get at the projects for a given employee, we
just check the project vector in th employee object. If we build code
that lets us modify the status of an employee in a project or the
projects in an employee's records, we should make sure that it checks
both spots.
We can also go back and put in "lazy instantiation" - instead of
instantiating a vector when we create the employee, we have the
accessor methods - the methods on employee that add or remove things
from the vector - check to see if there's a vector there when we add a
project. If yes, it just adds it to that vector. If not, it
instantiates a vector and then adds it. We do the same for projects.
> I have figured out that Hashtable is not the right data structure
> for this purpose. If somebody could guide me on this matter, it
> would be of great help.
General advice: if you're using 1.2 or later, there are lots of
nifty new "collection" classes, some of which may better suit your
purposes. If you're using an earlier version, you might want to check
out Object Space's JGL for their collection classes.
Steven J. Owens
[EMAIL PROTECTED]
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html