码迷,mamicode.com
首页 > 其他好文 > 详细

Clob类型转换成String类型

时间:2019-01-29 13:54:38      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:final   details   java   print   nts   play   title   void   eww   

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类型

标签:final   details   java   print   nts   play   title   void   eww   

原文地址:https://www.cnblogs.com/skiwnchqhh/p/10333523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!