---
title: "java读取clob字段的几种方法"
author: "shixiuteng"
date: "2011-12-21T11:08:55Z"
tags: []
categories: ["编程语言"]
slug: "java-clob"
draft: false
lastmod: "2011-12-21T11:08:55Z"
id: 1319286
url: "https://itzyx.iteye.com/blog/1319286"
---

最近频繁处理clob字段，故集中了几种读取clob字段的方法，供大家参考。

**第一种：**

```
Clob clob = rs.getClob("remark");//java.sql.Clob
String detailinfo = "";
if(clob != null){
detailinfo = clob.getSubString(0,(int)clob.length());
}

```

   
**第二种：**

```
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");
}

```

 **第三种：**

```
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";
}

}

```

 第一种方法代码量少，且能避免中文乱码问题；第二种方法与第一种方法效率差不多，也是常使用的一种方法；第三种方法效率极低，如果数据比较大的话建议不要使用。