Example usage for org.springframework.jdbc.core JdbcTemplate query

List of usage examples for org.springframework.jdbc.core JdbcTemplate query

Introduction

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

Prototype

@Override
    public <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper) throws DataAccessException 

Source Link

Usage

From source file:gov.nih.nci.ncicb.cadsr.bulkloader.dao.read.BulkLoaderReadDAOImpl.java

public List<String> getAlternateNameTypes() {
    if (alternateNameTypes == null) {
        String sql = "select distinct DETL_NAME from DESIGNATION_TYPES_LOV";
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        alternateNameTypes = (List<String>) jdbcTemplate.query(sql, new ResultSetExtractor() {
            @Override//from  ww  w  .j  av  a  2s .  co m
            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<String> altNameTypes = new ArrayList<String>();
                while (rs.next()) {
                    altNameTypes.add(rs.getString(1));
                }
                return altNameTypes;
            }
        });
    }

    return alternateNameTypes;
}

From source file:com.home.ln_spring.ch8.dao.jdbc.annotation.JdbcContactDao.java

@Override
public List<Contact> findAllWithDetail() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataDource());
    String sql = "select c.contact_id, c.first_name, c.last_name, c.birth_date, "
            + "t.contact_tel_detail_id, t.tel_type, t.tel_number from contact c "
            + "left join contact_tel_detail t on c.contact_id = t.contact_id";

    return jdbcTemplate.query(sql, new ContactWithDetailExtractor());
}

From source file:org.ohmage.cache.StringAndIdCache.java

/**
 * Reads the database for the information in the lookup table and
 * populates its map with the gathered information. If there is an issue
 * reading the database, it will just remain with the current lookup table
 * it has./*from  ww  w .j  ava  2  s .  com*/
 * 
 * This is synchronized as any number of threads may realize that the
 * cache is out-of-date and attempt to update it. The first one should
 * succeed and the following ones will abort as the first thing a refresh
 * does is, again, check if the cache is stale.
 * 
 * @complexity O(n) where n is the number of strings-values in the
 *             database.
 */
private synchronized void refreshMap() {
    // Only one thread should be updating this information at a time. Once
    // other threads enter, they should check to see if an update was just
    // done and, if so, should abort a second update.
    if ((getLastUpdateTimestamp() + getUpdateFrequency()) > System.currentTimeMillis()) {
        return;
    }

    // This is the JdbcTemplate we will use for our query.
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());

    // Get all of the strings and their corresponding IDs. If there is an
    // issue, report it and abort the update.
    List<StringAndId> stateAndId;
    try {
        stateAndId = jdbcTemplate.query(sqlForRetrievingValues, new RowMapper<StringAndId>() {
            @Override
            public StringAndId mapRow(ResultSet rs, int row) throws SQLException {
                return new StringAndId(rs.getInt(integerColumn), rs.getString(stringColumn));
            }
        });
    } catch (org.springframework.dao.DataAccessException e) {
        LOGGER.error("Error executing SQL '" + sqlForRetrievingValues + "'. Aborting cache refresh.");
        return;
    }

    // Create a new map, populate it, and then completely replace the old
    // one. This allows for concurrent reads while a new map is being
    // generated.
    BidirectionalHashMap<String, Integer> stringAndIdMap = new BidirectionalHashMap<String, Integer>();
    for (StringAndId currStateAndId : stateAndId) {
        stringAndIdMap.putKey(currStateAndId.string, currStateAndId.id);
    }
    this.stringAndIdMap = stringAndIdMap;

    setLastUpdateTimestamp(System.currentTimeMillis());
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

public List<String> getPatternNames() {
    //return all pattern names
    LOG.info("Returning all pattern names");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from pattern";
    List<String> patternNameList = jdbcTemplate.query(sql, new RowMapper() {
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("name");
        }//from  w w  w  .  j  a  v a 2s.  c om
    });

    return patternNameList;
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

