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

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

Introduction

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

Prototype

public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) 

Source Link

Document

Create a new NamedParameterJdbcTemplate for the given classic Spring org.springframework.jdbc.core.JdbcTemplate .

Usage

From source file:org.surfnet.cruncher.repository.StatisticsRepositoryImpl.java

@Override
public List<SpStatistic> getActiveServices(String userid, String idpEntityId) {
    NamedParameterJdbcTemplate namedJdbcTemplate = new NamedParameterJdbcTemplate(cruncherJdbcTemplate);

    String query = "select loginstamp as loginstamp, spentityid as spentityid, "
            + "spentityname as spentityname from user_log_logins " + "where " + "userid = :userId AND "
            + "idpentityid = :idpEntityId ";

    Map<String, Object> parameterMap = new HashMap<String, Object>();
    parameterMap.put("userId", userid);
    parameterMap.put("idpEntityId", idpEntityId);

    return namedJdbcTemplate.query(query, parameterMap, new RowMapper<SpStatistic>() {

        @Override/*  w w  w . j a  va  2 s .c o  m*/
        public SpStatistic mapRow(ResultSet rs, int row) throws SQLException {
            SpStatistic result = new SpStatistic();
            result.setEntryTime(rs.getTimestamp("loginstamp").getTime());
            result.setSpEntityId(rs.getString("spentityid"));
            result.setSpName(rs.getString("spentityname"));

            return result;
        }

    });
}

From source file:com.joliciel.talismane.terminology.postgres.PostGresTerminologyBase.java

void addParents(List<Term> childTerms) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_TERM + ", term_expansion_id FROM term"
            + " INNER JOIN text ON term_text_id=text_id"
            + " INNER JOIN term_expansions ON term_id = termexp_term_id"
            + " WHERE term_project_id = :term_project_id" + " AND termexp_expansion_id IN (:child_terms)";

    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("term_project_id", this.getCurrentProjectId());
    List<Integer> termIds = new ArrayList<Integer>();
    Map<Integer, PostGresTerm> childTermMap = new HashMap<Integer, PostGresTerm>();
    for (Term childTerm : childTerms) {
        PostGresTerm termInternal = (PostGresTerm) childTerm;
        if (termInternal.getParentsInternal() == null) {
            termIds.add(termInternal.getId());
            termInternal.setParentsInternal(new TreeSet<Term>());
            childTermMap.put(termInternal.getId(), termInternal);
        }/*ww  w.j a v a2 s  .c  om*/
    }
    paramSource.addValue("child_terms", termIds);

    LOG.trace(sql);
    LogParameters(paramSource);

    SqlRowSet rs = jt.queryForRowSet(sql, paramSource);
    TermMapper termMapper = new TermMapper();
    List<Term> parentTerms = new ArrayList<Term>();
    while (rs.next()) {
        Term term = termMapper.mapRow(rs);
        parentTerms.add(term);
        int childId = rs.getInt("termexp_expansion_id");
        PostGresTerm childTerm = childTermMap.get(childId);
        childTerm.getParentsInternal().add(term);
    }
    if (parentTerms.size() > 0) {
        this.addParents(parentTerms);
    }
}

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

/**
 * Executes the update defined in the passed sql 
 * @param sql The sql/* ww w. j ava2  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:dao.AdvSearchDAO.java

public static List<String> getTableNames(String scopes) {
    List<String> tables = null;
    if (StringUtils.isNotBlank(scopes)) {
        String[] scopeArray = scopes.split(",");
        List<String> scopeList = Arrays.asList(scopeArray);
        Map<String, List> param = Collections.singletonMap("scopes", scopeList);
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
                getJdbcTemplate().getDataSource());
        tables = namedParameterJdbcTemplate.queryForList(GET_DATASET_TABLE_NAMES_BY_SCOPE, param, String.class);
    } else {//from www  .  j  a v a  2 s. co m
        tables = getJdbcTemplate().queryForList(GET_DATASET_TABLE_NAMES, String.class);
    }

    return tables;
}

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

@Override
public Parameters loadParameters(int parametersId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PARAM + " FROM ocr_param WHERE param_id=:param_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("param_id", parametersId);

    LOG.info(sql);/*www  .  j av  a 2 s  .com*/
    logParameters(paramSource);
    Parameters parameters = null;
    try {
        parameters = (Parameters) jt.queryForObject(sql, paramSource,
                new ParametersMapper(this.getSecurityServiceInternal()));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return parameters;
}

