From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate
final File blobIn = new File(\"spring2004.jpg\");
final InputStream blobIs
All of this seemed way too complicated to me. This works and is simple. It uses org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.support.SqlLobValue;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
public void setBlob(Long id, byte[] bytes) {
try {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("id", id);
parameters.addValue("blob_field", new SqlLobValue(new ByteArrayInputStream(bytes), bytes.length, new DefaultLobHandler()), OracleTypes.BLOB);
jdbcTemplate.update("update blob_table set blob_field=:blob_field where id=:id", parameters);
} catch(Exception e) {
e.printStackTrace();
}
}
Please use:
addValue("p_file", noDataDmrDTO.getFile_data(), Types.BINARY)
noDataDmrDTO.getFile_data() is byte array.
{
simpleJdbcCall =
new SimpleJdbcCall(jdbcTemplate).withProcedureName("insert_uploaded_files").withCatalogName("wct_mydeq_stg_upld_pkg")
.withSchemaName("WCT_SCHEMA");
SqlParameterSource sqlParms =
new MapSqlParameterSource().addValue("p_upload_idno", Integer.parseInt("143"))
.addValue("p_file_type_idno", Integer.parseInt(noDataDmrDTO.getFile_type_idno())).addValue("p_file_name", noDataDmrDTO.getFile_name())
.addValue("p_file", noDataDmrDTO.getFile_data(), Types.BINARY).addValue("p_comments", noDataDmrDTO.getComments())
.addValue("p_userid", noDataDmrDTO.getUserid());
simpleJdbcCallResult = simpleJdbcCall.execute(sqlParms);
}
Another solution with lambda (which is not required):
jdbcTemplate.update(dbcon -> {
PreparedStatement ps = dbcon.prepareStatement("INSERT INTO ...");
ps.setString(1, yourfieldValue);
ps.setBinaryStream(2, yourInputStream, yourInputStreamSizeAsInt));
return ps;
});
NB. Sorry this does not include KeyGenerator.
I got the same issue to update blob data -- need to update image into database. than i find some solution as below. for more details update image into database
LobHandler lobHandler = new DefaultLobHandler(); statusRes = jdbcTemplate.update("update USERS set FILE_CONTENT = ?, FILE_NAME = ? WHERE lower(USER_ID) = ?", new Object[] {new SqlLobValue(image, lobHandler),fileName,userIdLower}, new int[] {Types.BLOB,Types.VARCHAR,Types.VARCHAR});
In case your underlying database is mysql, you can autogenerate your primary key. Then to insert a record into your db, you can use the following syntax for insertion:
INSERT INTO lob_table (a_blob) VALUES (?)
Maybe some like this:
public class JdbcActorDao implements ActorDao {
private SimpleJdbcTemplate simpleJdbcTemplate;
private SimpleJdbcInsert insertActor;
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
this.insertActor =
new SimpleJdbcInsert(dataSource)
.withTableName("t_actor")
.usingGeneratedKeyColumns("id");
}
public void add(Actor actor) {
Map<String, Object> parameters = new HashMap<String, Object>(2);
parameters.put("first_name", actor.getFirstName());
parameters.put("last_name", actor.getLastName());
Number newId = insertActor.executeAndReturnKey(parameters);
actor.setId(newId.longValue());
}
// ... additional methods
}