Search code examples
javadatabasespringclob

Inserting Clob with NamedParameterJdbcTemplate


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.


Solution

  • 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);