Java更新Oracle的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("<![CDATA["+String.valueOf(map.get("FLD_HOPE_FINISH_DATE_NEW"))+"]]>");
					}
					if (attr.getValue().equals("software_req_content")) {
						fieldElement.setText("<![CDATA["+String.valueOf(map.get("FLD_SOFTWARE_REQ_CONTENT_NEW"))+"]]>");
					}
					if (attr.getValue().equals("req_complexity")) {
						fieldElement.setText("<![CDATA["+String.valueOf(map.get("FLD_REQ_COMPLEXITY_NEW_ID"))+"]]>");
					}
					if (attr.getValue().equals("is_dev_assessment")) {
						fieldElement.setText("<![CDATA["+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

相關文章
相關標籤/搜索