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

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

Introduction

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

Prototype

@Override
    @Nullable
    public <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
            throws DataAccessException 

Source Link

Usage

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

public Morphology loadMorphology(String morphologyCode) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_MORPHOLOGY + " FROM ftb_morph WHERE morph_code=:morph_code";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("morph_code", morphologyCode);

    LOG.info(sql);/*www. j av a2  s .com*/
    TreebankDaoImpl.LogParameters(paramSource);
    Morphology morphology = null;
    try {
        morphology = (Morphology) jt.queryForObject(sql, paramSource, new MorphologyMapper());
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return morphology;
}

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

public PhraseUnit loadPhraseUnit(int phraseUnitId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE_UNIT + " FROM ftb_phrase_unit WHERE punit_id=:punit_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("punit_id", phraseUnitId);

    LOG.info(sql);/*from www  .j  a  va 2s  .c o  m*/
    TreebankDaoImpl.LogParameters(paramSource);
    PhraseUnit phraseUnit = null;
    try {
        phraseUnit = (PhraseUnit) jt.queryForObject(sql, paramSource,
                new PhraseUnitMapper(this.treebankServiceInternal));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return phraseUnit;
}

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

public TreebankFile loadTreebankFile(String fileName) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_TREEBANK_FILE + " FROM ftb_file WHERE file_name=:file_name";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("file_name", fileName);

    LOG.info(sql);//from w w w  . j av a2  s .c  o m
    TreebankDaoImpl.LogParameters(paramSource);
    TreebankFile treebankFile = null;
    try {
        treebankFile = (TreebankFile) jt.queryForObject(sql, paramSource,
                new TreebankFileMapper(treebankServiceInternal));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return treebankFile;
}

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

@Override
public TextItem loadTextItem(String externalId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_TEXT_ITEM + " FROM ftb_text WHERE text_external_id=:text_external_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("text_external_id", externalId);

    LOG.info(sql);/*ww w.  ja  va 2 s. c  o m*/
    TreebankDaoImpl.LogParameters(paramSource);
    TextItem textItem = null;
    try {
        textItem = (TextItem) jt.queryForObject(sql, paramSource, new TextItemMapper());
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return textItem;
}

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

public PhraseType loadPhraseType(String phraseTypeCode) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE_TYPE + " FROM ftb_phrase_type WHERE ptype_code=:ptype_code";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("ptype_code", phraseTypeCode);

    LOG.info(sql);//from   w  w  w.j  av  a2  s.  c  o  m
    TreebankDaoImpl.LogParameters(paramSource);
    PhraseType phraseType = null;
    try {
        phraseType = (PhraseType) jt.queryForObject(sql, paramSource, new PhraseTypeMapper());
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return phraseType;
}

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

public SubCategory loadSubCategory(int subCategoryId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_SUB_CATEGORY + " FROM ftb_sub_category WHERE subcat_id = :subcat_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("subcat_id", subCategoryId);

    LOG.info(sql);/*  w  ww. jav  a2 s  . c om*/
    TreebankDaoImpl.LogParameters(paramSource);
    SubCategory subCategory = null;
    try {
        subCategory = (SubCategory) jt.queryForObject(sql, paramSource, new SubCategoryMapper());
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return subCategory;
}

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

public SubCategory loadSubCategory(Category category, String subCategoryCode) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_SUB_CATEGORY
            + " FROM ftb_sub_category WHERE subcat_cat_id = :subcat_cat_id AND subcat_code=:subcat_code";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("subcat_cat_id", category.getId());
    paramSource.addValue("subcat_code", subCategoryCode);

    LOG.info(sql);// ww w  .  ja  v a 2  s  . com
    TreebankDaoImpl.LogParameters(paramSource);
    SubCategory subCategory = null;
    try {
        subCategory = (SubCategory) jt.queryForObject(sql, paramSource, new SubCategoryMapper());
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return subCategory;
}

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

public Word loadWord(String text, String originalText) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    if (text == null)
        text = "";
    String sql = "SELECT " + SELECT_WORD + " FROM ftb_word" + " WHERE word_text=:word_text"
            + " AND word_original_text=:word_original_text";
    paramSource.addValue("word_text", text);
    paramSource.addValue("word_original_text", originalText);

    LOG.info(sql);/*from   w w w. j  av a2  s  .c o m*/
    TreebankDaoImpl.LogParameters(paramSource);
    Word word = null;
    try {
        word = (Word) jt.queryForObject(sql, paramSource, new WordMapper(this.treebankServiceInternal));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return word;
}

From source file:net.urosk.mifss.core.lib.db.PaginationHelper.java

public DataResult fetchPage(final DataSource dataSource, final NamedParameterJdbcTemplate jt,
        final String sqlFetchRows, final MapSqlParameterSource namedParameters, RowMapper rowMapper,
        final long offset, final long limit) {

    String countSql = StringUtils.replace(COUNT_QUERY, ":query", sqlFetchRows);

    final int rowCount = jt.queryForObject(countSql, namedParameters, Integer.class);

    Map<String, String> params = new HashMap<String, String>();
    params.put("query", sqlFetchRows);
    params.put("from", offset + "");
    params.put("to", (offset + limit) + "");
    params.put("limit", (limit) + "");

    // get database product name for paging!
    String dbProductName = getDatabaseProductName(dataSource);
    dbProductName = dbProductName.toLowerCase();

    String pagingSql = "NOT IMPELEMNTED FOR THIS DATABASE! - " + dbProductName;

    if (dbProductName.contains("mysql")) {

        pagingSql = StringUtils.replace(MYSQL_PAGING, ":query", sqlFetchRows);
        pagingSql = StringUtils.replace(pagingSql, ":from", offset + "");
        pagingSql = StringUtils.replace(pagingSql, ":limit", (limit) + "");

    } else if (dbProductName.contains("oracle")) {

        pagingSql = StringUtils.replace(ORACLE_PAGING, ":query", sqlFetchRows);
        pagingSql = StringUtils.replace(pagingSql, ":from", offset + "");
        pagingSql = StringUtils.replace(pagingSql, ":to", (offset + limit) + "");

    } else {/*from  ww w. j  av a 2s  . c  om*/
        logger.error("Paging for selected database is not yet implemented!! + Check paginationHelper for this");

    }

    List list = jt.query(pagingSql, namedParameters, rowMapper);

    // create the page object
    final DataResult dataResult = new DataResult();
    dataResult.setCount(rowCount);
    dataResult.setFrom(offset);
    dataResult.setLimit(limit);
    dataResult.setList(list);

    return dataResult;
}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testQueryForObjectWithRowMapper() throws SQLException {
    mockResultSet.next();//from   w  w  w  .j  ava2 s .c  om
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.DECIMAL);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setString(2, "UK");
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("id", new SqlParameterValue(Types.DECIMAL, new Integer(1)));
    params.put("country", "UK");
    Customer cust = (Customer) jt.queryForObject(SELECT_NAMED_PARAMETERS, params, new RowMapper() {
        public Object mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
}