aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Writing to the db file 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 "B&S: Writing to the db file" Watch "B&S: Writing to the db file" New topic
Author

B&S: Writing to the db file

Michael Vargenstien
Ranch Hand

Joined: Jan 27, 2007
Posts: 61
Hello Everybody,

I'm having some issues/bugs that I found when writing to the database. I'm trying to figure out if there is a way that I can write a byte, not a char, string, etc. to the database without using the Filestream classes. Is this even possible or do I need to refactor the code to use the filestream classes?

For example:
db file:
field1 | field2 | field3 | field4 | field5
=============================================
byte | string | string | string | string

....
write(somebyte, length);
write(field1, length);
write(field2, length);
....

where write is a function that accepts bytes or strings

Thanks.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    8
Not sure how you are implementing the DB part but you can use RandomAccessFile to read/write N bytes from a given offset. Here, N would be the size of the complete "row". After you get the bytes, you can convert them into whatever data types your upper layer needs as per the number of bytes devoted to each field.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Michael Vargenstien
Ranch Hand

Joined: Jan 27, 2007
Posts: 61
Thanks for you reply Paul. That is exactly what I'm doing at this point. However, to not "corrupt" the database it is necessary when writing to the database during a row implementation to write a Byte indicating the status of the particular individual within the database. My resolution was to use something along the lines of this:

var.writeByte(contractor.getStatus().intValue());

Anyhow, at least we are approaching this the same way.
Vincent Li
Greenhorn

Joined: Jan 12, 2007
Posts: 22
Originally posted by Michael Vargenstien:
..it is necessary when writing to the database during a row implementation to write a Byte indicating the status of the particular individual within the database. My resolution was to use something along the lines of this:

var.writeByte(contractor.getStatus().intValue());



Does that work? I mean doesn't returns 4 bytes and not one?

I simply defined a couple of byte constants and use that:


[ February 09, 2007: Message edited by: Vincent Li ]

Vince<br />SCJP(1.4), SCWCD(1.4), SCJD (5.0)
Michael Vargenstien
Ranch Hand

Joined: Jan 27, 2007
Posts: 61
Hmm, that's odd, I'm using the Junit test cases and was able to use this. I also verified the data within the database and it also appears to be intact. I thought that writeByte() returns a byte value, the reason I used writeByte(contractor.getStatus().intValue()) was because that returns an int value which is what the writeByte method was expecting.

[URL=http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html#writeByte(int)[/url]

Writes a byte to the file as a one-byte value. I think your approach should also work. Do you agree?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B&S: Writing to the db file