@Override
public List<Pattern> patternList() {
    //return all available (occured) patterns
    LOG.info("Returning all available (occured) patterns");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from pattern";
    List<Pattern> listPattern = jdbcTemplate.query(sql, new RowMapper<Pattern>() {

        @Override/*  w w  w. j av a 2 s . c  o  m*/
        public Pattern mapRow(ResultSet rs, int rowNumber) throws SQLException {
            Pattern pattern = new Pattern();
            pattern.setId(rs.getInt("id"));
            pattern.setName(rs.getString("name"));
            return pattern;
        }

    });
    return listPattern;
}

From source file:gov.nih.nci.ncicb.cadsr.common.persistence.dao.jdbc.JDBCValueDomainDAO.java

public Map<String, ValueMeaning> getValueMeanings(Collection<String> vmIds) {
    if (vmIds == null || vmIds.size() < 1) {
        return new HashMap<String, ValueMeaning>();
    }/*w w w.j  av a2s.c o  m*/
    String qry = "select * from VALUE_MEANINGS_VIEW where VM_IDSEQ in ( ";
    for (String vmId : vmIds) {
        qry += "'" + vmId + "',";
    }

    if (qry.lastIndexOf(",") == -1) {
        qry += "'')";
    } else {
        qry = qry.substring(0, qry.length() - 1) + ")";
    }

    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
    Map<String, ValueMeaning> vmMap = (Map<String, ValueMeaning>) jdbcTemplate.query(qry,
            new ResultSetExtractor() {
                public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                    Map<String, ValueMeaning> vmMap = new HashMap<String, ValueMeaning>();
                    while (rs.next()) {
                        ValueMeaning vm = new ValueMeaningTransferObject();
                        vm.setPreferredName(rs.getString(1));
                        vm.setPreferredDefinition(rs.getString(2));
                        String vmIdSeq = rs.getString(11);
                        vm.setLongName(rs.getString(14));
                        vm.setDefinitions(getDefinitions(vmIdSeq));
                        vm.setDesignations(getDesignations(vmIdSeq, null));

                        vmMap.put(vmIdSeq, vm);
                    }
                    return vmMap;
                }
            });

    return vmMap;
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

