Example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate update

List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate update

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate update.

Prototype

@Override
    public int update(String sql, Map<String, ?> paramMap) throws DataAccessException 

Source Link

Usage

From source file:cherry.foundation.etl.ExtractorImplTest.java

private void createData(int num) {
    NamedParameterJdbcTemplate template = getTemplate();
    for (int i = 0; i < num; i++) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("name", "name_" + i);
        paramMap.put("address", "address_" + i);
        template.update("INSERT INTO etl_extr_ldr_test (name, address) VALUES (:name, :address)", paramMap);
    }/* w  w  w.  j  av a  2  s  .c  o m*/
}

From source file:cherry.foundation.sql.SqlExecutorImpl.java

/**
 * SQL?// www.  j  a  va 2s. c o  m
 * 
 * @param dataSource 
 * @param reader SQL??
 * @param paramMap SQL???
 * @param continueOnError SQL?????
 * @throws IOException SQL???
 */
@Override
public void execute(DataSource dataSource, Reader reader, Map<String, ?> paramMap, boolean continueOnError)
        throws IOException {

    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);

    String sql;
    while ((sql = SimpleSqlParser.nextStatement(reader)) != null) {

        sql = sql.trim();
        if (sql.isEmpty()) {
            continue;
        }

        try {
            template.update(sql, paramMap);
        } catch (DataAccessException ex) {
            if (!continueOnError) {
                throw ex;
            }
        }
    }
}

From source file:org.tradex.jdbc.JDBCHelper.java

/**
 * Executes the update defined in the passed sql 
 * @param sql The sql/*from w w  w.  j  av  a2 s. c o  m*/
 * @param binds The bind values
 * @return the number of rows updated
 */
public int execute(CharSequence sql, Object... binds) {
    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(ds);
    return template.update(sql.toString(), getBinds(sql.toString().trim().toUpperCase(), binds));
}

From source file:no.magott.training.BusinessSchemaDbIntegrationTest.java

@Test
public void createAndInsertIntoSpursMatch() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    EmbeddedDatabase db = builder.addScript("classpath:business-schema.sql").build();
    String sql = "INSERT INTO SPURS_MATCH(MATCH_DATE, COMPETITION, OPPOSITION, VENUE, HALF_TIME_SCORE, GOALS_FOR, GOALS_AGAINST) VALUES(:date, :competition, :opposition, :venue, :halfTimeScore, :spursGoals, :oppositionGoals)";
    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(db);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("date", new Date());
    params.put("opposition", "Ar5ena1");
    params.put("halfTimeScore", "(2-0)");
    params.put("venue", "WHL");
    params.put("spursGoals", 5);
    params.put("oppositionGoals", 1);
    params.put("competition", "FACUP");
    jdbcTemplate.update(sql, params);
    db.shutdown();//  w  w w. j  ava2s . c  om
}

From source file:com.joliciel.jochre.boundaries.BoundaryDaoJdbc.java

@Override
public void deleteSplit(Split split) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("split_id", split.getId());
    String sql = null;/*  ww  w . j a v  a 2s.c  om*/

    sql = "delete from ocr_split where split_id = :split_id";

    LOG.debug(sql);
    logParameters(paramSource);
    jt.update(sql, paramSource);
}

From source file:cherry.foundation.etl.LoaderImpl.java

/**
 * ?/*from  ww  w.j a va 2  s.  co m*/
 * 
 * @param dataSource 
 * @param sql SQL
 * @param provider ??
 * @param limit ???
 * @return ????
 * @throws LimiterException ????
 * @throws IOException ??
 */
