Example usage for org.springframework.jdbc.core RowMapper RowMapper

List of usage examples for org.springframework.jdbc.core RowMapper RowMapper

Introduction

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

Prototype

RowMapper

Source Link

Usage

From source file:au.aurin.org.svc.GeodataFinder.java

public List<String> getPolygonIDS(final String uazTbl, final String polygonStr) {

    final String query = "select a.propid, ST_Asgeojson(geom)  from "
            + postgisDataStoreConfig.getDataStoreParams().get(SCHEMA.key) + "." + uazTbl
            + " as a INNER JOIN   ST_GeomFromText('" + polygonStr
            + "', 4326) as b  ON   ST_Intersects(a.geom, b.geometry)" + " where a.propid is not null "; // this line added later for avoiding
    // null.// w ww .  jav  a  2 s .  c o  m

    final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    LOGGER.info("getPolygonIDS: query is {} ", query);
    final List<String> entries = jdbcTemplate.query(query, new RowMapper<String>() {

        @Override
        public String mapRow(final ResultSet rs, final int arg1) throws SQLException {
            return rs.getObject(1).toString();
        }

    });

    LOGGER.info(" returning {} distinct entries ", entries.size());
    return entries;
}

From source file:com.sfs.whichdoctor.dao.TagDAOImpl.java

/**
 * Used to get a TagBean based on its ID.
 *
 * @param tagId the tag id// www .  ja va2 s.co m
 * @return the tag bean
 * @throws WhichDoctorDaoException the which doctor dao exception
 */
public final TagBean load(final int tagId) throws WhichDoctorDaoException {

    dataLogger.info("TagId: " + tagId + " requested");

    final String loadTag = getSQL().getValue("tag/loadId");

    TagBean tag = null;
    try {
        tag = (TagBean) this.getJdbcTemplateReader().queryForObject(loadTag, new Object[] { tagId },
                new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        return loadTag(rs, true);
                    }
                });

    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results for this search: " + ie.getMessage());
    }
    return tag;
}

From source file:cz.dasnet.dasik.plugin.WordCounter.java

public String getTop10Daily(Dasik bot, int from) {
    String sql = "SELECT * FROM users WHERE words_daily > 0 ORDER BY words_daily DESC LIMIT " + from + ", 10";
    List<User> users = bot.getUserDao().query(sql, UserDaoImpl.userRowMapper);

    sql = "SELECT SUM(words_daily) FROM users";
    double sum = bot.getUserDao().query(sql, new RowMapper<Integer>() {

        public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getInt(1);
        }/*  w w w .  j a  va2s .  c om*/
    }).get(0);

    StringBuilder sb = new StringBuilder();
    sb.append("Today's top spammers [").append(from + 1).append("-").append(from + users.size()).append("]: ");

    int i = from + 1;
    for (User u : users) {
        if (u.getWordsDaily() == 0) {
            break;
        }
        String nick = bot.maskToNick(u.getMask());
        if (nick == null) {
            if (u.getMask().contains("users.quakenet.org")) {
                nick = u.getMask().split("@")[1].split("\\.", 2)[0];
            } else {
                nick = u.getMask().split("@")[0];
            }
        }
        sb.append(i).append(". ").append(nick).append("(").append(u.getWordsDaily()).append(" :: ")
                .append(String.format("%1.0f%%", u.getWordsDaily() * 100 / sum)).append(") ");
        i++;
    }
    return sb.toString();
}

From source file:com.ewcms.component.auth.dao.UserDAO.java

@Override
public UserInfo getUserInfo(final String username) {
    String sql = "Select * From component_auth_userinfo Where username = ?";
    return jdbcTemplate.queryForObject(sql, new Object[] { username }, new RowMapper<UserInfo>() {
        @Override/*  w  ww.j a  va 2s  . com*/
        public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(username);
            userInfo.setName(rs.getString("name"));
            userInfo.setRealname(rs.getString("realname"));
            userInfo.setSex(rs.getInt("sex"));
            userInfo.setEducation(rs.getInt("education"));
            userInfo.setBirthYear(rs.getInt("birth_year"));
            userInfo.setBirthMonth(rs.getInt("birth_month"));
            userInfo.setBirthDay(rs.getInt("birth_day"));
            userInfo.setZip(rs.getString("zip"));
            userInfo.setAddress(rs.getString("address"));
            userInfo.setPhone(rs.getString("phone"));
            userInfo.setMphone(rs.getString("mphone"));

            return userInfo;
        }
    });
}

From source file:com.sfs.whichdoctor.dao.PhoneDAOImpl.java

/**
 * Load the phone bean.//from w w  w .  ja  v a2  s .co m
 *
 * @param phoneId the phone id
 * @return the phone bean
 * @throws WhichDoctorDaoException the which doctor dao exception
 */
@SuppressWarnings("unchecked")
public final PhoneBean load(final int phoneId) throws WhichDoctorDaoException {

    dataLogger.info("Getting phoneId:" + phoneId);

    final String loadPhoneId = getSQL().getValue("phone/load") + " WHERE phone.PhoneId = ?";

    PhoneBean phoneNumber = null;

    try {
        phoneNumber = (PhoneBean) this.getJdbcTemplateReader().queryForObject(loadPhoneId,
                new Object[] { phoneId }, new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        return loadPhone(rs);
                    }
                });

    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results found for this search: " + ie.getMessage());
    }
    return phoneNumber;
}

From source file:bd.gov.forms.dao.FormDaoImpl.java

@Override
public Form getForm(String formId) {
    return (Form) jdbcTemplate.queryForObject("SELECT * FROM form WHERE form_id = ?", new Object[] { formId },
            new RowMapper() {

                public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                    Form form = new Form();

                    form.setId(resultSet.getInt("id"));
                    form.setFormId(resultSet.getString("form_id"));
                    form.setTitle(resultSet.getString("title"));
                    form.setSubTitle(resultSet.getString("subtitle"));
                    form.setDetail(resultSet.getString("detail"));
                    form.setTableName(resultSet.getString("table_name"));
                    form.setStatus(resultSet.getInt("status"));
                    form.setTemplateFileName(resultSet.getString("template_file_name"));

                    return form;
                }/*from  w w w  .jav  a2s.  c o  m*/
            });
}

From source file:com.github.dbourdette.glass.log.joblog.jdbc.JdbcJobLogStore.java

private Page<JobLog> getLogs(String sqlBase, SqlParameterSource params, Query query) {
    String sql = query.applySqlLimit("select * " + sqlBase + " order by logDate asc");

    List<JobLog> jobLogs = jdbcTemplate.query(sql, params, new RowMapper<JobLog>() {
        @Override//w  w w . j av  a  2s.c o m
        public JobLog mapRow(ResultSet rs, int rowNum) throws SQLException {
            return doMapRow(rs, rowNum);
        }
    });

    String countSql = "select count(*) " + sqlBase;

    Page<JobLog> page = Page.fromQuery(query);

    page.setItems(jobLogs);
    page.setTotalCount(jdbcTemplate.queryForInt(countSql, params));

    return page;
}

From source file:repository.InteractionsDAO.java

public List<Interactions> getInteractionsByPage(int start, int total) {
    String sql = "SELECT interactions.interactionid, interactions.clientid, interactions.username, interactions.typeofinteraction, interactions.interactiontime, clients.id "
            + "FROM interactions AS interactions "
            + "INNER JOIN Clients AS clients ON clients.id = interactions.clientid "
            + "ORDER BY clients.id, interactions.interactiontime " + "LIMIT " + (start - 1) + "," + total;
    return template.query(sql, new RowMapper<Interactions>() {
        @Override// w  ww  . ja va2 s  . c  o m
        public Interactions mapRow(ResultSet rs, int row) throws SQLException {
            Interactions i = new Interactions();
            i.setInteractionid(rs.getInt(1));
            i.setClientid(rs.getInt(2));
            //                i.setFirstname(rs.getString(3));
            //                i.setLastname(rs.getString(4));
            i.setTypeofinteraction(rs.getString(5));
            i.setInteractiontime(rs.getString(6));

            Clients clients = new Clients();
            clients.setId(rs.getInt(1));
            clients.setFirstname(rs.getString(2));
            clients.setLastname(rs.getString(3));

            i.setClient(clients);

            return i;
        }
    });
}