@Override
public List<TrippleBottom> getTrippleBottomList() {
    //return all triple bottom patterns
    LOG.info("Returning all triple bottom patterns");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from tripplebottom";
    List<TrippleBottom> listPattern = jdbcTemplate.query(sql, new RowMapper<TrippleBottom>() {

        @Override/*  w w w . j a  v  a  2 s  .co m*/
        public TrippleBottom mapRow(ResultSet rs, int rowNumber) throws SQLException {
            TrippleBottom pattern = new TrippleBottom();
            pattern.setId(rs.getInt("id"));
            pattern.setName(rs.getString("name"));
            pattern.setFirstMin(rs.getString("firstMin"));
            pattern.setSecondMin(rs.getString("secondMin"));
            pattern.setThirdMin(rs.getString("thirdMin"));
            pattern.setBreakPoint(rs.getString("breakPoint"));
            pattern.setFirstMinPrice(rs.getDouble("fistMinPrice"));
            pattern.setSecondMinPrice(rs.getDouble("secondMinPrice"));
            pattern.setThirdMinPrice(rs.getDouble("thirdMinPrice"));
            pattern.setBreakPointPrice(rs.getDouble("breakPointPrice"));
            pattern.setFirstMax(rs.getString("firstMax"));
            pattern.setFirstMaxPrice(rs.getDouble("firstMaxPrice"));
            pattern.setSecondMax(rs.getString("secondMax"));
            pattern.setSecondMaxPrice(rs.getDouble("secondMaxPrice"));
            return pattern;
        }

    });
    return listPattern;
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

@Override
public Doubletop getDoubletop(String id) {
    //return requested double pattern
    LOG.info("Returning requested double top pattern", id);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from doubletop where id=" + id;
    List<Doubletop> doubletop = jdbcTemplate.query(sql, new RowMapper<Doubletop>() {

        @Override/* w w w.jav a 2 s.  co m*/
        public Doubletop mapRow(ResultSet rs, int rowNumber) throws SQLException {
            Doubletop doubletop1 = new Doubletop();

            doubletop1.setBreakPointPrice(rs.getDouble("breakPointPrice"));
            doubletop1.setFirstMaxPrice(rs.getDouble("firstMaxPrice"));
            doubletop1.setFirstMinPrice(rs.getDouble("firstMinPrice"));
            doubletop1.setSecondMaxPrice(rs.getDouble("secondMaxPrice"));
            doubletop1.setBreakPoint(rs.getString("breakPoint"));
            doubletop1.setFirstMax(rs.getString("firstMax"));
            doubletop1.setFirstMin(rs.getString("firstMin"));
            doubletop1.setSecondMax(rs.getString("secondMax"));

            return doubletop1;
        }

    });

    return doubletop.get(0);
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

@Override
public TrippleTop getTrippleTop(String id) {
    //return requested triple top pattern
    LOG.info("Returning requested triple top pattern", id);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from trippletop where id=" + id;
    List<TrippleTop> trippletop = jdbcTemplate.query(sql, new RowMapper<TrippleTop>() {

        @Override/*from   w w w .j  a va2s. c o  m*/
        public TrippleTop mapRow(ResultSet rs, int rowNumber) throws SQLException {
            TrippleTop trippletop1 = new TrippleTop();

            trippletop1.setBreakPointPrice(rs.getDouble("breakPointPrice"));
            trippletop1.setFirstMaxPrice(rs.getDouble("firstMaxPrice"));
            trippletop1.setSecondMaxPrice(rs.getDouble("secondMaxPrice"));
            trippletop1.setThirdMaxPrice(rs.getDouble("thirdMaxPrice"));
            trippletop1.setFirstMinPrice(rs.getDouble("firstMinPrice"));
            trippletop1.setSecondMinPrice(rs.getDouble("secondMinPrice"));
            trippletop1.setBreakPoint(rs.getString("breakPoint"));
            trippletop1.setFirstMax(rs.getString("firstMax"));
            trippletop1.setSecondMax(rs.getString("secondMax"));
            trippletop1.setThirdMax(rs.getString("thirdMax"));
            trippletop1.setFirstMin(rs.getString("firstMin"));
            trippletop1.setSecondMin(rs.getString("secondMin"));

            return trippletop1;
        }

    });

    return trippletop.get(0);
}

From source file:com.arcane.dao.Impl.PatternDaoImpl.java

@Override
public DoubleBottom getDoubleBottom(String id) {
    //return requested double bottom pattern
    LOG.info("Returning requested double bottom pattern", id);
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "SELECT * from doublebottom where id=" + id;
    List<DoubleBottom> doublebottom = jdbcTemplate.query(sql, new RowMapper<DoubleBottom>() {

        @Override/*  w w w  .j  a  v  a2  s.  c o  m*/
        public DoubleBottom mapRow(ResultSet rs, int rowNumber) throws SQLException {
            DoubleBottom doublebottom1 = new DoubleBottom();

            doublebottom1.setBreakPointPrice(rs.getDouble("breakPointPrice"));
            doublebottom1.setFirstMaxPrice(rs.getDouble("firstMaxPrice"));
            doublebottom1.setFirstMinPrice(rs.getDouble("firstMinPrice"));
            doublebottom1.setSecondMinPrice(rs.getDouble("secondMinPrice"));
            doublebottom1.setBreakPoint(rs.getString("breakPoint"));
            doublebottom1.setFirstMax(rs.getString("firstMax"));
            doublebottom1.setFirstMin(rs.getString("firstMin"));
            doublebottom1.setSecondMin(rs.getString("secondMin"));

            return doublebottom1;
        }

    });

    return doublebottom.get(0);
}