Test storage binaries
@Test
public void Test () throws ioexception{
InputStream in=new FileInputStream ("E:\\test.txt");
Byte[] Tblob=new byte[in.available ()];
In.read (TBLOB);
In.close ();
Session ses = hibernatesessionfactory.getsession ();
System.out.println (Ses.hashcode ());
Ses.begintransaction ();
BigObject bb=new bigobject ("Test large text", Tblob);
Ses.save (BB);
Ses.gettransaction (). commit ();
Ses.close ();
}
Test Read binary file
@Test
public void Readtest () throws ioexception{
Session ses = hibernatesessionfactory.getsession ();
BigObject BigObject = (bigobject) ses.get (Bigobject.class, 42);
System.out.println (Bigobject.gettclob ());
if (Bigobject.gettblob ()!=null) {
OutputStream os=new FileOutputStream ("E:\\readbat.txt");
Os.write (Bigobject.gettblob ());
Os.close ();
}
}
Test blob and CLOB types
@Test
public void Test1 () throws exception{
Session ses=hibernatesessionfactory.getsession ();
Ses.begintransaction ();
BigObject bob=new BigObject ();
Bob.settclob (Ses.getlobhelper (). Createclob (""));//Save an empty CLOB instance
Bob.settblob (Ses.getlobhelper (). Createblob (new byte[1]);//Save an empty blob instance
Ses.save (Bob);//Save Object
Ses.flush ();//Flush cache
Ses.refresh (Bob,lockmode.upgrade);//Lock record
if (Bob.gettclob ()!=null) {
Writer Writer=bob.gettclob (). Setcharacterstream (0);
Writer.write ("Can't remember Ah");
Writer.close ();
}
if (Bob.gettblob ()!=null) {
InputStream in=new FileInputStream ("E:\\ceshi.txt");
Byte[] Bytearray=new byte[in.available ()];
In.read (ByteArray);
In.close ();
Writes binary data from a file to a Blob object
OutputStream Os=bob.gettblob (). Setbinarystream (1L);
Os.write (ByteArray);
Os.close ();
}
Ses.gettransaction (). commit ();
Ses.close ();
}
Test blob and CLOB type read from database to hard disk
@Test
public void Test2 () throws exception{
Session ses=hibernatesessionfactory.getsession ();
BigObject obj = (bigobject) ses.get (Bigobject.class, 59);
if (Obj.gettclob ()!=null) {
Reader Reader=obj.gettclob (). Getcharacterstream ();
Char[] Cha=new char[1];
StringBuilder sb=new StringBuilder ();
while (Reader.read (CHA)!=-1) {
Sb.append (New String (CHA));
}
System.out.println (sb.tostring (). Trim ());
Reader.close ();
}
if (Obj.gettblob ()!=null) {
InputStream In=obj.gettblob (). Getbinarystream ();
OutputStream os=new FileOutputStream ("E:\\aaa.txt");
int len=-1;
while ((Len=in.read ())!=-1) {
Os.write (len);
}
}
Ses.close ();
}
Hibernate binary or large file type data and Oracle interaction