permaculture playing cards*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Reading from the DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Reading from the DB" Watch "NX: Reading from the DB" New topic
Author

NX: Reading from the DB

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Hi all,
I am working on my Contractors assignment. Just started with trying to read from DB File.
I saw this in our forum
*********************************
The Data class reads from the file using the RandomAccessFile class. All you have to do is use this class to interrogate the database.
***********************************

I think RandomAccessFile is the easier way to read from the DB...Are thery any document, where I can learn more about this and start using it...Please help me on this.
Thanks
Karthik
Stefan Olson
Greenhorn

Joined: Sep 22, 2003
Posts: 5
Stefan Olson
Greenhorn

Joined: Sep 22, 2003
Posts: 5
I think RandomAccessFile is the easier way to read from the DB...Are thery any document, where I can learn more about this and start using it...Please help me on this.

http://java.sun.com/j2se/1.3/docs/api/java/io/RandomAccessFile.html
Get used to referencing the Sun API docs... it's the best way to go.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Karthik,
Sun have a very brief introduction to Random Access Files in their Essential Java Classestutorial.
Regards, Andrew


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

Joined: Dec 13, 2002
Posts: 43
Thanks Guys. Today I tried using Random Access File methods to read from the data base. The magic cookie, rec length, no of fields came out ok. And for the no of fields, name length was read, but when I tried to read the field name for the name length...It is not working. I am sure I am doing something wrong.
look at my code for this part:
char name[] = new char[nameLength],temp;
for (int count=0;count<name.length;count++){
temp=raf.readChar();
name[count]=temp;
It is returning some strange characters...Please suggest me what is the best way to read the field name and other fields. That would be of great help.
Thanks
Karthik
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Karthik,

The Sun Code Conventions recommends only having one declaration per line.
As for your problem .... I think if you check your instructions, you will find that the field names and field contents are stored as bytes. Possibly even US-ASCII 7 bit bytes (which will raise another question I am sure )
You are trying to read a Character which is two bytes long.
You need to read in bytes fully, then convert them to a String.
Regards, Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: Reading from the DB
 
Similar Threads
Testing of data class instance(s)
NX:Using RandomAccessFile and FileChannel
Assignment Comments relating use of Caching and RandomAccessFile
RandomAccessFile
Locking question - URLyBird 1.2.1