DataSource dataSource;
LobHandler lobHandler;
...
JdbcTemplate jt = new JdbcTemplate(dataSource);
int newId = 1;
Date firstPerformance = new Date(System.currentTimeMillis());
File in = new File("spring2004.jpg");
InputStream is = null;
try {
is = new FileInputStream(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
insertBlob(db, newId, firstPerformance, is, (int) in.length(), showId);
...
private void insertBlob(JdbcTemplate jt, final int newId,
final Date firstPerformance, final InputStream is,
final int blobLength, final int showId) {
jt.execute(
"INSERT INTO Show_Poster " +
"(id, first_performance, poster_image, REF_Show_id) " +
"VALUES (?, ?, ?, ?)",
new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException {
ps.setInt(1, newId);
ps.setDate(2, new java.sql.Date(firstPerformance));
lobCreator.setBlobAsBinaryStream(ps, 3, is, blobLength);
ps.setInt(4, showId);
}
}
);
}
Comments
Post a Comment