Re: Idiots guide for Blob -> Object...
Posted by: mezz2k
Date: May 24, 2005 05:18PM

OK I've managed to get a clumsy version working by writing the object to a file first. Excuse the code but I don't have the time to do anything but a quick and dirty explanation.

SWrap hi = new SWrap("hi mysql");//String wrapper
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test"));//new output to file
out.writeObject(hi);//write it
File f = new File("test");
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));//read it back in
PreparedStatement pstmt = con.prepareStatement( "INSERT INTO t(object) VALUES (?)");
pstmt.setBinaryStream(1, bin, (int)f.length());//prepare statement with file
pstmt.clearParameters();//write to database and clear for read back
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT object from t LIMIT 1");;//get results
ObjectInputStream in = new ObjectInputStream(rs.getBinaryStream(1));
SWrap sw = (SWrap)in.readObject();//read in from db and convert to object
System.out.println(sw.getMe());//dump to console

Is it possible to eliminate the writing to a file step if this proves to be the only way to solve the problem? I'm also aware that there might be some errors in that code but I don't have time to check it again.

Options: ReplyQuote

Written By
Re: Idiots guide for Blob -> Object...
May 24, 2005 05:18PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.