How to process OracleBLOB fields in jdbc

Source: Internet
Author: User
What detours will most people take? 1. Use the method in jdk for transmission. The getBlob () method exists in the ResultSet and the setBlob () method exists in the PreparedStatement. Therefore, most people will try to read and write the setBlob () and getBlob () methods, or transmit the BLOB between two databases. This method is actually not feasible, according to some resources on the Internet

What detours will most people take? 1. Use the method in jdk for transmission. The getBlob () method exists in the ResultSet and the setBlob () method exists in the PreparedStatement. Therefore, most people will try to read and write the setBlob () and getBlob () methods, or transmit the BLOB between two databases. This method is actually not feasible, according to some resources on the Internet

What detours will most people take?
1. Use the method in jdk for transmission. The getBlob () method exists in the ResultSet and the setBlob () method exists in the PreparedStatement. Therefore, most people will try the setBlob method.

(), GetBlob () for read/write, or BLOB transmission between two databases. This method is actually not feasible. According to some information on the Internet, sun official documentation has some methods.

All are errors.
2. Use ResultSet. getBinaryStream and PreparedStatement. setBinaryStream to read and write BLOB or transmit data between two databases. I tried this method myself,

It is found that if BLOB stores text files, there will be no problem. If it is binary files, there will be problems with transmission.

Based on your experience and reading the official Oracle documents, you can use the following methods:
1. Create a record and insert BLOB Data
1.1 when creating A record, use the oracle function to insert an empty BLOB. Assume that field A is of the BLOB type:
Insert xxxtable (A, B, C) values (empty_blob (), 'xxx', 'yyyy ')
1.2 query the inserted record and update BLOB. Before the query, set an attribute of Connection:
Conn. setAutoCommit (false); if this step is missing, exceptions such as fetch out of sequence may occur.
1.3 query the inserted record, followed by "for update", as shown below:
Select A from xxxtable where xxx = 999 for update. If for update is missing, the row containing the LOB value is not locked.

Exception
1.4 obtain and update the BLOB from the queried blob field. The Code is as follows:
BLOB blob = (BLOB) rs. getBlob ("");
OutputStream OS = blob. getBinaryOutputStream ();
BufferedOutputStream output = new BufferedOutputStream (OS );

You can use the output. write method to write the content to the output file. For example, we write a file into this field:
BufferedInputStream input = new BufferedInputStream (new File ("c: // hpWave. log"). toURL (). openStream ());
Byte [] buff = new byte [2048]; // Buffer Used for file writing
Int bytesRead;
While (-1! = (BytesRead = input. read (buff, 0, buff. length ))){
Output. write (buff, 0, bytesRead );
System. out. println (bytesRead );
}
The above Code reads data from input 2 K and then writes it to output.
1.5 after the preceding execution is complete, close output, input, and the queried ResultSet.
1.6 finally run conn. commit (); Submit the updated content, and execute conn. setAutoCommit (true); change back to the Connction attribute
2. Modify the record. The method is similar to the above method,
2.1 First Update fields other than BLOB
2.2 use a similar method in 1.3 to obtain records
2.3 During modification, note the following: BLOB may be NULL in the record to be updated by a, so that the value obtained by executing blob. getBinaryOutputStream () may be

Null, close the select record and execute update xxxtable set A = empty_blob () where xxx again. In this way, an empty BLOB (not null) is written first, and then

Use the methods in 1.3 and 1.4 to execute the update record. B do not forget to execute setAutoCommit (false), "for update", and the following conn. commit.
3. read data from BLOB fields.
3.1 setAutoCommit () is not required to read records, and select... for update.
3.2 query the output records using the normal select Method
3.3 obtain and read BLOB from ResultSet, as shown below:
BLOB B _to = (BLOB) rs. getBlob ("");
InputStream is = B _from.getBinaryStream ();
BufferedInputStream input = new BufferedInputStream (is );
Byte [] buff = new byte [2048];
While (-1! = (BytesRead = input. read (buff, 0, buff. length ))){
// Write data here, for example, in the BufferedOutputStream of the file
System. out. println (bytesRead );
}
The data in blob is retrieved cyclically, written to buff, and then the buff content is written to the desired location.
4. Transmission of blob fields between two databases
Similar to the methods 1 and 3 above, get BufferedOutputStream while get BufferedInputStream and read and write data. Note that

Connection needs to execute conn. setAutoCommit (false); and add "for update" and the final commit () when obtaining the record ();

To sum up the above methods, it is simply to first create an empty BLOB, then obtain its BufferedOutputStream for writing, or obtain BufferedInputStream for reading.

2007-6-23 19:45:02
View comments ???

19:48:30 java language operations in Oracle Database CLOB Data Type field type: blob, clob, nclob
Note: three large objects (LOB) are used to save large image files or text files with formats, such as Miceosoft Word documents and non-text files such as audio and video files, the maximum length is 4 GB. There are several types of LOB, depending on the type of bytes you use. Oracle 8i actually stores the data in the database. You can perform read, storage, write, and other special operations.
  
The attribute in the clobtest_table we operate on is (string id, CLOB-type picstr). At present, we assume that a large character object str already contains the data we need to store in the picstr field. And the connection object conn has been created. In the following example, the program does not want to occupy too much space, so it does not write the exception. For more information, see the api doc. You will be able to know what exceptions to throw. Here we will only show you how to write them.
  
Code:
  
  
(1) perform the insert operation on the database clob type
**************************************** *********
  
Java. SQL. PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
  
Conn. setAutoCommit (false );
Query = "insert into clobtest_table (id, picstr) values (?, Empty_clob ())";
Java. SQL. PreparedStatement pstmt = conn. prepareStatement (query );
Pstmt. setString (1, "001 ");
Pstmt.exe cuteUpdate ();
Pstmt = null
Query = "select picstr from clobtest_table where id = '001' for update ";
Pstmt = con. prepareStatement (query)
Rs = pstmt.exe cuteQuery ();
  
Oracle. SQL. CLOB clobtt = null;
If (rs. next ()){
Clobtt = (oracle. SQL. CLOB) rs. getClob (1 );
}
Writer wr = clobtt. getCharacterOutputStream ();
Wr. write (strtmp );
Wr. flush ();
Wr. close ();
Rs. close ();
Con. commit ();
  
  
  
(2) check whether the database has been successfully inserted through SQL/plus.
**************************************** *********
  
The PL/SQL package DBMS_LOB is used to process LOB data. Check whether the insert is successful. Use the getlength procedure of the DBMS_LOB package to check whether str has been saved to the picstr field. For example:
  
SQL> select dbms_lob.getlength (picstr) from clobtest_table;
  
  
(3) read database clob
**************************************** *********
  
Reading relative inserts is easy. There is no big difference between basic steps and half of database data retrieval.
String description = ""
Query = "select picstr from clobtest_table where id = '001 '";
Pstmt = con. prepareStatement (query );
ResultSet result = pstmt.exe cuteQuery ();
If (result. next ()){
Oracle. jdbc. driver. OracleResultSet ors =
(Oracle. jdbc. driver. OracleResultSet) result;
Oracle. SQL. CLOB clobtmp = (oracle. SQL. CLOB) ors. getClob (1 );
  
If (clobtmp = null | clobtmp. length () = 0 ){
System. out. println ("===== CLOB object is blank ");
Description = "";
} Else {
Description = clobtmp. getSubString (long) 1, (int) clobtmp. length ());
System. out. println ("====== string form" + description );
}

}

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.