Use Java to operate clob Data Types in Oracle databases

Source: Internet
Author: User

For information about Oracle field types, refer to Oracle technical network. The following excerpt describes blob. clob and other types. It is easy for everyone to understand.
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 );
}
}

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.