Clob类型转换成String类型
Posted skiwnchqhh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Clob类型转换成String类型相关的知识,希望对你有一定的参考价值。
oracle中表结构如下:
create table GRID_RESOURCE
(
ID VARCHAR2(50),
CNNAME VARCHAR2(50),
TYPE VARCHAR2(50),
PARENTID VARCHAR2(50),
VIEWWIDTH VARCHAR2(50),
REQUESTURL VARCHAR2(256),
COUNTSQL CLOB,
ISDISPLAY VARCHAR2(50),
TREEINDEX VARCHAR2(256),
TABLES VARCHAR2(256),
NOTE3 VARCHAR2(256)
)
COUNTSQL字段为CLOB类型,索引为6
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class T {
public static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
/**
* @param args
*/
public static void main(String[] args) {
new T().getList();
}
public void getList() {
String querySQL = "select * from GRID_RESOURCE";
String countSQL = "";
List<Object[]> list = new ArrayList<Object[]>();
// List o_list = new ArrayList();
try {
list = sessionFactory.openSession().createSQLQuery(querySQL).list();
for (Object[] obj : list) {
Map<String, Object> map = new HashMap<String, Object>();
Clob sc = (Clob) obj[6];
countSQL = ClobToString(sc);
System.out.println(countSQL);
}
// 另一个遍历方法
/*
* for(Object obj:o_list){ Object oo[] = (Object[])obj;
* System.out.println(oo[6]); }
*/
} catch (Exception e) {
e.printStackTrace();
}
}
// 将字Clob转成String类型
public String ClobToString(Clob sc) throws SQLException, IOException {
String reString = "";
Reader is = sc.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
以上是关于Clob类型转换成String类型的主要内容,如果未能解决你的问题,请参考以下文章
Spring+Mybatis类型转换的问题,oracle数据库中有一个clob类型,怎样在查询以后转换为String类型?