Example usage for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback

List of usage examples for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback.

Prototype

PreparedStatementCallback

Source Link

Usage

From source file:org.apache.lucene.store.jdbc.handler.ActualDeleteFileEntryHandler.java

public void deleteFile(final String name) throws IOException {
    jdbcTemplate.execute(table.sqlDeleteByName(), new PreparedStatementCallback() {
        @Override//from w w  w.  j  a  v  a 2  s  . c  o  m
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.executeUpdate();
        }
    });
}

From source file:com.example.spring.jdbc.template.CustomerDao.java

public Boolean saveCustomereByPreparedStatement(final Customer e) {
    String query = "insert into customer values(?,?,?)";
    return jdbcTemplate.execute(query, new PreparedStatementCallback<Boolean>() {
        @Override//ww  w  .  j  ava2 s  .co m
        public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {

            ps.setString(1, e.getCutomerId());
            ps.setString(2, e.getName());
            ps.setString(3, e.getEmail());

            return ps.execute();

        }
    });

}

From source file:org.apache.lucene.store.jdbc.index.FetchOnOpenJdbcIndexInput.java

public void configure(final String name, final JdbcDirectory jdbcDirectory, JdbcFileEntrySettings settings)
        throws IOException {
    jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(),
            new PreparedStatementCallback<Object>() {
                @Override/*ww  w  .j  a  v a 2  s .com*/
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    try (ResultSet rs = ps.executeQuery()) {
                        if (!rs.next()) {
                            throw new JdbcStoreException(
                                    "No entry for [" + name + "] table " + jdbcDirectory.getTable());
                        }
                        length = rs.getInt(3);

                        Blob blob = rs.getBlob(2);
                        data = blob.getBytes(1, (int) length);
                        if (data.length != length) {
                            throw new IOException("read past EOF");
                        }

                    } catch (JdbcStoreException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public boolean fileExists(final String name) throws IOException {
    return ((Boolean) jdbcTemplate.execute(table.sqlSelectNameExists(), new PreparedStatementCallback() {

        @Override/*from  w  w  w  .ja v a2  s . com*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).booleanValue();
}

From source file:org.apache.lucene.store.jdbc.index.FetchOnBufferReadJdbcIndexInput.java

protected void refill() throws IOException {
    jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(),
            new PreparedStatementCallback<Object>() {
                @Override//from   www .j a va  2  s .  co  m
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    try (ResultSet rs = ps.executeQuery()) {
                        // START read blob and update length if required
                        if (!rs.next()) {
                            throw new JdbcStoreException(
                                    "No entry for [" + name + "] table " + jdbcDirectory.getTable());
                        }
                        synchronized (this) {
                            if (totalLength == -1) {
                                totalLength = rs.getLong(3);
                            }
                        }
                        // END read blob and update length if required

                        long start = bufferStart + bufferPosition;
                        long end = start + bufferSize;
                        if (end > length()) // don't read past EOF
                            end = length();
                        bufferLength = (int) (end - start);
                        if (bufferLength <= 0)
                            throw new IOException("read past EOF");

                        if (buffer == null) {
                            buffer = new byte[bufferSize]; // allocate buffer lazily
                            seekInternal(bufferStart);
                        }
                        // START replace read internal
                        Blob blob = rs.getBlob(2);
                        readInternal(blob, buffer, 0, bufferLength);

                        bufferStart = start;
                        bufferPosition = 0;
                    } catch (JdbcStoreException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:com.ebay.pulsar.analytics.dao.RDBMS.java

public Boolean execute(final String sql, final Map<String, ?> parameters) {
    return this.namedParameterJdbcTemplate.execute(sql, parameters, new PreparedStatementCallback<Boolean>() {
        @Override/* www . j  av  a 2 s .  c o m*/
        public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            //               if(ps.getParameterMetaData()!=null){
            //                  int count=ps.getParameterMetaData().getParameterCount();
            //                  for(int i=0;i<count;i++){
            //                     String name=ps.getParameterMetaData().getParameterTypeName(i);
            //                     System.out.println(i+":"+name);
            //                     if(name.equalsIgnoreCase("properties") || "config".equalsIgnoreCase(name)){
            //                        Blob bv=ps.getConnection().createBlob();
            //                        bv.setBytes(0, ((String)parameters.get(name)).getBytes());
            //                        ps.setBlob(i, bv);
            //                     }
            //                  }
            //                  return ps.execute();
            //               }else{
            //                  return ps.execute();
            //               }
            return ps.execute();
        }
    });
}

From source file:info.naiv.lab.java.tool.sqlite.exporter.component.DataAccess.java

/**
 *
 * @param sourceTempl/*from   www. j  a  va  2 s . c  o m*/
 * @param destTempl
 * @param info
 * @param valueHandler
 */
public void copy(final JdbcTemplate sourceTempl, JdbcTemplate destTempl, TableInfo info,
        final ValueHandler valueHandler) {

    final Query sq = selectTables.merge(info);

    Query q = insertSql.merge(info);
    final List<Field> fields = info.getFields();
    q.execute(destTempl, new PreparedStatementCallback<Void>() {

        @Override
        public Void doInPreparedStatement(final PreparedStatement ps) throws SQLException, DataAccessException {
            ps.getConnection().setAutoCommit(false);
            try {
                List<Integer> list = sq.query(sourceTempl, new RowMapper<Integer>() {

                    @Override
                    public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
                        int col = 1;
                        for (Field field : fields) {
                            Object val = valueHandler.handleValue(field, rs);
                            ps.setObject(col, val);
                        }
                        ps.addBatch();
                        if (rowNum % BATCH_SIZE == 0) {
                            logger.info("execBatch: {}", rowNum);
                            ps.executeBatch();
                        }
                        return rowNum;
                    }
                });

                int total = list.size();
                logger.info("total Batch: {}", total);
                if (total % BATCH_SIZE != 0) {
                    ps.executeBatch();
                }
                ps.getConnection().commit();
            } catch (SQLException | RuntimeException e) {
                ps.getConnection().rollback();
                throw e;
            }
            return null;
        }
    });
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public long fileModified(final String name) throws IOException {
    return ((Long) jdbcTemplate.execute(table.sqlSelecltLastModifiedByName(), new PreparedStatementCallback() {

        @Override//ww  w .  j ava 2  s .c o m
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).longValue();
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public void touchFile(final String name) throws IOException {
    jdbcTemplate.update(table.sqlUpdateLastModifiedByName(), new PreparedStatementCallback() {
        @Override/*from   ww  w.  j  a v  a 2  s  .co  m*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.executeUpdate();
        }
    });
}

From source file:com.nortal.petit.orm.statement.InsertStatement.java

@Override
public void exec() {
    prepare();//from   ww w . j  a  v a  2 s.  c o m
    if (!CollectionUtils.isEmpty(getBeans())) {
        if (getMapping().id() == null) {
            execBatchUpdate();
        } else {
            final KeyHolder keyHolder = new GeneratedKeyHolder();
            final InterceptorCalls interceptorCalls = new InterceptorCalls();
            getJdbcTemplate().execute(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    return con.prepareStatement(getSql(), Statement.RETURN_GENERATED_KEYS);
                }
            }, new PreparedStatementCallback<Object>() {
                @Override
                public Object doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    MappingParamFunction<B> paramFunction = new MappingParamFunction<B>(getMapping());

                    for (B bean : getBeans()) {
                        paramFunction.setBean(bean);
                        Object[] params = getParams(paramFunction);
                        Object[] queryParams = params.length == 1 && params[0] instanceof Object[]
                                ? (Object[]) params[0]
                                : params;
                        interceptorCalls.setBeanValues(bean, queryParams);
                        ArgPreparedStatementSetter.setValues(ps, queryParams, 1);

                        ps.executeUpdate();
                        extractKeys(ps);
                    }
                    return null;
                }

                /**
                 * @param ps
                 * @throws SQLException
                 */
                private void extractKeys(PreparedStatement ps) throws SQLException {
                    ResultSet keys = ps.getGeneratedKeys();
                    if (keys != null) {
                        try {
                            RowMapperResultSetExtractor<Map<String, Object>> rse = new RowMapperResultSetExtractor<Map<String, Object>>(
                                    new ColumnMapRowMapper(), 1);
                            keyHolder.getKeyList().addAll(rse.extractData(keys));
                        } finally {
                            JdbcUtils.closeResultSet(keys);
                        }
                    }
                }
            });

            try {
                Property<B, Object> idProperty = getMapping().id();
                for (int i = 0; i < getBeans().size(); i++) {
                    B bean = getBeans().get(i);
                    Object key = keyHolder.getKeyList().get(i).get(idProperty.column());
                    idProperty.write(bean, key);
                    interceptorCalls.setBeanId(bean, key);
                }
            } catch (Exception e) {
                throw new PersistenceException("InsertStatement.exec: unable to write bean primary key", e);
            }
            interceptorCalls.callInterceptor();
        }
    } else {
        getJdbcTemplate().update(getSql(), getParams(null));
    }
}