Insert addition using NamedParameterJdbcTemplate

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

My question is how to do this with NamedParameterJdbcTemplate? It does not have methods that take the mysterious PreparedStatementSetter interface as a parameter.

+5
source share
3 answers

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

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);
+8
source

- , , Oracle, - , . getJdbcTemplate JdbcDaoSupport ( spring.)

getJdbcTemplate().execute(new ConnectionCallback() {

        public Object doInConnection(Connection con) throws SQLException, DataAccessException {

            PublishResponseObject responseObject = new PublishResponseObject();
            OracleCallableStatement ocstmt = null;
            CLOB clob = null;

            try {
                clob = createCLOB(xmlString, con);
                ocstmt = (OracleCallableStatement) con.prepareCall("{call schmea.publish(?)}");
                //When in insert mode and update By Pk is specified updates are possible and version numbers will be returned.
                ocstmt.setCLOB(1, clob);
             ...
             }
             finally {
               clob.close()
               stmt.close
            }
0

I am using Spring 2.5.6 + Oracle, and for me it worked right away.

// Inserts file into DB and returns the key for the new row
public Number insert(String filename, byte[] data) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("filename", filename);
    params.addValue("data", data);

    // Returns the autogenerated ID
    KeyHolder keyHolder = new GeneratedKeyHolder();
    String[] columnNames = {"ID"};

    // This is a NamedParameterJdbcTemplate
    jdbcTemplate.update(INSERT_SQL, params, keyHolder, columnNames);

    return keyHolder.getKey();
}
0
source

All Articles