Usage of Varchar2/Blob/Clob in Oracle

Source: Internet
Author: User
Tags connection reset

The database provides three field types: Varchar2, Blob, and Clob, which are used to store string or binary data. Varchar2 and Clob are used to store string data, while Blob is used to store binary data.

Varchar2 is stored in a single byte and has two maximum lengths: 4000 for the field type and 32767 for the variable type in PL/SQL.

Today, I made a small mistake, that is, the varchar2 type return value of the function is also 4000 in length, not 32767 as I thought.
Blob is stored in a single byte and is suitable for storing binary data and files.
Clob uses multi-byte storage and is suitable for storing large text data.

The processing of BLOB/CLOB fields in Oracle is quite special, so pay special attention to the following two points:

1. Use the Stream Mechanism to read and write BLOB/CLOB in Oracle JDBCTherefore, you must note that you cannot read/write BLOB/CLOB fields in the batch processing; otherwise
Stream type cannot be used in batching exception.

2. The Oracle BLOB/CLOB field itself has a cursor (cursor ),JDBC uses a cursor to operate the Blob/Clob field. Before the Blob/Clob field is created, the cursor handle cannot be obtained.
Connection reset by peer: socket write error exception.

The correct method is: first create an empty Blob/Clob field, and then obtain the cursor from this empty Blob/Clob field. For example, the following code:

PreparedStatement ps = conn. prepareStatement ("insert into PICTURE (image, resume) values (?,?) ");
// Use oralce. SQL. BLOB/CLOB. empty_lob () to construct an empty Blob/Clob object
Ps. setBlob (1, oracle. SQL. BLOB. empty_lob ());
Ps. setClob (2, oracle. SQL. CLOB. empty_lob ());

Ps. excuteUpdate ();
Ps. close ();



// Read The Blob/Clob handle again
Ps = conn. prepareStatement ("select image, resume from PICTURE where id =? For update ");
Ps. setInt (1,100 );

ResultSet rsw.ps.exe cuteQuery ();
Rs. next ();
Oracle. SQL. BLOB imgBlob = (oracle. SQL. BLOB) rs. getBlob (1 );
Oracle. SQL. CLOB resClob = (oracle. SQL. CLOB) rs. getClob (2 );

 

// Write binary data into Blob
FileInputStream inStream = new FileInputStream ("c: // image.jpg ");
OutputStream outStream = imgBlob. getBinaryOutputStream ();
Byte [] buf = new byte [1, 10240];
Int len;
While (len = inStream. read (buf)> 0 ){
OutStream. write (buf, 0, len );
}
InStream. close ();
OutStream. cloese ();


// Write the string to Clob
ResClob. putString (1, "this is a clob ");

// Update the Blob/Clob field to the database.
Ps = conn. prepareStatement ("update PICTURE set image =? And resume =? Where id =? ");
Ps. setBlob (1, imgBlob );
Ps. setClob (2, resClob );
Ps. setInt (3,100 );

Ps.exe cuteUpdate ();
Ps. close ();

 

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.