Meaningless Drivel is fun!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Writing to .DB with RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Writing to .DB with RandomAccessFile" Watch "Writing to .DB with RandomAccessFile" New topic
Author

Writing to .DB with RandomAccessFile

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Hello Guys.. I am doing periodic writing to .db

anyone pass using period update?

My flow would be like this

1) truncate the file using raf.setlength.(header size)
2) start writing all the records from cache into DB using raf.write()


It says All text values, and all fields (which are text), contain only 9 bit charactors, null terminated if less than maxium length for the field. May I know how to do the writing!! last hurdle for db layer.. please help!
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

The writing is quite simple. Just use RandomAccessFile. Move the file pointer past all the schema information and begin writing your records.

Most people I think ignored the null terminating character and simply padded all fields (where required) so ensure that each field takes up the max space allowed by the schema for that field.


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Sean Keane wrote:The writing is quite simple. Just use RandomAccessFile. Move the file pointer past all the schema information and begin writing your records.

Most people I think ignored the null terminating character and simply padded all fields (where required) so ensure that each field takes up the max space allowed by the schema for that field.


how do I do this padding? any sample code anywhere?

Anyone pass with my current implementation of periodic update before?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Ixus See wrote:how do I do this padding? any sample code anywhere?


To pad, simply add empty spaces. If you look at the code from Andrew Monkhouse book you'll probably see sample code see here.

Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Sean Keane wrote:
Ixus See wrote:how do I do this padding? any sample code anywhere?


To pad, simply add empty spaces. If you look at the code from Andrew Monkhouse book you'll probably see sample code see here.



oic thanks, so did anyone pass by using period update? If not I rather do code change =X
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

I don't understand what a period update is.

But I know people passed with:

1) Storing records in a cache.
2) Completely overwriting the records section of the data file with the contents of the cache.
3) Padding all fields (when required) with empty spaces to fill out the max length of the field for a record.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Oh, I think I understand what you mean by period update now! You are periodically writing the contents of the cache to the data file.

I have read that people passed with this approach. But people also passed with the approach of only writing the contents of the cache to the file on shutdown.

I would go with the approach of only writing the contents of the cache to the data file on shutdown because you aren't going to get any extra marks for adding functionality for periodicly writing the cache to the file, so why bother?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

A periodic update is not needed (I passed without it), but should at least be mentioned in your choices.txt to solve one of the drawbacks of the approach of using a record cache (huge data loss if e.g. server crashes)


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

Joined: Jul 17, 2011
Posts: 160
Sean Keane wrote:Oh, I think I understand what you mean by period update now! You are periodically writing the contents of the cache to the data file.

I have read that people passed with this approach. But people also passed with the approach of only writing the contents of the cache to the file on shutdown.

I would go with the approach of only writing the contents of the cache to the data file on shutdown because you aren't going to get any extra marks for adding functionality for periodicly writing the cache to the file, so why bother?


nvm I just change it to updating that individual record =x

by the way

I keep getting square boxes when i read the database after writing..

example elephant[][][][]

do i need to add black space to the left over strings when using string buffer?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Ixus See wrote:do i need to add black space to the left over strings when using string buffer?

Maybe you could get some inspiration from the source code provided by Seane in one of his previous posts in this thread. I followed a similar approach for writing my records back to the database file.
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
Roel De Nijs wrote:
Ixus See wrote:do i need to add black space to the left over strings when using string buffer?

Maybe you could get some inspiration from the source code provided by Seane in one of his previous posts in this thread. I followed a similar approach for writing my records back to the database file.


i did
Ixus See
Ranch Hand

Joined: Jul 17, 2011
Posts: 160
for(int i =0; i<out.length(); i++) out.setCharAt(i, ' ');

solve using the following ;) thanks guys
 
Don't get me started about those stupid light bulbs.
 
subject: Writing to .DB with RandomAccessFile
 
Similar Threads
Unicode and MSSQL
Writing to DB file
Structure of the DB file, what is to be stored
Writting to the DB File - totally lost
Unicode support for Tamil language