Your Person class will need to have a Vector of addresses.
When/if you call add address, it adds one to the vector,
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
The database should contain an ID in the user table to tell it which addresses are associated OR the database can have a foreign key back to the user table OR you can have a table that does nothing but contain a foreign-key to the user table mapped to a foreign-key in the address table so you can still map users to addresses.
Here is a technique to save:
Let's say you have an ID field both on the user and the address classes.
1) Open a SQL transaction (if allowed)
2) Update the user, if ID exists and data has changed / Insert user if ID is empty
- If inserted, re-select to get the db generated ID
3) for each address with an address ID, update (if changed)
4) for each address without an address ID, insert
5) Commit the transaction
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
You must know the ID of the person.
If your database has IDs for the user or for the address + user,
you must know the ID and update the data based on that ID.
It really depends on the layout of your database.
I cannot tell you more than that without actually seeing the schema.
To insert, you will use the "INSERT INTO" statement.
to update, you will use the "UPDATE" statement.
Build SQL statements as strings based on the user ID or address ID.
You will need an open connection to the database.
http://en.wikipedia.org/wiki/Java_Database_Connectivity
I'm not sure which part is the problem at this point.
Do you need to know how to connect to the database for the update or insert?
Either way, I would not recommend putting database access code INSIDE the actual person class.
I would create a master as a collection of person objects and an archiver class for controlling the loading and storing of Person objects. That way, you'll have visibility to all necessary records without dealing with the one.
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
Question Answered as of 1 Year Ago by
thines01