Java更新Oracle的clob类型字段

Java更新Oracle的clob类型字段

  • 1.查询该clob字段
  • 2.处理该clob字段查询结果
  • 3.更新该clob字段查询结果

1.查询该clob字段

<select id="selectItsmTask" resultType="java.util.HashMap"
		parameterType="java.util.HashMap">
		select a.task_oid,a.task_history from itsm_task a where a.task_oid = #{REQ_TASK_OID,jdbcType=VARCHAR}
	select>

2.处理该clob字段查询结果

/**
	 * Clob类型 转String
	 * 
	 * @param clob
	 * @return
	 */
	private String ClobToString(Clob clob) {
		String ret = "";
		try {
			Reader read = clob.getCharacterStream();
			BufferedReader br = new BufferedReader(read);
			String s;
			s = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (s != null) {
				sb.append(s);
				s = br.readLine();
			}
			ret = sb.toString();
			if (br != null) {
				br.close();
			}
			if (read != null) {
				read.close();
			}
		} catch (SQLException e) {
			logger.error("Clob转String失败(sql):" + e);
		} catch (IOException e) {
			logger.error("Clob转String失败:" + e);
		}
		return ret;
	}
	/**
	 * 修改clob字段内容
	 * 
	 * @param map
	 * @return
	 */
	private void updateClob(Map<String, Object> map) {
		Map<String, Object> updateInfo = new HashMap<String, Object>();
		updateInfo.put("reqTaskOid", map.get("REQ_TASK_OID"));
		// 获取itsm_task中的task_history字段内容
		Map<String, Object> result = fieldsModifyMapper.selectItsmTask(map);
		Clob columnClob = (Clob) result.get("TASK_HISTORY");
		String taskHistory = ClobToString(columnClob);
		// 修改itsm_task中的task_history字段内容
		// 创建xml的Document对象
		try {
			Document document = DocumentHelper.parseText(taskHistory);
			List lists = document.selectNodes("/workflow/node/fields/field");//寻找field节点
			Iterator iter = lists.iterator();
			while (iter.hasNext()) {
				Element fieldElement = (Element) iter.next();
				if (fieldElement.attributes().size() > 0) {
					Attribute attr = (Attribute) fieldElement.attributes().get(0);
					if (attr.getValue().equals("hope_finish_date")) {
						fieldElement.setText("+String.valueOf(map.get("FLD_HOPE_FINISH_DATE_NEW"))+"]]>");
					}
					if (attr.getValue().equals("software_req_content")) {
						fieldElement.setText("+String.valueOf(map.get("FLD_SOFTWARE_REQ_CONTENT_NEW"))+"]]>");
					}
					if (attr.getValue().equals("req_complexity")) {
						fieldElement.setText("+String.valueOf(map.get("FLD_REQ_COMPLEXITY_NEW_ID"))+"]]>");
					}
					if (attr.getValue().equals("is_dev_assessment")) {
						fieldElement.setText("+String.valueOf(map.get("FLD_IS_DEV_ASSESSMENT_NEW_ID"))+"]]>");
					}
				}
			}
			updateInfo.put("taskHistory", StringEscapeUtils.unescapeXml(document.asXML()));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		fieldsModifyMapper.updateItsmTask(updateInfo);
	}

3.更新该clob字段查询结果

<update id="updateItsmTask" parameterType="java.util.HashMap">
		update itsm_task a 
		<set>
			<if test="taskHistory != null">
				a.task_history =#{taskHistory,jdbcType=CLOB} 
			if>
		set>
		where a.task_oid = #{reqTaskOid,jdbcType=VARCHAR} 
	update>

**注意:**使用StringEscapeUtils.unescapeXml(String str)方法,防止标签符号在存入Oracle数据库的时候被转义。

你可能感兴趣的:(Java)