Using Oracle BLOBs in Java

Source: Internet
Author: User
Tags commit int size requires stmt
LOB (Large Object) in Oracle Oracle can store very large data (possibly 4GB) so that it can be written to the database by serializing a file or any other object into a byte output stream (OutputStream) and then using the byte input stream ( InputStream) reads the data and deserializes it into the original file or object. Operation requires the use of Oracle's JDBC package, which extends the Blob objects in the Sun's JDBC package. At the same time need to pay attention to some details. The following code shows how to use a blob (a JDBC package in an instance that requires Oracle).

Import Oracle.jdbc.OracleResultSet; Using Oracle's ResultSet object
Import Oracle.sql.BLOB; Use Oracle's Blob object, not Sun's blob

...

try{
Connection conn=< Database Connection >;
File file=< to the database object >;
Conn.setautocommit (FALSE); To cancel the auto commit property of a Connection object
String File_name=file.getname ();

There is an item table in the database, where the file_name (VARCHAR2) store file name, File_blob (BLOB) stores the file object
String sql= "INSERT into item (FILE_NAME,FILE_BLOB) VALUES (' + file_name +" ', Empty_blob ()) "; Use "Empty_blob ()" To give birth to an empty BLOB
Statement stmt=conn.createstatement ();
int count=stmt.executeupdate (SQL);

Sql= "Select File_blob from Item WHERE iid= '" + IID + "' for UPDATE"; Use ' for UPDATE ' to get a write lock on a table
ResultSet rs=stmt.executequery (SQL);
Rs.next ();
BLOB blob= ((oracleresultset) RS). GetBlob ("File_blob"); Get Blob Object
OutputStream Out=blob.getbinaryoutputstream (); Create an output stream
InputStream in=new fileinputstream (file); Create an input stream
int size=blob.getbuffersize ();
Byte[] Buffer=new byte[size]; Create buffer
int Len;
while ((Len=in.read (buffer))!=-1)
Out.write (Buffer,0,len);
In.close ();
Out.close ();

Conn.commit ();
}
catch (Exception ex) {
try{
Conn.rollback ();
}
catch (SQLException Sqle) {
System.err.println (Sqle.getmessage ());
}
}

If you want to read out the file, just call the Blob's Getbinarystream () to generate an input stream, and then write a file.




Related Article

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.