This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Database read problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Database read problem" Watch "Database read problem" New topic
Author

Database read problem

uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
I am having the B&S assignment and I am at a leel of biginners stage. Reading through different posts I got little idea about RAF, like it has header, scheama etc. If somebody would like to explain in my scenario, greatly appreciated.

When I read my DB file and use System.out.println(raf.length());
Does it mean I donot have any records in my database file ? Please see the discription below. any help is appreciated. I am totally stuck.

Start of file
4 byte numeric, magic cookie value. Identifies this as a data file
2 byte numeric, number of fields in each record

Schema description section.
Repeated for each field in a record:
1 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
1 byte numeric, field length in bytes
end of repeating block

Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information

End of file



Database schema
The database that Bodgitt and Scarper uses contains the following fields: Field descriptive name Database field name Field length Detailed description
Subcontractor Name name 32 The name of the subcontractor this record relates to.
City location 64 The locality in which this contractor works
Types of work performed specialties 64 Comma separated list of types of work this contractor can perform.
Number of staff in organization size 6 The number of workers available when this record is booked
Hourly charge rate 8 Charge per hour for the subcontractor. This field includes the currency symbol
Customer holding this record owner 8 The id value (an 8 digit number) of the customer who has booked this. Note that for this application, you should assume that customers and CSRs know their customer ids. The system you are writing does not interact with these numbers, rather it simply records them. If this field is all blanks, the record is available for sale.
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332

When I read my DB file and use System.out.println(raf.length());


then... what? (do you see?)

Your records are in data section. I think it is very unlikely, that you have 0 records in your db file.
uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
it brings me zero (0)

and when I do this

or


It throws exception

and when I do this



it brings me the strange output [B@107077e, B@7ced01 , B@26e431......
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Hi Uzma,

I'm not sure what you are asking but the raf.length() should return the length of your file in bytes.

I would recommend you start by writing a simple program to output the contents of the file.

For example to read the magic cookie you would use the readInt() method, which will read the first 4 bytes of the file.

The determine the number of fields using the readShort() method, which will read the next 2 bytes of the file.

Then you need a for loop to read the number of fields defined by the previous step.

Now you have read the header you can read each record, using the information from the schema description, i.e. you know the field length in bytes to read in each record.

Hope it helps

Jason
uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
Please help I am posting my code and tell me where I am making the mistake
Your help is valuable




and it brings me

file found
the pointer is:0
the file size:0
EOF: null
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
I think it could be the path of your DB file.

If you add the following line before creating the RandomAccessFile, see what you get.

uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
it says :
file exists = true
file found
the pointer is:0
the file size:0
EOF: null

that means file exists.
thanks for the help
any further suggestions?
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Have you checked the size of the file from the operating system, tried opening the file using notepad, vi etc? Do you see bytes in the file?

Have you used "rw" when creating the RandomFileAccess in the past? as it will create a empty file if it can't find a file.

Have you got a backup of the file?
uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
Now I replaced the file with the backup
and getting

file exists = false
FNF: db-2x3.db (The system cannot find the file specified)

you were so right

so now why it is not finding the file though I have replaced it

thanks

Uzma
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Where is the db file in location to your class file?

You could try including the fully qualified path i.e.

new File("C:\\directory\\db-2x3.db");

Just make sure you use copy of the file and not the backup file!
[ July 30, 2007: Message edited by: Jason Moors ]
uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
I gave the path and it is even the same directory as of the contractorAccessfile

what should I do now

Only thing is .db is the extension I gave to file but even removing the extension does not help

thanks

Uzma
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Is the db file in the same directory as the class or the location your are executing the java command?

i.e. From your example code you are using the sun.certify package so is the db file under \sun\certify or where you are running the java command? which normally would be above the sun\certify directory
 
wood burning stoves
 
subject: Database read problem