• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Reading from the DB

 
Karthik Balasubramanian
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stefan Olson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,
Sun have a very brief introduction to Random Access Files in their Essential Java Classestutorial.
Regards, Andrew
 
Karthik Balasubramanian
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic