List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate NamedParameterJdbcTemplate
public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate)
From source file:com.hexin.core.dao.BaseDaoSupport.java
@Override public Integer insertReturnPK(String sql, Map<String, ?> args) { long startTime = System.currentTimeMillis(); long endTime; long durTime; debugSql(sql, args);//from w w w .j a va2s.co m DataSource dataSource = (DataSource) ContextUtil.getBean("dataSource"); NamedParameterJdbcTemplate namedJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); SqlParameterSource ps = new MapSqlParameterSource(args); KeyHolder holder = new GeneratedKeyHolder(); namedJdbcTemplate.update(sql, ps, holder); Integer pk = holder.getKey().intValue(); endTime = System.currentTimeMillis(); durTime = endTime - startTime; logger.debug("This jdbc operation costs time: " + durTime); return pk; }
From source file:com.joliciel.lefff.LefffDaoImpl.java
public Category loadCategory(String categoryCode) { NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); String sql = "SELECT " + SELECT_CATEGORY + " FROM lef_category WHERE category_code=:category_code"; MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("category_code", categoryCode); LOG.info(sql);// ww w . j av a2 s. c o m LefffDaoImpl.LogParameters(paramSource); Category category = null; try { category = (Category) jt.queryForObject(sql, paramSource, new CategoryMapper(this.getLefffServiceInternal())); } catch (EmptyResultDataAccessException ex) { ex.hashCode(); } return category; }
From source file:nl.surfnet.coin.teams.service.impl.TeamExternalGroupDaoImpl.java
@Override public List<TeamExternalGroup> getByExternalGroupIdentifiers(Collection<String> identifiers) { try {/*from w w w .j a va2s .c o m*/ NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(this.jdbcTemplate); Map<String, Object> params = new HashMap<String, Object>(); params.put("identifiers", identifiers); String s = "SELECT teg.id AS teg_id, teg.grouper_team_id, eg.id AS eg_id, eg.identifier, eg.name, eg.description, eg.group_provider " + "FROM team_external_groups AS teg " + "INNER JOIN external_groups AS eg " + "ON teg.external_groups_id = eg.id " + "WHERE eg.identifier in (:identifiers) "; return template.query(s, params, new RowMapper<TeamExternalGroup>() { @Override public TeamExternalGroup mapRow(ResultSet rs, int rowNum) throws SQLException { return mapRowToTeamExternalGroup(rs); } }); } catch (EmptyResultDataAccessException er) { return new ArrayList<TeamExternalGroup>(); } }
From source file:com.yahoo.sql4d.indexeragent.sql.DBAccessor.java
/** * Suitable for CRUD operations where no result set is expected. * @param params/*from w w w . ja va2 s. c om*/ * @param query * @return */ public boolean execute(Map<String, String> params, String query) { final AtomicBoolean result = new AtomicBoolean(false); Tuple2<DataSource, Connection> conn = null; try { conn = getConnection(); NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(conn._1()); jdbcTemplate.execute(query, params, new PreparedStatementCallback<Void>() { @Override public Void doInPreparedStatement(PreparedStatement ps) { try { result.set(ps.execute()); } catch (SQLException e) { result.set(false); } return null; } }); } catch (Exception ex) { Logger.getLogger(DBAccessor.class.getName()).log(Level.SEVERE, null, ex); result.set(false); } finally { returnConnection(conn); } return result.get(); }
From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java
@Override public void saveJochreDocument(JochreDocument jochreDocument) { NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); MapSqlParameterSource paramSource = new MapSqlParameterSource(); JochreDocumentInternal iJochreDocument = (JochreDocumentInternal) jochreDocument; paramSource.addValue("doc_filename", jochreDocument.getFileName()); paramSource.addValue("doc_name", jochreDocument.getName()); paramSource.addValue("doc_owner_id", jochreDocument.getOwnerId()); paramSource.addValue("doc_locale", jochreDocument.getLocale().getLanguage()); paramSource.addValue("doc_name_local", jochreDocument.getNameLocal()); paramSource.addValue("doc_publisher", jochreDocument.getPublisher()); paramSource.addValue("doc_city", jochreDocument.getCity()); paramSource.addValue("doc_year", jochreDocument.getYear()); paramSource.addValue("doc_reference", jochreDocument.getReference()); String sql = null;/* w ww .j a va 2s .c o m*/ if (jochreDocument.isNew()) { sql = "SELECT nextval('ocr_doc_id_seq')"; LOG.info(sql); int jochreDocumentId = jt.queryForInt(sql, paramSource); paramSource.addValue("doc_id", jochreDocumentId); sql = "INSERT INTO ocr_document (doc_id, doc_filename, doc_name, doc_locale, doc_owner_id" + ", doc_name_local, doc_publisher, doc_city, doc_year, doc_reference) " + "VALUES (:doc_id, :doc_filename, :doc_name, :doc_locale, :doc_owner_id" + ", :doc_name_local, :doc_publisher, :doc_city, :doc_year, :doc_reference)"; LOG.info(sql); logParameters(paramSource); jt.update(sql, paramSource); iJochreDocument.setId(jochreDocumentId); } else { paramSource.addValue("doc_id", jochreDocument.getId()); sql = "UPDATE ocr_document" + " SET doc_filename = :doc_filename" + ", doc_name = :doc_name" + ", doc_locale = :doc_locale" + ", doc_owner_id = :doc_owner_id" + ", doc_name_local = :doc_name_local" + ", doc_publisher = :doc_publisher" + ", doc_city = :doc_city" + ", doc_year = :doc_year" + ", doc_reference = :doc_reference" + " WHERE doc_id = :doc_id"; LOG.info(sql); logParameters(paramSource); jt.update(sql, paramSource); } }
From source file:com.joliciel.jochre.graphics.GraphicsDaoJdbc.java
@Override public JochreImage loadJochreImage(int imageId) { NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); String sql = "SELECT " + SELECT_IMAGE + " FROM ocr_image WHERE image_id=:image_id"; MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("image_id", imageId); LOG.debug(sql);/* ww w .jav a 2 s . c om*/ logParameters(paramSource); JochreImage image = null; try { image = (JochreImage) jt.queryForObject(sql, paramSource, new JochreImageMapper(this.getGraphicsServiceInternal())); } catch (EmptyResultDataAccessException ex) { ex.hashCode(); } return image; }
From source file:com.joliciel.talismane.terminology.postgres.PostGresTerminologyBase.java
@Override public List<Term> getTermsByText(final String searchText) { MONITOR.startTask("getTermsByText"); try {// w ww . jav a 2 s .co m NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); String sql = "SELECT " + SELECT_TERM + " FROM term" + " INNER JOIN text ON term_text_id=text_id" + " WHERE text_text LIKE :term_text" + " AND term_project_id = :term_project_id" + " ORDER BY text_text"; MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("term_text", searchText + "%"); paramSource.addValue("term_project_id", this.getCurrentProjectId()); LOG.trace(sql); LogParameters(paramSource); @SuppressWarnings("unchecked") List<Term> terms = jt.query(sql, paramSource, new TermMapper()); return terms; } finally { MONITOR.endTask("getTermsByText"); } }
From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java
public void savePhraseUnit(PhraseUnitInternal phraseUnit) { NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("punit_word_id", phraseUnit.getWordId() == 0 ? null : phraseUnit.getWordId()); paramSource.addValue("punit_phrase_id", phraseUnit.getPhraseId() == 0 ? null : phraseUnit.getPhraseId()); paramSource.addValue("punit_position", phraseUnit.getPositionInSentence()); paramSource.addValue("punit_lemma_id", phraseUnit.getLemmaId() == 0 ? null : phraseUnit.getLemmaId()); paramSource.addValue("punit_cat_id", phraseUnit.getCategoryId() == 0 ? null : phraseUnit.getCategoryId()); paramSource.addValue("punit_subcat_id", phraseUnit.getSubCategoryId() == 0 ? null : phraseUnit.getSubCategoryId()); paramSource.addValue("punit_morph_id", phraseUnit.getMorphologyId() == 0 ? null : phraseUnit.getMorphologyId()); paramSource.addValue("punit_compound", phraseUnit.isCompound()); paramSource.addValue("punit_pos_in_phrase", phraseUnit.getPositionInPhrase()); paramSource.addValue("punit_compound_next", phraseUnit.getNextCompoundPartId() == 0 ? null : phraseUnit.getNextCompoundPartId()); paramSource.addValue("punit_guessed_postag_id", phraseUnit.getGuessedPosTagId() == 0 ? null : phraseUnit.getGuessedPosTagId()); if (phraseUnit.isNew()) { String sql = "SELECT nextval('ftb_phrase_unit_punit_id_seq')"; LOG.info(sql);//from w w w . j a va2 s . c o m int phraseUnitId = jt.queryForInt(sql, paramSource); paramSource.addValue("punit_id", phraseUnitId); sql = "INSERT INTO ftb_phrase_unit (punit_id, punit_word_id, punit_phrase_id, punit_position, punit_lemma_id, punit_cat_id" + ", punit_subcat_id, punit_morph_id, punit_compound, punit_pos_in_phrase, punit_compound_next, punit_guessed_postag_id) " + "VALUES (:punit_id, :punit_word_id, :punit_phrase_id, :punit_position, :punit_lemma_id, :punit_cat_id" + ", :punit_subcat_id, :punit_morph_id, :punit_compound, :punit_pos_in_phrase, :punit_compound_next, :punit_guessed_postag_id)"; LOG.info(sql); TreebankDaoImpl.LogParameters(paramSource); jt.update(sql, paramSource); phraseUnit.setId(phraseUnitId); } else { paramSource.addValue("punit_id", phraseUnit.getId()); String sql = "UPDATE ftb_phrase_unit" + " SET punit_word_id = :punit_word_id" + ", punit_phrase_id=:punit_phrase_id" + ", punit_position=:punit_position" + ", punit_lemma_id=:punit_lemma_id" + ", punit_cat_id=:punit_cat_id" + ", punit_subcat_id=:punit_subcat_id" + ", punit_morph_id=:punit_morph_id" + ", punit_compound=:punit_compound" + ", punit_pos_in_phrase=:punit_pos_in_phrase" + ", punit_compound_next=:punit_compound_next" + ", punit_guessed_postag_id=:punit_guessed_postag_id" + " WHERE punit_id=:punit_id"; LOG.info(sql); TreebankDaoImpl.LogParameters(paramSource); jt.update(sql, paramSource); } }
From source file:com.joliciel.lefff.LefffDaoImpl.java
public void saveCategory(CategoryInternal category) { NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource()); MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("category_code", category.getCode()); paramSource.addValue("category_description", category.getDescription()); if (category.isNew()) { String sql = "SELECT nextval('seq_category_id')"; LOG.info(sql);//from ww w. ja v a 2s . co m int categoryId = jt.queryForInt(sql, paramSource); paramSource.addValue("category_id", categoryId); sql = "INSERT INTO lef_category (category_id, category_code, category_description) VALUES (:category_id, :category_code, :category_description)"; LOG.info(sql); LefffDaoImpl.LogParameters(paramSource); jt.update(sql, paramSource); category.setId(categoryId); } else { String sql = "UPDATE lef_category" + " SET category_code = :category_code" + ", category_description = :category_description" + " WHERE category_id = :category_id"; paramSource.addValue("category_id", category.getId()); LOG.info(sql); LefffDaoImpl.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 ww w. j a va2 s . c o 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 template * JDBC template with access to 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 JdbcTemplate template, final String update, final String delete, final String table, final String... keys) { super(); checkNotNull(type, "Received a null pointer as the class type"); checkNotNull(template, "Received a null pointer as the JDBC template"); 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(template).withTableName(table).usingGeneratedKeyColumns(keys); jdbcTemplate = new NamedParameterJdbcTemplate(template); }