It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DB File Insert Values 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 "DB File Insert Values" Watch "DB File Insert Values" New topic
Author

DB File Insert Values

Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Guys I just started with the assignment. I have been assigned the urlybird assignment. I was thinking in order to start off, i will start off with fetching and inserting data in the .db file. If you guys think there is some better way to start, please let me know. But i thought, once I know how I am supposed to insert values, I can then design the rest of the application.

To just give inserting a try, I tried using the program posted on this forum. I wanted to know if i was on the right track. The given description file says that the header will give us the length of each field.
So i was thinking, in order to insert a string value for each record in the database, I will format the string using String.format("%"+lengthoffield+"s",value_to_insert); Then i will convert this into a byte array using String.getBytes().

I wanted to know if i was on the right approach or was there an easier way to do so.... morever, if i try to do so using regular strings, i get a missing character in the last field of the last record. Wanted to know if I was the only person with such problems.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Gaurav,


Check the SCJD Faq and look for Roberto Perillo's Database File Reader program.


HTH,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

I would suggest writing your own little program to read the database file based on the data file Format (see your instructions). Or you could use Roberto's database file reader (which you'll can find in the ScjdFaq). This will give you a clear understanding of the base of your application, the database file. And then you can start from there.
And as a final remark: you don't have to insert any values into your database file, because it already contains everything you'll need. Don't forget to save an original copy of your database file, because you have to submit an original, unchanged version of the database file


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Roel De Nijs wrote:I would suggest writing your own little program to read the database file based on the data file Format (see your instructions). Or you could use
And as a final remark: you don't have to insert any values into your database file, because it already contains everything you'll need. Don't forget to save an original copy of your database file, because you have to submit an original, unchanged version of the database file


Wouldnt the following method from the interface write a record to the database? assuming there are no deleted entries

// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
public int create(String[] data) throws DuplicateKeyException;
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Yes, of course! But I'm convinced you'll 1st have to get familiar with your database file and its structure, before you can add records to the database file through the create-method. So I created my own database file reader (similar to Roberto's), because his tool was not available at that moment. And then I started with implementing the read-method, it's the easiest one Then I did the delete-method, followed by the update-method and finally the create-method.

And I tested each method implementation with running JUnit tests for each possible situation (success, failure, wrong parameters,...)
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
i had a small doubt. In urlybird While reading the file, the first part of the db file contains metadata about the size of each field, number of fields, magic cookie etc. Then the actual records start. The first part remains constant for this db file. So if i had to add a record, or delete, i dont actually need to parse through the metadata.
I was wondering, would it be a good design if i had a constant number of bytes which i can jump over during fetch/read/add operations? The disadvantage would be , in the real world, if someone changed the number of fields, my logic would collapse(since the size of the metadata would increase). But do i have to take that into consideration against performance? Or do i assume the db file has a constant structure no matter what. Another idea i thought was i could calculate the size of the metadata everytime i create an object of data.java, the data access component. I wanted a feed back as to what others might have thought or done in my place
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

This decision is just yours: any option you choose will be fine as long as you argument this decision in your choices.txt file.

I read the database schema dynamically and kept a number, which indicates the start of the record section. So it's not a real constant and could change if fields are added, removed, size is extended,... But I know for example Roberto Perillo used all constants to navigate through the records and the database file.

It's up to you

Good luck!
Kind regards,
Roel
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
one more design question. I wanted to know your thoughts. The interface for the db requires me to write two methods.
1) Read(int rec no) to read the record which returns the record as a string array
2) search (string[[ criteria) which will search based on the criteria and which effectively scans the enttire file.

As far as read is concerned, i start from the beginning of the record start and read all records untill i reach the record number which i am supposed to return and return the values.

My qustion was, in order to reduce code redundancy, is it a good idea to have the search method call the Read method? Because in searhc, i am effectively reading every record. However, since i reset the starter, the complexity will increase exponentially(O!) to be precise.

I am thinking of refactoring the code to achieve both, but in case i have to choose, i wanted to know if either seems to be a decent option for you guys
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Hi Gaurav,

I think your read-method should benefit from your random access database file. So based on the record number you can calculate on which position that record starts and you know from the database structure how big a record is. So a record can be read using:


Don't forget these 2 actions should be performed in an atomic operation.

Kind regards,
Roel
 
wood burning stoves
 
subject: DB File Insert Values
 
Similar Threads
Reading Record by Rec Number
connection to db file
still stuck in database
What about records
some questions regarding database?