From source file:dao.CircleDAO.java

public String getName(int id) {
    String q = "SELECT FirstName, LastName\n" + "FROM person\n" + "WHERE SSN = (\n" + "   SELECT SSN\n"
            + "    FROM user\n" + "    WHERE UserId = " + id + "\n" + ")";

    String name = this.jdbcTemplate.queryForObject(q, new RowMapper<String>() {
        @Override/*  w  ww.j  a  va 2  s . c o m*/
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {

            return rs.getString("FirstName") + " " + rs.getString("LastName");
        }
    });

    return name;
}

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

/**
 * {@inheritDoc}//from  w w w  . j a v  a  2 s .c o  m
 */
@Override
public List<LoginData> getLogins(final LocalDate start, final LocalDate end, final String idpEntityId,
        final String spEntityId) {
    final List<LoginData> result = new ArrayList<LoginData>();

    NamedParameterJdbcTemplate namedJdbcTemplate = new NamedParameterJdbcTemplate(cruncherJdbcTemplate);

    String query = "select * from aggregated_log_logins " + "where " + "entryday >= :startDate AND "
            + "entryday <= :endDate AND " + "(:spEntityId IS NULL OR spentityid = :spEntityId) AND "
            + "(:idpEntityId IS NULL OR idpentityid = :idpEntityId) "
            + "order by idpentityid, spentityid, entryday ";

    Map<String, Object> parameterMap = getParameterMap(start, end, idpEntityId, spEntityId);

    namedJdbcTemplate.query(query, parameterMap, new RowMapper<Object>() {
        private Map<LocalDate, Integer> queryResult = new HashMap<LocalDate, Integer>();
        private LoginData currentAggregate = null;

        @Override
        public Object mapRow(ResultSet rs, int row) throws SQLException {
            LoginData currentRow = getLoginDataFromRow(rs);

            /*
             * aggregate if sp/idp entityid differs from previous record
             * do not aggregate if on first record
             * if on last record, aggregate last entries
             */
            if (!currentRow.equals(currentAggregate) && !rs.isFirst()) {
                //record is different, aggregate previous one and start fresh
                result.add(aggregateCurrentEntry(currentAggregate, start, end));
                queryResult = new HashMap<LocalDate, Integer>();

            }
            queryResult.put(new LocalDate(rs.getDate("entryday")), rs.getInt("entrycount"));
            currentAggregate = currentRow;

            if (rs.isLast()) {
                // aggregate last set
                result.add(aggregateCurrentEntry(currentAggregate, start, end));
            }

            /*
             * This is kinda weird, but single row results are stored in 
             * queryResult (hashmap) or aggregated in result (List<loginData)
             */
            return null;
        }

        private LoginData aggregateCurrentEntry(final LoginData loginData, final LocalDate start,
                final LocalDate end) {
            LocalDate current = start;

            int total = 0;
            while (current.isBefore(end.plusDays(1))) {
                Integer count = queryResult.get(current);
                if (count == null) {
                    loginData.getData().add(0);
                } else {
                    loginData.getData().add(count);
                    total += count;
                }
                current = current.plusDays(1);
            }
            loginData.setTotal(total);
            loginData.setPointStart(start.toDate().getTime());
            loginData.setPointEnd(end.toDate().getTime());
            loginData.setPointInterval(POINT_INTERVAL);
            return loginData;
        }

        private LoginData getLoginDataFromRow(ResultSet rs) throws SQLException {
            LoginData result = new LoginData();
            result.setIdpEntityId(rs.getString("idpentityid"));
            result.setIdpname(rs.getString("idpentityname"));
            result.setSpEntityId(rs.getString("spentityid"));
            result.setSpName(rs.getString("spentityname"));
            return result;
        }
    });
    return result;
}