Several ways to read CLOB fields in Java

Source: Internet
Author: User

Recently, the Clob field is frequently processed, so several methods of reading CLOB fields are concentrated for reference.

The first type:

Clob Clob = Rs.getclob ("remark");//java.sql.clob
Stringdetailinfo = "";
if (clob!= null) {
Detailinfo= clob.getsubstring ((Long) 1, (int) clob.length ());
}


The second type:

Clob clob= Rs.getclob ("remark");//java.sql.clob
inti = 0;
if (clob!= null) {
Inputstreaminput = Clob.getasciistream ();
Intlen = (int) clob.length ();
byteby[] = new Byte[len];
while ( -1!= (i = Input.read (by, 0, by.length))) {
Input.read (by,0, i);
}
detailinfo= new String (by, "Utf-8");
}

The third type:

Clobclob = Rs.getclob ("remark");//java.sql.clob
Stringvalue= "";
Stringline= "";
if (clob!=null) {
Reader reader= ((Oracle.sql.CLOB) CLOB). Getcharacterstream ();
BufferedReader br=new BufferedReader (reader);
while ((Line=br.readline ())!=null)
{
Value + = line + "\ r \ n";
}

}

The first method code is small, and can avoid the problem of Chinese garbled, the second method is similar to the first method of efficiency, is often used as a method, the third method is very inefficient, if the data is relatively large, it is recommended not to use.

I put 100 pages of a Word document content through FCK into a database Clob field, and then read through the above three methods, the first two methods used for almost 4 seconds, the third method used three minutes. But the first two methods do not take into account the very large data situation, generally enough (a novel is not a problem).

Several ways to read CLOB fields in Java

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.