@Override
public LoadResult load(DataSource dataSource, String sql, Provider provider, Limiter limiter)
        throws LimiterException, IOException {

    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource);

    limiter.start();
    try {

        provider.begin();

        long totalCount = 0;
        long successCount = 0;
        long failedCount = 0;
        Map<String, ?> data;
        while ((data = provider.provide()) != null) {

            totalCount += 1;

            try {
                template.update(sql, data);
                successCount += 1;
            } catch (DataAccessException ex) {
                if (allowedFailCount <= 0) {
                    throw ex;
                }

                failedCount += 1;
                logger.warn("SQL failed: count={}, message={}", failedCount, ex.getMessage());
                if (allowedFailCount < failedCount) {
                    throw ex;
                }
            }

            if (batchCount > 0 && batchCount <= totalCount) {
                break;
            }

            limiter.tick();
        }

        provider.end();

        LoadResult result = new LoadResult();
        result.setTotalCount(totalCount);
        result.setSuccessCount(successCount);
        result.setFailedCount(failedCount);
        return result;

    } finally {
        limiter.stop();
    }
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public void deleteJochrePage(JochrePage page) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("page_id", page.getId());
    String sql = null;//from w w  w.j  a  v a2  s  .co m

    sql = "delete from ocr_page" + " WHERE page_id = :page_id";

    LOG.info(sql);
    logParameters(paramSource);
    jt.update(sql, paramSource);
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public void replaceAuthors(JochreDocument doc) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    paramSource.addValue("docauthor_doc_id", doc.getId());
    String sql = "DELETE FROM ocr_doc_author_map WHERE docauthor_doc_id = :docauthor_doc_id";
    LOG.info(sql);//from www .  j  a  v  a2 s  .co m
    logParameters(paramSource);
    jt.update(sql, paramSource);

    for (Author author : doc.getAuthors()) {
        paramSource = new MapSqlParameterSource();

        paramSource.addValue("docauthor_doc_id", doc.getId());
        paramSource.addValue("docauthor_author_id", author.getId());

        sql = "INSERT INTO ocr_doc_author_map (docauthor_doc_id, docauthor_author_id)"
                + " VALUES (:docauthor_doc_id, :docauthor_author_id)";

        LOG.info(sql);
        logParameters(paramSource);
        jt.update(sql, paramSource);
    }
}

From source file:com.joliciel.jochre.security.SecurityDaoJdbc.java

@Override
public void saveParametersInternal(ParametersInternal parameters) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    paramSource.addValue("param_last_failed_login", parameters.getLastFailedLoginAttempt());
    paramSource.addValue("param_captcha_interval", parameters.getCaptachaIntervalSeconds());
    String sql = null;// w ww  .  j ava  2s. co m

    paramSource.addValue("param_id", parameters.getId());

    sql = "UPDATE ocr_param" + " SET param_last_failed_login = :param_last_failed_login"
            + ", param_captcha_interval = :param_captcha_interval" + " WHERE param_id = :param_id";

    LOG.info(sql);
    logParameters(paramSource);
    jt.update(sql, paramSource);

}

From source file:com.joliciel.jochre.boundaries.BoundaryDaoJdbc.java

@Override
public void saveSplit(Split split) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    SplitInternal iSplit = (SplitInternal) split;

    paramSource.addValue("split_shape_id", split.getShapeId());
    paramSource.addValue("split_position", split.getPosition());
    String sql = null;/*from   w  w w . j a v  a 2  s  . com*/

    if (split.isNew()) {
        sql = "SELECT nextval('ocr_split_id_seq')";
        LOG.debug(sql);
        int splitId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("split_id", splitId);

        sql = "INSERT INTO ocr_split (split_id, split_shape_id, split_position) "
                + "VALUES (:split_id, :split_shape_id, :split_position)";

        LOG.debug(sql);
        logParameters(paramSource);
        jt.update(sql, paramSource);

        iSplit.setId(splitId);
    } else {
        paramSource.addValue("split_id", split.getId());

        sql = "UPDATE ocr_split" + " SET split_shape_id = :split_shape_id"
                + ", split_position = :split_position" + " WHERE split_id = :split_id";

        LOG.debug(sql);
        logParameters(paramSource);
        jt.update(sql, paramSource);
    }
}