Package com. vnet. flyed. text;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. SQL. sqlexception;
Import java. util. List;
Import oracle. SQL. Blob;
Import org. hibernate. hibernate;
Import org. hibernate. lockmode;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Import org. hibernate. lob. serializableblob;
Import com. vnet. flyed. Factory. hibernatesessionfactory;
Import com. vnet. flyed. pojo. vnetuser;
Public class hibernatetest {
Public static void main (string [] ARGs ){
Configuration Config = new configuration (). Configure ();
Session session = hibernatesessionfactory. getsession ();
Vnetuser user = new vnetuser ();
Transaction TR = session. begintransaction ();
User. setid (100 );
User. setname ("flyed ");
User. setpassword ("flyed ");
Try {
User. setimage (Oracle. SQL. Blob. empty_lob ());
} Catch (sqlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
User. setresume (hibernate. createclob (""));
Session. Save (User );
Session. Flush ();
Try {
Session. Refresh (user, lockmode. Upgrade );
Serializableblob sb = (serializableblob) user. getimage ();
// Call its getwrappedblob for Transformation
Blob blob = (BLOB) sb. getwrappedblob ();
// Read a question and write it to the output stream of the Blob Field
Outputstream out = blob. getbinaryoutputstream ();
Fileinputstream imgis = new fileinputstream ("D:/tom.gif ");
Byte [] Buf = new byte [102400]; // 10 k Cache
Int Len;
While (LEN = imgis. Read (BUF)> 0 ){
Out. Write (BUF, 0, Len );
}
Imgis. Close ();
Out. Close ();
} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Session. Save (User );
Tr. Commit ();
String hql = "from vnetuser where id = 100 ";
Query query = session. createquery (hql );
List list = query. List ();
If (list. Size ()> 0 ){
Vnetuser user1 = (vnetuser) list. Get (0 );
Try {
Inputstream is = user1.getimage (). getbinarystream ();
Outputstream OS = new fileoutputstream ("D:/a.jpg ");
Byte [] Buf = new byte [1, 1024];
Int I = 0;
While (I = is. Read (BUF ))! =-1 ){
OS. Write (BUF, 0, I );
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Session. Close ();
}
}