Large fields in Java/jsp Oracle store binary/large text

Source: Internet
Author: User
Tags connection reset

The database provides two field types: blob and clob, which are used to store large strings or binary data (slices ).

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 in Oracle JDBC to read and write blob/clob. Therefore, be sure not to read and write blob/clob fields in batch processing; otherwise
Stream type cannot be used in batching exception.

2. the Blob/clob field in Oracle has a 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 rs = 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 ();

 

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.