From source file:com.joliciel.lefff.LefffDaoImpl.java

public void deleteAttribute(int attributeId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    String sql = "DELETE FROM lef_attribute WHERE attribute_id = :attribute_id";
    paramSource.addValue("attribute_id", attributeId);
    LOG.info(sql);/*from  w w  w  .ja v a 2  s.  com*/
    LefffDaoImpl.LogParameters(paramSource);
    jt.update(sql, paramSource);
}

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

@Override
public List<JochreDocument> findDocuments() {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_DOCUMENT + " FROM ocr_document ORDER BY doc_name";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    LOG.info(sql);//from   w  w  w .j  a v a2s . c  o  m
    logParameters(paramSource);
    @SuppressWarnings("unchecked")
    List<JochreDocument> documents = jt.query(sql, paramSource,
            new JochreDocumentMapper(this.getDocumentServiceInternal()));

    return documents;
}

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

/**
 * Batch executes the update define in the passed sql 
 * @param sql The sql//www  . j av a  2s.c om
 * @param bindSets An array of bind value arrays
 * @return an array containing the numbers of rows affected by each update in the batch
 */
public int[] batchExecute(CharSequence sql, Object[]... bindSets) {
    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(ds);
    SqlParameterSource[] sps = new SqlParameterSource[bindSets.length];
    for (int i = 0; i < bindSets.length; i++) {
        sps[i] = getBinds(sql.toString().trim().toUpperCase(), bindSets[i]);
    }
    return template.batchUpdate(sql.toString(), sps);
}

From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java

public void deleteCategory(int categoryId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    String sql = "DELETE FROM ftb_category WHERE cat_id = :cat_id";
    paramSource.addValue("cat_id", categoryId);
    LOG.info(sql);/*  w  w  w.j  av  a  2  s .c o m*/
    TreebankDaoImpl.LogParameters(paramSource);
    jt.update(sql, paramSource);
}

From source file:com.wandrell.pattern.repository.spring.SpringJdbcRepository.java

/**
 * Constructs a {@code SpringJDBCRepository} with the specified data and
 * queries./*from  w  w  w .  ja  v a 2  s  .  co m*/
 * <p>
 * It will require templated queries for the update and delete operations.
 * The parameters for these queries will be acquired automatically from the
 * entity received for each of the operations.
 * <p>
 * The recommended delete query just requires knowing the ID of the entity,
 * so it can be similar to this:
 * <p>
 * {@code DELETE FROM employees WHERE id = :id"}
 * <p>
 * The update query requires all the columns which will be updated:
 * <p>
 * {@code UPDATE employees SET name = :name WHERE id = :id}
 * <p>
 * Any additional query which may be required, such as one for acquiring all
 * the entities, will be built from the received data.
 *
 * @param type
 *            the class of the objects to be returned
 * @param source
 *            source of the data
 * @param update
 *            query template for updating an entity on the database
 * @param delete
 *            query template for deleting an entity on the database
 * @param table
 *            table linked to the repository's entities
 * @param keys
 *            primary keys of the table
 */
public SpringJdbcRepository(final Class<V> type, final DataSource source, final String update,
        final String delete, final String table, final String... keys) {
    super();

    checkNotNull(type, "Received a null pointer as the class type");
    checkNotNull(source, "Received a null pointer as the data source");
    checkNotNull(update, "Received a null pointer as the update query");
    checkNotNull(delete, "Received a null pointer as the delete query");
    checkNotNull(table, "Received a null pointer as the table");
    checkNotNull(keys, "Received a null pointer as the key columns");

    classType = type;

    // Queries
    selectAllQuery = String.format("SELECT * FROM %s", table);
    updateQueryTemplate = update;
    deleteQueryTemplate = delete;

    insertHandler = new SimpleJdbcInsert(source).withTableName(table).usingGeneratedKeyColumns(keys);

    jdbcTemplate = new NamedParameterJdbcTemplate(source);
}