oracle中的clob字段的回车换行的处理

时间:2021-10-15 08:16:27

贴两个公共方法,一个是在你插入数据库时把字符串转换成clob类型的,另一个是在查询时,将clob字段转换成字符串;

本人开发环境:Ext+beehive+oracle

第一个方法:

 /**
* 将String转为Clob
*
* @param strPar
* @return
* @throws Exception
*/
public static Clob stringToClob(String strPar) throws Exception {
return new javax.sql.rowset.serial.SerialClob(strPar.toCharArray());
}
第二个方法:
/**	 * 将clob转为String	 * 	 * @param clobPar	 * @return	 * @throws Exception	 */	 public static String clobToString(Object clobPar) throws Exception {		String valueStr = "";		if (clobPar instanceof java.sql.Clob) {			java.sql.Clob clob = (java.sql.Clob) clobPar;			StringBuffer strOut = new StringBuffer();			String aux;			BufferedReader br = new BufferedReader(clob.getCharacterStream());			while ((aux = br.readLine()) != null) {				strOut.append(aux);				strOut.append("\n");			}			if (strOut.length() > 0) {				valueStr = strOut.substring(0, strOut.length() - 1);			}		}		return valueStr;	 }
具体应用:

查询时:例子:

HashMap<String,Object> map = pyApplyPersonMngr.getApplyPerInfoByID(oid);//获取数据
PyApplyPerson pymodel = new PyApplyPerson();//初始化model
if(map!=null){<span style="white-space:pre">	</span>String achievement = clobToString(map.get("ACHIEVEMENT"));//此处获取的是clob字段的值,并转换成字符串<span style="white-space:pre">	</span>BeanUtil.fillBeanWithMap(pymodel, map);<span style="white-space:pre">	</span>pymodel.setAchievement(achievement);}
好了,偷个懒,具体应用看自己发挥。