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



  • 相关阅读:
    Rust 包管理器 Cargo 入门
    如何设置对企业批量的图文档加密?2021企业首选公司文档加密方案,宁波风奥金甲数据防泄漏
    跟坚哥学QUIC系列:加密和传输握手
    如何在SQLServer中处理每天四亿三千万记录的
    高德最佳实践:Serverless 规模化落地有哪些价值?
    浅谈 Pull Request 与 Change Request 研发协作模式
    Dbeaver连接国产数据库人大金仓
    我对云原生软件架构的观察与思考
    Java Web整合开发(20) -- Hibernate入门
    2 修改与恢复
  • 原文地址:https://www.cnblogs.com/itmyhome/p/4131339.html
Copyright © 2020-2023  润新知