|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectexample.stock.Database
public abstract class Database
This class provides a wrapper class for the
RecordStore
class.
It allows for easier addition and deletion as well as better searching and
updating of records. The used recordIDs are kept in a Vector
which we use to access the indices of the records. The last used recordID
is stored at the beginning of the database and when the database is opened,
each recordID up to the last one used is tested to see if a record exists in
that position and a new Vector
of used recordIDs
is generated.
Field Summary | |
---|---|
protected javax.microedition.rms.RecordStore |
database
The database storing all the records and the last used recordID in position 1 |
protected int |
lastID
The last used ID in the database |
protected javax.microedition.rms.RecordComparator |
rc
The object used to compare two records and see if they are equal |
protected java.util.Vector |
recordIDs
The Vector of used recordIDs that are in the database |
Constructor Summary | |
---|---|
Database()
|
Method Summary | |
---|---|
void |
add(java.lang.String record)
Add the record to the database Add the recordID to our vector Update the database's last ID counter |
void |
cleanUp(java.lang.String fileName)
Remove the database from persistant storage |
void |
close()
Close the database and remove it from persistant storage if it is empty |
void |
delete(java.lang.String s)
Delete the record from the database and remove that recordID from the vector of used recordIDs |
int |
getNumRecords()
Return the number of records in the database |
void |
open(java.lang.String fileName)
Initializes the database and if it's not a new database, loads the recordID of the last record out of the first position in the RecordStore . |
java.lang.String |
search(java.lang.String s)
Find and return a record |
void |
update(java.lang.String s,
byte[] data)
Update the record with the name s with the data
in the byte[] array |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected volatile javax.microedition.rms.RecordStore database
protected volatile java.util.Vector recordIDs
Vector
of used recordIDs that are in the database
protected int lastID
protected javax.microedition.rms.RecordComparator rc
Constructor Detail |
---|
public Database()
Method Detail |
---|
public void open(java.lang.String fileName) throws javax.microedition.rms.RecordStoreNotFoundException, javax.microedition.rms.RecordStoreException, javax.microedition.rms.RecordStoreFullException
Initializes the database and if it's not a new database, loads the
recordID of the last record out of the first position in the
RecordStore
. We have stored it there when we closed the
database, then checks each ID from 1 to lastID to see if they exist in
the database and then add the IDs that exist to the recordIDs
Vector
fileName
- The name of the RecordStore
to open
RecordStoreNotFoundException
- is thrown if the
RecordStore
indicated with fileName
cannot be found
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
RecordStoreFullException
- is thrown when the
storage system is is full
javax.microedition.rms.RecordStoreNotFoundException
javax.microedition.rms.RecordStoreException
javax.microedition.rms.RecordStoreFullException
public void close() throws javax.microedition.rms.RecordStoreNotOpenException, javax.microedition.rms.RecordStoreException
Close the database and remove it from persistant storage if it is empty
RecordStoreNotOpenException
- is thrown when trying
to close a RecordStore
that is not open
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException
public void cleanUp(java.lang.String fileName) throws javax.microedition.rms.RecordStoreNotFoundException, javax.microedition.rms.RecordStoreException
Remove the database from persistant storage
fileName
- the name of the RecordStore
to remove
javax.microedition.rms.RecordStoreNotFoundException
javax.microedition.rms.RecordStoreException
public void add(java.lang.String record) throws javax.microedition.rms.RecordStoreNotOpenException, javax.microedition.rms.RecordStoreFullException, javax.microedition.rms.RecordStoreException
Add the record to the database
Add the recordID to our vector
Update the database's last ID counter
record
- The record data to be added to the database
RecordStoreNotOpenException
- is thrown when
trying to close a RecordStore
that is not open
RecordStoreFullException
- is thrown when the storage
system is is full
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreFullException
javax.microedition.rms.RecordStoreException
public void delete(java.lang.String s) throws javax.microedition.rms.RecordStoreNotOpenException, javax.microedition.rms.RecordStoreException
Delete the record from the database and remove that recordID from the vector of used recordIDs
s
- The name of the record to delete from the database
RecordStoreNotOpenException
- is thrown when trying
to close a RecordStore
that is not open
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException
public java.lang.String search(java.lang.String s) throws javax.microedition.rms.RecordStoreNotOpenException, javax.microedition.rms.RecordStoreException
Find and return a record
s
- The name of the record to search for
null
if not found
RecordStoreNotOpenException
- is thrown when trying
to close a RecordStore
that is not open
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException
public void update(java.lang.String s, byte[] data) throws javax.microedition.rms.RecordStoreNotOpenException, javax.microedition.rms.RecordStoreFullException, javax.microedition.rms.RecordStoreException
Update the record with the name s
with the data
in the byte[] array
s
- The name of the record to updatedata
- the new data to update the record with
RecordStoreNotOpenException
- is thrown when trying
to close a RecordStore
that is not open
RecordStoreFullException
- is thrown when the storage
system is is full
RecordStoreException
- is thrown when a general
exception occurs in a RecordStore
operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreFullException
javax.microedition.rms.RecordStoreException
public int getNumRecords() throws javax.microedition.rms.RecordStoreNotOpenException
Return the number of records in the database
RecordStoreNotOpenException
- is thrown when trying
to close a RecordStore
that is not open
javax.microedition.rms.RecordStoreNotOpenException
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |