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

}



posted on 2014-02-23 18:55  itmyhome  阅读(14901)  评论(0编辑  收藏  举报

导航