Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:35:50+00:00 2026-05-20T09:35:50+00:00

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

  • 0

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

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

And also how to 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

Is there a way I could combine the two?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:35:51+00:00Added an answer on May 20, 2026 at 9:35 am

    I came here looking for the same answer, but wasn’t satisfied with what was accepted. So I did a little digging around and came up with this solution that I’ve tested in Oracle 10g and Spring 3.0

    public Long save(final byte[] blob) {
      KeyHolder keyHolder = new GeneratedKeyHolder();
      String sql = "insert into blobtest (myblob) values (?)"; //requires auto increment column based on triggers
      getSimpleJdbcTemplate().getJdbcOperations().update(new AbstractLobPreparedStatementCreator(lobHandler, sql, "ID") {
        @Override
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException {
          lobCreator.setBlobAsBytes(ps, 1, blob);
        }
      }, keyHolder);
    
      Long newId = keyHolder.getKey().longValue();
      return newId;
    }
    

    this also requires the following abstract class, based in part on Spring’s AbstractLobCreatingPreparedStatementCallback

    public abstract class AbstractLobPreparedStatementCreator implements PreparedStatementCreator {
      private final LobHandler lobHandler;
      private final String sql;
      private final String keyColumn;
      public AbstractLobPreparedStatementCreator(LobHandler lobHandler, String sql, String keyColumn) {
        this.lobHandler = lobHandler;
        this.sql = sql;
        this.keyColumn = keyColumn;
      }
      public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
        PreparedStatement ps = con.prepareStatement(sql, new String[] { keyColumn });
        LobCreator lobCreator = this.lobHandler.getLobCreator();
        setValues(ps, lobCreator);
        return ps;
      }
      protected abstract void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException;
    }
    

    Also, the table you create in Oracle should have an auto-incremented column for the id using a sequence and trigger. The trigger is necessary because otherwise you’d have to use Spring’s NamedParameterJdbcOperations (to do the sequence.nextval in your SQL) which doesn’t seem to have support for KeyHolder (which I use to retrieve the auto-gen id). See this blog post (not my blog) for more info: http://www.lifeaftercoffee.com/2006/02/17/how-to-create-auto-increment-columns-in-oracle/

    create table blobtest (
    id number primary key,
    myblob blob);
    
    create sequence blobseq start with 1 increment by 1;
    
    CREATE OR REPLACE TRIGGER blob_trigger
    BEFORE INSERT
    ON blobtest
    REFERENCING NEW AS NEW
    FOR EACH ROW
    BEGIN
    SELECT blobseq.nextval INTO :NEW.ID FROM dual;
    end;
    /
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble retrieving data from my database using Spring Jdbc. Here's my issue:
I was using spring jdbc template to insert some data into the database and
Can I access spring bean that exposed using http invoker (server) from GWT application
How do I get Spring to load Hibernate's properties from hibernate.cfg.xml ? We're using
Does anyone know how what Spring Jdbc template method I could use to execute
I'm developing a DAO using Spring JdbcDaoSupport and would like to know if anyone
I am attempting to pull some information from my tnsnames file using regex. I
I need to get the spring application context from a non bean object. In
Is it possible to use a Spring container for DI from inside Eclipse plugins?
We try to convert from string to Byte[] using the following Java code: String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.