Guys, here's a simple tool I created that reads the .db file that is sent along with the SCJD assignments we currently have. This will particularly be helpful to people in the beginning of the development, which is when we have lots of doubts, like, "where should I start from", or "I think my .db file is corrupted", etc.
In order to correctly work, it is likely that a few changes will be required, and they should be done according to the database description, which can be found in the instructions.html file. For instance, some databases have 2 bytes storing each record flag; in this case, it would be necessary to change the value of the RECORD_FLAG_BYTES field to 2, as well as the VALID field.
Note that this code only reads the database file, that's why FileInputStream is being used. Also, I'd really really appreciate if you guys could test it and let me know if you run into any issue.
This message was edited 5 times. Last update was at by Roberto Perillo
I'm glad you liked it! That's the idea. Since there are a few .db formats, you end up having to make some adjustments in order to work correctly. But just changing the number of bytes of the class-level fields should be enough. Did you have to have to change something else?
Thanks a lot, I just got my assignment (URLyBird 1.3.1) and tried your program and it worked great, I just had to change DATABASE_LOCATION and comment out package suncertify.db;
That's the spirit! It shouldn't be necessary to change many things to use this tool. That's the idea! Also, please take a look at an important administrative private message I just sent you.
I was so confused about the database file I got from Sun for my SCJD assignment (URLyBird 1.1.2). I just downloaded the assignment a week ago and I have been trying unsuccessfully to make sense of the .db file I received. Until I ran across your tool, that is.
Please do what you have to do to make this tool popular on the ranch. I am sure other SCJD candidates will find it extremely useful.
When we download the project, we usually have lots of doubts, and it usually takes sometime to understand everything that has to be done. The idea of this tool is to offer a "kick off" to people that is starting with the development. With it, you can have an understanding of how the database is organized, and then you can start writing your own code!
I'm worried that I'm missing some simple approach to accessing B&S 2.1.1 .db file records...
Roberto's tool is the closest thing I've found to accessing the .db file provided with my B&S 2.1.1 materials. Somehow, though, I think it should be easier to skip right to the records and read data than Roberto's tool makes it appear. Maybe I feel this way because the Camerlengo, Monkhouse SCJD book's sample project has no code reckoning with RandomAccessFile file seeking.
Can anyone comment on how I should locate the first record in my B&S .db file, e.g., using ?
Thanks for the words, partner! I feel really happy when I see a "passed" post here, and even more happy when I'm able to help with that!
Grary, my friend, this tool is just for you to have an idea of how the reading of the .db file can be done. It simply reads the .db file and displays its content on the console. I believe that, in your case, you'll need to have something a little bit more related to your particular implementation (such as getting the data of a given record, according to its number, etc). For this, you'll have to go through some thinking and implement your solution. For instance, in my implementation of the Data class, you can go directly to the location of a given record in the .db file:
Where the getFilePointerOffset method gets the offset of a particular record. For that, I have a formula that finds out what is the offset in the .db file of a given record (I use information, like, header length, record length, etc).
This tool is just for you to have an idea of how this can be done, so you can start thinking about your particular implementation.
great tool, I am jsut starting off with some help from Andrew's book, there's a lots of stuff , and information about database in the instructions file seem cryptic to me, but this tool helped, now I get a clear picture on what's in that db file
Thanks Roberto for the tool. I had just to change it here, change it there and it worked. For instance I had to change the RECORD_LENGTH_BYTES to zero.
cheers
Bob
Ps. working on Early Birdy Stuff (real name: URLyBird)
This message was edited 1 time. Last update was at by Bob Wheeler
Thanks Roberto for presenting this nicely readable.
It saved a lot of hard-coded numbers from my assignment.
Probably recordLength is a field missing from the db file I have.
Second 4 –byte from the file start is “4 byte numeric, offset to start of record zero”
And I feel its file pointer to data-section-start.
possibly recordLength is to be build from adding schema-description-section fieldLengths.
First 4 byte magic-cookie , next 4 byte record-length , right ?
My assignment says a Data File Format
Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
So, is that "start of record zero" pointing to Data section. (offset into file equal to "offset to start of record zero" value) I feel probably my db format doesn't have a recordLength value directly given
Interesting... I had never seen this description...
Well, I think you don't really have the record length info in your .db file. But that shouldn't be a problem. The only think is that you have to make sure that these 4 bytes related to the "offset to start of record zero" are considered when you read your database, so it is read correctly.
When you run the tool, are the records displayed correctly? Which one is your assignment?
This tool is created for everybody to read the complete content of your database file, so an InputStream will be more than enough. For the real assignment, you will jump directly to the position of a record and then you will use the RandomAccessFile and its seek-method.
Thanks Robert !
I am using your code .
Only change that I made is I am using RandomAccessFile .
I am able to fetch all the data .
I am getting the following error .
Exception in thread "main" java.lang.RuntimeException: The given file does not exist.
at scjd.DBFileReader.main(DBFileReader.java:184)
Caused by: java.io.FileNotFoundException: C:\db-1x3.db (Access is denied)
Robert, what changes I will need to make ?
I want to record the location for each record so that I will be able to use the seek command.