Clob errorORA-01704: string literal too long

Clob字段在SQL语句中一般可当作字符串处理。
但当字符串的长度>4000时,会出现errorORA-01704: string literal too long错误。
因为SQL语句超过了允许的长度,在PL/SQL中可能限制更短。

处理方法:
java中可引用preparedStatement。PL/SQL中则可使用变量。

tomcat6.0和weblogic9.2下测试通过
/**
	 * @param conn
	 *            连接
	 * @param table
	 *            表名
	 * @param idColumn
	 *            查询关键字列名
	 * @param idValue
	 *            查询关键字值
	 * @param clobColumn
	 *            CLOB列名
	 * @param clobValue
	 *            CLOB值
	 * @return
	 */
	public int updateClob(Connection conn, String table, String idColumn, String idValue, String clobColumn,
			String clobValue) {
		int se = -1;
		ResultSet rs = null;
		PreparedStatement pst = null;
		boolean defaultCommit = true;
		try {
			defaultCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);


			// 将原Clob字段置空
			String updateToEmptySql = "UPDATE " + table + " SET " + clobColumn + " = EMPTY_CLOB() WHERE " + idColumn
					+ "='" + idValue + "'";
			pst = conn.prepareStatement(updateToEmptySql);
			pst.executeUpdate();

			// 更新Clob字段
			String getForUpdateSql = "select " + clobColumn + " from " + table + " where " + idColumn + " = '"
					+ idValue + "' for update";
			pst = conn.prepareStatement(getForUpdateSql);
			rs = pst.executeQuery();

			if (rs.next()) {
				oracle.sql.CLOB clob = (oracle.sql.CLOB) (rs.getClob(clobColumn));
				clob.putString(1, clobValue);
				String sql = "update " + table + " set " + clobColumn + "=? where " + idColumn + "='" + idValue + "'";
				pst = conn.prepareStatement(sql);
				pst.setClob(1, clob);
				se = pst.executeUpdate();
				conn.commit();
				conn.setAutoCommit(defaultCommit);
			}
		} catch (SQLException e) {
			se = -1;
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return se;
	}

你可能感兴趣的:(oracle,sql)