Spring JdbcTemplate - Insert blob and return generated key

后端 未结 11 1790
猫巷女王i
猫巷女王i 2020-12-15 06:06

From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate

final File blobIn = new File(\"spring2004.jpg\");
final InputStream blobIs         


        
相关标签:
11条回答
  • 2020-12-15 06:12

    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();
            }
        }
    
    0 讨论(0)
  • 2020-12-15 06:13

    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);
    
    }
    
    0 讨论(0)
  • 2020-12-15 06:16

    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.

    0 讨论(0)
  • 2020-12-15 06:16

    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});
    

    0 讨论(0)
  • 2020-12-15 06:21

    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 (?)
    
    0 讨论(0)
  • 2020-12-15 06:23

    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
    }
    
    0 讨论(0)
提交回复
热议问题