如何使用jdbc在Oracle中编写长字符串的更新查询?

时间:2017-01-17 08:43:11

标签: java sql oracle

我有一个查询

String sqlQuery = 'update t1 set col1 = ? where colId = ?';

表格结构:

t1 table
--------------
colId   |col1
--------|------
Integer |Clob
-------------

col1 - 值可以超过4000个字符。 因此,当我执行查询时,我收到以下错误:

  

SQL错误:ORA-01704:字符串文字太长   01704. 00000 - “字符串字面太长”

如何编写sql更新查询。我不想要存储过程?任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

Clob字符最长可达2,147,483,647个字符,因此在您的情况下应该足够了,以防止此问题,而不是将值作为String字面值提供,仅限于4,000个字符,只要您已经注意到,请将其作为nAscii流或Clob提供,分别归功于其中一种方法setAsciiStream或感谢方法setClob(int parameterIndex, Reader reader)班级PreparedStatement

这是一个具体的例子:

PreparedStatement ps = ...

// - set the value of the input parameter to the input Reader
ps.setClob(1, new StringReader(myStringContent));
// Or setAsciiStream(1, fis);
ps.setInt(2, id);
ps.execute();