從春天JDBC文檔,我知道如何insert a blob using JdbcTemplateSpring的JdbcTemplate - 插入BLOB,並返回生成的密鑰
final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_blob) VALUES (?, ?)",
new AbstractLobCreatingPreparedStatementCallback(lobhandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException {
ps.setLong(1, 1L);
lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length());
}
}
);
blobIs.close();
以及如何retrieve the generated key of a newly inserted row:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"id"});
ps.setString(1, name);
return ps;
}
},
keyHolder);
// keyHolder.getKey() now contains the generated key
有沒有一種方法,我可以結合他們倆?
接受這個答案,因爲所有的upvotes。你應該用'getJdbcTemplate()'替換'getSimpleJdbcTemplate()。getJdbcOperations()',因爲現在棄用了SimpleJdbcTemplate。 – itsadok 2012-10-09 07:26:04