Hibernate binary or large file type data and Oracle interaction

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.