This part of the project is to be implemented individually.
One way to address some of the problems with lightweight persistence
is to use an SQL database to store objects. This offers the advantages
of using a relational database (transparency, control, data sharing,
etc.) while preserving the object model. To do this, you will define
a set of methods that will be implemented for every object that you will
store in the database:
objID Is the object ID of the
object to read in.
db Is the
handle to the opened SQL database.
hm Is the
HashMap of all objects read in. This is how we keep track of which
objects have been created already.
NotInDBException Is thrown when the
object is not found in the database.
The readSQL method reads data from the database for a particular
object, identified by that object's objID. Data are read from
one or more relations through the JDBC interface. The data are put into
the appropriate fields of the object. If the object contains
references to other objects, those objects must be created and initialized
from the database through their readSQL methods. The HashMap
implements (objID,object) pairs to keep track of which objects have
already been created and initialized. Re-initializing an already-initialized
object from the DB is not a problem, but only one object must be created
for each objID.
db Is
the handle to the opened SQL database.
The writeSQL method writes data from the object out to the
database. Data are inserted or replaced in tables through the JDBC
interface. If the object contains references to other objects, those
objects are written out as well through their writeSQL methods
(it should behave like lightweight persistence).
The removeSQL method removes the persistent object data from
the MySQL database. All tables in which this object's objID appears
as part of the primary key must be updated. This method does not
enforce
cleanup of references to this object from other database entries, responsibility
for that lies within the application.
Before compiling, you must edit SqlTest.java and
change the following line to read:
Where dbname is your assigned MySQL database, username is your MySQL userid, and password is the MySQL password (see class newsgroup for details).
The fix_sqltest script will make this change for you if you run it with your email userid in the same directory as SqlTest.java:
Do all of the following: