【实用】Clob类型与String类型的相互转换

转化方法如下 

    // Clob类型 转String
    public String ClobToString(Clob clob) throws SQLException, IOException {
      String reString = "";
      Reader is = clob.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();
      if(br!=null){
          br.close();
      }
      if(is!=null){
          is.close();
      }
      return reString;
     }


        // String类型转换成Clob类型
	public Clob StringToClob(final String string) {

		if(null == string || string.trim().length() == 0){
			return null;
		}
		return new org.hibernate.lob.ClobImpl(string);
	}

方法调用示例

    @Override
    public Map seletProjectInfoToNew(String pid) {
        Map resultMap = new HashMap();
        Map result = new HashMap();
        try {
            Map plan = informationMapper.seletProjectInfoToNew(pid);
            List> map = informationMapper.seletProjectScrollToNew(pid);
            String belong_emphasis = ClobToString((Clob) plan.get("belong_emphasis_area_val"));
            String belong_area = ClobToString((Clob) plan.get("belong_area_val"));
            plan.put("belong_emphasis_area_val",belong_emphasis);
            plan.put("belong_area_val",belong_area);
            result.put("projectInfo",plan);
            result.put("projectAffiliated",map);
            ReturnUtil.createSuccess(resultMap,result,CommonRest.QUERY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            ReturnUtil.createError(resultMap, CommonRest.QUERY_FAILD);
        }

        return resultMap;
    }

亲测有效!!!

你可能感兴趣的:(Java基础知识)