This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Reading the record from the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Reading the record from the database" Watch "Reading the record from the database" New topic
Author

Reading the record from the database

Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hi all,
I have one doubt about reading the records from the database.
I have one class Database that reads the record from my .db file. And My Data class works with this Database object. Data class don�t read the records from the .db file directly it call the method of the Database class instance that reads the record. All access to the db file will be made from Database. Every record will be wrapped in the Record class.
I use also DataSchema, class that reads the header of the database and have some information about the length and names of the records� fields. All information about the length an field�s name will be built dynamically. But now I have one doubt how to read one records and write it to the object Record, how to make it more dynamically?
I have one method readRecordAt(int recNo) in the Database class.
With the following context:

And now I try to fill the fields of the Record with the data.


this will called the method setValue(int, String) from Record class:

the readDB(byte[]) reads the information from the database (RandomAccessFile) and convert the byte array to String.


How do you think about it?
I have another idea don�t use the Record object. And store every records as one Array of String.
How do you think what is the best way to do this?
Do you have other ideas? Are there another way to fill the Record object more dynamically?
Thanks a lot for your help and comments!


SCJP 1.4<br />SCJD 1.4 (in progress)
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
I decided to stay with the Object like a Record.
It will be a wrapper for String[] array after i call String[] read(int) from Data.

I will not use the fields like name, location... .
Advantage of this: dynamic. If the database will be changed i don't need to change the Record class and all classes that works with the records will not be changed.
How do you think about it?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Olena,

You still need to provide the required methods in your Data class, several of which require you to deal with String[].

Otherwise I would agree that this is better - I did something similar in my project, but at a much higher level (client side, in the model of my HMVC).

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Dear Andrew, thanks for your reply!

I have one doubt in my design.
I use also MVC. All database access methods (readAllRecords, find()) I call from my Controller with DataAdaptor. DataAdaptor wraps my SUN�s Data class. Data works with db file and implements all methods from DB Interface. I work with Data only from DataAdaptor.
Is it correct? Or do I need to work with Data directly?
I don't know whether it's true or not.

The problem is that I am not very strong in design.
And I have to build the table with all non-deleted records.
I have declared one method getAllRecords() in the DataAdaptor. DataAdaptor knows the number of records and takes all records from the db file in the loop by calling public String[] read(int recNo) throws RecordNotFoundException from Data. This method getAllRecords() will be called from Controller. But I cannot implement (extending DB Interface) this method getAllRecords() direct in Data.
Is it correct? Or do you have another idea? I am trying to find the correct decision, but I couldn�t.
Please, please, please correct and help me, thanks.

Olena
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Olena,
I use also MVC. All database access methods (readAllRecords, find()) I call from my Controller with DataAdaptor.


In a traditional MVC design, the Model interacts with the database, while the View displays the data and the Controller accepts input from the user and initiates the appropriate actions.

So I would not normally expect to see your Controller call DataAdaptor methods.

DataAdaptor wraps my SUN’s Data class. Data works with db file and implements all methods from DB Interface. I work with Data only from DataAdaptor.
Is it correct? Or do I need to work with Data directly?


There are two separate issues here (and I don't have a definitive answer for either of them ).
  • Whether your client software needs to be able to call methods corresponding to the Interface API you were given in your instructions, or whether they can call your wrapper / business methods.


  • There is a long discussion about this in "Should lock methods be callable by the client". I don't feel that the arguments for allowing clients to call business methods (thin client solution) were persuasive, but it certainly appears that Sun do not penalize candidates for taking this route.
  • If you choose to expose your provided interface to the client, where you can abstract that provided interface. I chose to have a factory on the client side, which my model called. This factory provided an instance of a class which implemented a client only interface, which abstracted the whole direct connection / networked connection. This is one location (amongst many) where you could also convert between business methods and client methods.


  • But you could also decide to keep the calls to the Data class methods all the way up to the Model level - there is no "one right way" to do this .

    Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Reading the record from the database
     
    Similar Threads
    threads writing to same file
    database lock
    suncertify.db.Data
    a question about the synchronization.
    Data layer design.