Inserting Clob with NamedParameterJdbcTemplate

后端 未结 3 553
轻奢々
轻奢々 2021-01-12 20:14

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/InsertC

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 20:36

    I'm using Spring 2.5.6 + Oracle and for me it worked straight 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();
    }
    

提交回复
热议问题