Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up just performing two queries, one to create the row and one to update the blob.</p> <pre><code>int id = insertRow(); updateBlob(id, blob); </code></pre> <p>Looking at the Spring source code and extracting the needed parts, I came up with this:</p> <pre><code>final KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); getJdbcTemplate().execute( "INSERT INTO lob_table (blob) VALUES (?)", new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement ps) throws SQLException { LobCreator lobCreator = lobHandler.getLobCreator(); lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length()); int rows = ps.executeUpdate(); List generatedKeys = generatedKeyHolder.getKeyList(); generatedKeys.clear(); ResultSet keys = ps.getGeneratedKeys(); if (keys != null) { try { RowMapper rowMapper = new ColumnMapRowMapper(); RowMapperResultSetExtractor rse = new RowMapperResultSetExtractor(rowMapper, 1); generatedKeys.addAll((List) rse.extractData(keys)); } finally { JdbcUtils.closeResultSet(keys); } } if (logger.isDebugEnabled()) { logger.debug("SQL update affected " + rows + " rows and returned " + generatedKeys.size() + " keys"); } return new Integer(rows); } } ); </code></pre> <p>I can't say I fully understand what is going on here. I'm not sure if the complicated method to extract the generated key is necessary in this simple case, and I'm not entirely clear about the benefit of even using JdbcTemplate when the code gets this hairy.</p> <p>Anyway, I tested the above code and it works. For my case, I decided it would complicate my code too much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload