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 ();