Inserting Clob with NamedParameterJdbcTemplate

jabal picture jabal · Apr 26, 2011 · Viewed 13k times · Source

I usually use the lobHandler + JdbcTemplate + PreparedStatementSetter triplet to insert my Clob into the database, as I saw on http://www.java2s.com/Code/Java/Spring/InsertClobData.htm

My question is how to do this with a NamedParameterJdbcTemplate? It has no methods accepting the mysterious PreparedStatementSetter interface as a parameter.

Answer

Jonas picture Jonas · Apr 26, 2011

This works without using the PreparedStatementCallback and lobHandler, at least when inserting a string.

NamedParameterJdbcTemplate template; //= new NamedParameterJdbcTemplate(pDs);
String INSERT_STMT = "INSERT INTO MYTABLE (ID, LONG_TEXT) VALUES (:id, :clob)";
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("id", 1L, Types.NUMERIC);
paramSource.addValue("clob", "a long long text", Types.CLOB);
template.update(INSERT_STMT, paramSource);