Access to BLOB fields in Oracle databases

Source: Internet
Author: User

The following articles mainly introduce the access to BLOB fields in Oracle databases. I often encounter the access to BLOB fields in Oracle databases, the requirement is to store a file or file stream to the Oracle database. Oracle8 provides Blob and Clob to store binary big object data.

However, it is not compatible with Blob in Java. SQL. It often causes the Blob field to be unable to be locked or the operation fails. In summary, I have shared some experiences.

First, create a test data table.

 
 
  1. drop table filelist;  
  2. commit;  
  3. CREATE TABLE SYSTEM.FILELIST (  
  4. "FILENAME" VARCHAR2(50) NOT NULL,  
  5. "FILESIZE" NUMBER(20) NULL,  
  6. "FILEBODY" BLOB NULL,  
  7. PRIMARY KEY("FILENAME"), UNIQUE("FILENAME")) ;  
  8. commit;  

During the test, first read the hard disk file into the Oracle database and then read it to another new file on the hard disk. The original code is as follows:

 
 
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.sql.*;  
  4. import oracle.sql.*;  
  5. import oracle.jdbc.driver.*;  
  6. import java.text.*;  
  7. public class test  
  8. {  
  9. public static void main(String args[]) throws java.io.IOException,java.sql.SQLException  
  10. {  
  11. dbBean db1=new dbBean();  
  12. /**  

* Here is my data connection Bean

* You can use your own connection Bean.

 
 
  1. */  
  2. byte a[]=null;  

** Read the test file test.doc into this byte array

 
 
  1. java.io.FileInputStream fin=null;  
  2. java.io.FileOutputStream fout=null;  
  3. oracle.jdbc.OracleResultSet ors=null;  

** Rs must be provided by the Oracle database.

 
 
  1. oracle.jdbc.driver.OraclePreparedStatement opst=null; 

** PreparedStatement

Provided by Oracle

 
 
  1. try  
  2. {  
  3. java.io.File f1=new java.io.File("c:/temp/test.doc");  
  4. java.io.File f2=new java.io.File("c:/temp/testout.doc");  

** Write the Information read from BLOB

// Enter the file for comparison with the source file

 
 
  1. fin=new java.io.FileInputStream(f1);  
  2. fout=new java.io.FileOutputStream(f2);  

Int flength = (int) f1.length (); // ** the length of the byte used to read the file

 
 
  1. System.out.println("file length::"+flength);  
  2. a=new byte[flength];  
  3. int i=0;int itotal=0;  

/** Read files into byte Arrays

 
 
  1. for (;itotal<flength;iitotal=i+itotal )  
  2. {  
  3. i=fin.read(a,itotal,flength-itotal);  
  4. }  
  5. fin.close();  
  6. System.out.println("read itotal::"+itotal);  

/** Note that the BLOB of the Oracle database must be initialized with EMPTY_BLOB ().

 
 
  1. String mysql="insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";  
  2. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  3. opst.setString(1,"wordtemplate");  
  4. opst.setInt (2,flength);  
  5. opst.executeUpdate();  
  6. opst.clearParameters();  

/** Locate the BLOB field after inserting other data

 
 
  1. mysql="select filebody from filelist where filename=?";  
  2. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  3. opst.setString(1,"wordtemplate");  
  4. ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
  5. if (ors.next())  
  6. {  

Oracle. SQL. BLOB blob = ors. getBLOB (1);/** get the BLOB Field

Int j = blob. putBytes (1, a);/** write the byte array into the BLOB Field

 
 
  1. System.out.println("j:"+j);  
  2. db1.conn.commit();  
  3. ors.close();  
  4. }  
  5. System.out.println("insert into ok");  

Byte B [] = null;/** Save the bytes read from BLOB

 
 
  1. opst.clearParameters();  
  2. mysql="select filebody from filelist where filename=?";  
  3. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  4. opst.setString(1,"wordtemplate");  
  5. ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
  6. if (ors.next())  
  7. {  
  8. oracle.sql.BLOB blob2=ors.getBLOB(1);  
  9. System.out.println("blob2 length:"+blob2.length());  

B = blob2.getBytes (1, flength);/** retrieve byte stream data from BLOB

 
 
  1. System.out.println("b length::"+b.length);  
  2. db1.conn.commit();  
  3. }  
  4. ors.close();  

/** Write the bytes read from BLOB into the file

 
 
  1. fout.write(b,0,b.length);  
  2. fout.close();  
  3. System.out.println("write itotal::"+b.length);  
  4. }  
  5. catch(Exception e)  
  6. {  
  7. System.out.println("errror :"+e.toString() );  
  8. e.printStackTrace();  
  9. }  
  10. finally  

{/** Close all data connections

 
 
  1. stmt.close();  
  2. db1.closeConn();  
  3. }  
  4. }  
  5. }  

Compile and run the program in TomCat.

It should be noted that the Blob access process is generally first stored in BLOB-related control data, such as the file name, then queried and located BLOB fields, using the methods provided by the Oracle Database Blob:

 
 
  1. public int putBytes(long pos,byte bytes[])  
  2. public byte[] getBytes(long pos,byte bytes[])  

Or use

 
 
  1. public OutputStream getBinaryOutputStream() throws SQLException  
  2. public InputStream getBinaryStream() throws SQLException  

Because the input and output streams are always used to buffer streams in byte arrays, we will not give an example.

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.