Several ways to read CLOB fields in Java

Source: Internet
Author: User

http://cache.baiducontent.com/c?m= 9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc790f1a063ce3be25200d548d98297a40e91a1ab1a12b71360125b49acb834ad fb8912a2f8e2037771d8c4516d604bb8e1b65972fc809adfc0ee7cba16ccefec5d2af4325cf44737e97808f4d7163&p= Ce769a44d18201ed08e294780a5d867d&newp=9062cf1a86cc41a95ff7ce2d0217d02f5c5bc4387ebada1f74cec9&user= Baidu&fm=sc&query=java+clob%d7%d6%b6%ce&qid=&p1=4 has recently processed Clob fields frequently, so we have concentrated several methods for reading Clob fields for your reference.

The first type:

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


The second type:

Clob Clob = Rs.getclob ("remark");//java.sql.clob
int i = 0;
if (CLOB! = null) {
InputStream input = Clob.getasciistream ();
int len = (int) clob.length ();
byte by[] = 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:

Clob Clob = Rs.getclob ("remark");//java.sql.clob
String value= "";
String line= "";
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.