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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:54:09  来源:igfitidea点击:

Conversion From String to Clob and Clob to String

javasqlstringclob

提问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 CLOBby calling connection.createClob(), then you can populate it with methods such as setString, setCharacterStreamor setAsciiStream.

您可以CLOB通过调用创建connection.createClob(),然后可以使用诸如setStringsetCharacterStream或 之类的方法填充它setAsciiStream

Create a PreparedStatementand call its setClob()to store your CLOB in the databae.

创建一个PreparedStatement并调用它setClob()以将您的 CLOB 存储在数据库中。

Retrieving the data is just the same, read it from a ResultSetand call getCharacterStream, getAsciiStreamor getSubStreamon it.

检索数据也是一样的,从 a 中读取ResultSet并调用getCharacterStreamgetAsciiStream或者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