• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Reading Data from Database

 
David Khoi Pham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys!

I just passed my SCJP and am now undertaking the Sun Certified Java Developer exam. I have started working on it now and have encountered a few problems that have stumped me.

My approach was to create a RandomAccessFile with a reference to the .db file given to me. I then proceeded to read in all of the header values. After that, my file pointer was at the start of the data section. So, from there, I decided to read in the rest of the bytes and put it into a byte array which I then converted into a String array so that I could see readable information.

The problem I encountered was that I could see all the record information, but what was missing was the byte flag (which determines if a record is valid or not) that should be located at the beginning of each record. If this helps, where the byte flag should have been, there is just a space when I print out the array.

Any tips on getting the flag to print?

Thanks,
David Pham

P.S. Also, should my array which holds record information hold whole records at each index or hold individual field information for all records at each index?
 
Cless Alvein
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The byte flag is simply that: a byte flag. Use an if-else or a switch-case statement to compare it against the possible values of a flag (as defined in the schema) to determine what the flag signifies.
 
David Khoi Pham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh, so does that mean that even though I can't see it, the byte flag is in the String array that I created?
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one got me, too. Yes, it is pretty much whitespace at the beginning of each record. So it is there, just not a printable character. Have you tried looking at the file with a hex editor? I suggest you try that. It helped me immensely with this part of the project. As far as reading records or fields into your array, I think records is what you want. I am stuck on this part right now. I am trying to put them with a count number into a HashMap. I have fields in my byte array, not records.
 
David Khoi Pham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply!

But when it comes time to read the information into the JTable, won't I need to have individual fields instead of records?
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we do need both, and can get both with the given information. From the information in the file, we know how big each field is, and a little simple arithmetic gives us the record size. I'm not at home now and don't have my code with me, so I can't be more specific about the JTable and the tableModel, but I'll check it later. Like I said, I'm still having issues with this part of the project
 
David Khoi Pham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I take a whole record and parse it down to individual fields so I can list it in a JTable?
 
Gabriel Vargas
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Yout have in header your field specification (numer of fields, size of each field including deleted byte record size), so you can read each field individually, i use read methods of RandomAccessFile. You can read each field individually and fills a value object or an array of Strings (there are more threads talking about that, i use a value object), but to put this in a JTable you must pass your object over layers of your application, and as i think you must do first your data layer and others layers (like network layer) before you construct your gui layer (don't worry about this in this step), when data layer and network layer is done you can see gui layer have another issues.

I hope it help you.
[ July 04, 2007: Message edited by: Juan Gabriel Vargas Bola´┐Żos ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic