java 从字符串到 Clob 和 Clob 到字符串的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11391170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion From String to Clob and Clob to String
提问by Reddy
I am trying to save XML response which comes as StrignBuffer to method. I need to save this string data to Sql as CLOB and retrieve the same.
我正在尝试将作为 StrignBuffer 的 XML 响应保存到方法中。我需要将此字符串数据作为 CLOB 保存到 Sql 并检索相同的数据。
The response can be upto 3-4Kb, which should be retrieved to StringBuffer again
响应最大3-4Kb,需要再次检索到StringBuffer
Can any one give me tutorial or sample code which doesn't include db specific methods/jars.
任何人都可以给我不包含数据库特定方法/jars 的教程或示例代码。
I can change db column type if CLOB is not suitable or any other better alternative.
如果 CLOB 不合适或任何其他更好的选择,我可以更改 db 列类型。
Please suggest.
请建议。
采纳答案by Amir Pashazadeh
You can create a CLOB
by calling connection.createClob()
, then you can populate it with methods such as setString
, setCharacterStream
or setAsciiStream
.
您可以CLOB
通过调用创建connection.createClob()
,然后可以使用诸如setString
、setCharacterStream
或 之类的方法填充它setAsciiStream
。
Create a PreparedStatement
and call its setClob()
to store your CLOB in the databae.
创建一个PreparedStatement
并调用它setClob()
以将您的 CLOB 存储在数据库中。
Retrieving the data is just the same, read it from a ResultSet
and call getCharacterStream
, getAsciiStream
or getSubStream
on it.
检索数据也是一样的,从 a 中读取ResultSet
并调用getCharacterStream
,getAsciiStream
或者getSubStream
在它上面。
回答by gavenkoa
For NamedParameterJdbcTemplte
:
对于NamedParameterJdbcTemplte
:
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("clob", "a long long text", Types.CLOB);
namedTemplate.update(INSERT_STMT, paramSource);
For JdbcTemplate
:
对于JdbcTemplate
:
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
and:
和:
private @Autowire LobHandler lobHandler;
jdbcTemplate.update("insert into customer(id,name,comments) "
+ "values (?, ?, ?)", new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, 2L);
ps.setString(2, "Jon");
lobHandler.getLobCreator().setClobAsString(ps, 3, "Clob data");
}
});
To extract String from ResultSet
:
从 中提取字符串ResultSet
:
inc.setDetail(lobHandler.getClobAsString(rs, "DETAIL"));
Look to official docs: http://docs.spring.io/spring/docs/3.0.x/reference/jdbc.html#jdbc-lob
查看官方文档:http: //docs.spring.io/spring/docs/3.0.x/reference/jdbc.html#jdbc-lob