Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Data lost when writing object to a file  RSS feed

 
Conie Ooi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to write an object which is extracted from database to a file. The following is the statement I use:
Object Obj = rs.getObject(colName);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(Obj);
After the object is saved to the file, part of the data is lost. Am I using the correct way to write the object to a file? If not, can you suggest a better way?
Any help is appreciated. Thanks...
regards
 
Snigdha Solanki
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your code here. What exactly do you mean by losing part of data?
I am doing it the same was as yours and it works fine for me.


Output is:
Int is 1
String is One
Char is 1
 
Conie Ooi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for the reply.
These are my code:

Let me explain about what I'm trying to do. I have uploaded a jpg file to the database as a BLOB. In fact I'm trying to upload any file to the database. The function above basically want to download the image file and save it as different name.
You might wonder why I use rs.getObject() instead of getBlob(). Whenever I use getBlob(), I always received the following message:

I do not know what is wrong. I checked my db2java.zip, there is no getBlob() in the class DB2ResultSet. Do you have any idea why this happen? What is/ are the missing things?
Again, any suggestion is highly appreciated and waiting for your reply... Thank you in advanced
[ Edited by Dave to format code ]
[ August 05, 2002: Message edited by: David O'Meara ]
 
Conie Ooi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't explain what i mean "data lost" in the previous reply. When I upload a text file and download it again, there are some stranges characters at the first few rows of the file. When I download an jpg file, the file size is the same, but the file counldn't be viewed.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly was the object stored in the database anyway? As a blob? Probably you want to use getBlob() rather than getObject(), and then use the getBytes() or getBinaryStream() of the Blob class to extract the binary data from the object. Or if you're not sure how the data is stored in the DB, use

to learn what kind of object you're really dealing with.
If you have access to the code that puts the image data into the database, you should probably post some of that to clarify what's going on.
By the way, you've found one of my pet peeves:

Why on earth does anyone write code like this? Sure, probably no IOException will occur, and there won't be a problem. But if anything does go wrong, you're basically telling the machine to ignore the problem and not tell you. This is Wrong. At the very least, put ioe.printStackTrace() in the catch, so that you get some indication if there's a problem.
 
Sandhya Chamala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If u r trying to store an image in database, better go for Blob. For this u need the oracle package. Check whether the code below can be useful to u. I took a string to store as a Blob object in database. Instead of that try with ur image object.

Code is :
--------
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class Blob_Example{

public static void main(String args[])
{
InputStream byte_stream = null;
InputStreamReader rdr = null;
String xmlstring = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // give some large string here
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(url,username,password);// ur db url, username and password
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
try{
stmt.execute ("CREATE TABLE blobtable (blob_data BLOB)");
stmt.execute ("insert into blobtable values (1, EMPTY_BLOB())");
ResultSet rset = stmt.executeQuery("select blob_data from blobtable FOR UPDATE");
if (rset.next()) {
BLOB blob = ((OracleResultSet)rset).getBLOB(1);
OutputStream ostream = ((BLOB)blob).getBinaryOutputStream();
ostream.write(xmlstring.getBytes());
ostream.flush();
ostream.close();
}
conn.commit();
ResultSet rs = stmt.executeQuery("select blob_data from blobtable");
while (rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB (1);
byte_stream = blob.getBinaryStream();
FileOutputStream fp = new FileOutputStream("blob.txt");
rdr = new InputStreamReader(byte_stream,"UTF8");
int i = 0;
while ((i = byte_stream.read()) != -1 )
fp.write(i);
}
}
}
 
Conie Ooi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code but I'm not using oracle, my database is DB2.
I saw a lot of people that use DB2 as the database use getBlob() method in their program, but I do not know why getBlob() doesn't works on my program.
After few days of trying, I manage to get the content of the blob from the database without using geBlob() method. Thanks a lot for all the helps....
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!