Example usage for org.springframework.jdbc.support.lob LobCreator setBlobAsBytes

List of usage examples for org.springframework.jdbc.support.lob LobCreator setBlobAsBytes

Introduction

In this page you can find the example usage for org.springframework.jdbc.support.lob LobCreator setBlobAsBytes.

Prototype

void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content) throws SQLException;

Source Link

Document

Set the given content as bytes on the given statement, using the given parameter index.

Usage

From source file:org.apache.camel.component.jdbc.aggregationrepository.JdbcAggregationRepository.java

@SuppressWarnings("unchecked")
public Exchange add(final CamelContext camelContext, final String correlationId, final Exchange exchange) {
    return (Exchange) transactionTemplate.execute(new TransactionCallback() {

        public Exchange doInTransaction(TransactionStatus status) {
            String sql;//ww w.  j  a  va  2  s  .  c  om
            Exchange result = null;
            final String key = correlationId;

            try {
                final byte[] data = codec.marshallExchange(camelContext, exchange);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Adding exchange with key: [" + key + "]");
                }

                String insert = "INSERT INTO " + getRepositoryName() + " (" + EXCHANGE + ", " + ID
                        + ") VALUES (?, ?)";
                String update = "UPDATE " + getRepositoryName() + " SET " + EXCHANGE + " = ? WHERE " + ID
                        + " = ?";

                boolean present = jdbcTemplate.queryForInt(
                        "SELECT COUNT (*) FROM " + getRepositoryName() + " WHERE " + ID + " = ?", key) != 0;
                sql = present ? update : insert;

                // Recover existing exchange with that ID
                if (isReturnOldExchange() && present) {
                    result = get(key, getRepositoryName(), camelContext);
                }

                jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(getLobHandler()) {
                    @Override
                    protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                        lobCreator.setBlobAsBytes(ps, 1, data);
                        ps.setString(2, key);
                    }
                });

            } catch (IOException e) {
                throw new RuntimeException("Error adding to repository " + repositoryName + " with key " + key,
                        e);
            }

            return result;
        }
    });

}

From source file:org.apache.camel.component.jdbc.aggregationrepository.JdbcAggregationRepository.java

public void remove(final CamelContext camelContext, final String correlationId, final Exchange exchange) {
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final String key = correlationId;
            final String confirmKey = exchange.getExchangeId();
            try {
                final byte[] data = codec.marshallExchange(camelContext, exchange);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Removing key [" + key + "]");
                }/*w w  w. j  a  v a  2s . c o m*/

                jdbcTemplate.update("DELETE FROM " + getRepositoryName() + " WHERE " + ID + " = ?",
                        new Object[] { key });

                jdbcTemplate.execute(
                        "INSERT INTO " + getRepositoryNameCompleted() + " (" + EXCHANGE + ", " + ID
                                + ") VALUES (?, ?)",
                        new AbstractLobCreatingPreparedStatementCallback(getLobHandler()) {
                            @Override
                            protected void setValues(PreparedStatement ps, LobCreator lobCreator)
                                    throws SQLException {
                                lobCreator.setBlobAsBytes(ps, 1, data);
                                ps.setString(2, confirmKey);
                            }
                        });
            } catch (IOException e) {
                throw new RuntimeException("Error removing key " + key + " from repository " + repositoryName,
                        e);
            }
        }
    });
}

From source file:org.easyrec.store.dao.plugin.impl.PluginDAOMysqlImpl.java

public void storePlugin(PluginVO plugin) {

    final PluginVO pluginParam = plugin;
    try {//from  w  ww  . ja v a2s .com

        getJdbcTemplate().execute(SQL_ADD_PLUGIN, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

            @Override
            protected void setValues(PreparedStatement ps, LobCreator lobCreator)
                    throws SQLException, DataAccessException {
                ps.setString(1, pluginParam.getDisplayName());
                ps.setString(2, pluginParam.getPluginId().getUri().toString());
                ps.setString(3, pluginParam.getPluginId().getVersion().toString());
                ps.setString(4, pluginParam.getOrigFilename());
                ps.setString(5, pluginParam.getState());
                lobCreator.setBlobAsBytes(ps, 6, pluginParam.getFile());
                ps.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
            }
        });

    } catch (DataIntegrityViolationException e) {

        logger.info("Updating plugin!");
        getJdbcTemplate().execute(SQL_UPDATE_PLUGIN,
                new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

                    @Override
                    protected void setValues(PreparedStatement ps, LobCreator lobCreator)
                            throws SQLException, DataAccessException {
                        ps.setString(1, pluginParam.getDisplayName());
                        ps.setString(2, pluginParam.getOrigFilename());
                        ps.setString(3, pluginParam.getState());
                        lobCreator.setBlobAsBytes(ps, 4, pluginParam.getFile());
                        ps.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
                        ps.setString(6, pluginParam.getPluginId().getUri().toString());
                        ps.setString(7, pluginParam.getPluginId().getVersion().toString());
                    }
                });
    } catch (Exception ex) {
        logger.error("An error occured storing the plugin! " + ex);
    }
}

From source file:org.springframework.integration.cluster.strictorder.JdbcEntityQueues.java

public void add(final String key, Message<?> message) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from   w  w w.  ja  v  a  2s.  com
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(message);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    this.jdbcOperations.execute(getInsertSql(),
            new AbstractLobCreatingPreparedStatementCallback(getLobHandler()) {
                protected void setValues(PreparedStatement ps, LobCreator lobCreator)
                        throws SQLException, DataAccessException {
                    ps.setString(1, key);
                    ps.setString(2, dispatcherName);
                    lobCreator.setBlobAsBytes(ps, 3, bos.toByteArray());
                }
            });
}