Example usage for org.apache.commons.dbutils ResultSetHandler ResultSetHandler

List of usage examples for org.apache.commons.dbutils ResultSetHandler ResultSetHandler

Introduction

In this page you can find the example usage for org.apache.commons.dbutils ResultSetHandler ResultSetHandler.

Prototype

ResultSetHandler

Source Link

Usage

From source file:org.sonar.server.db.migrations.v36.Referentials.java

private Map<Long, String> selectLongString(Database database, String sql) throws SQLException {
    Connection connection = database.getDataSource().getConnection();
    try {/*ww  w .  j  av  a  2 s  .c o m*/
        return new QueryRunner().query(connection, sql, new ResultSetHandler<Map<Long, String>>() {
            @Override
            public Map<Long, String> handle(ResultSet rs) throws SQLException {
                Map<Long, String> map = Maps.newHashMap();
                while (rs.next()) {
                    map.put(rs.getLong(1), rs.getString(2));
                }
                return map;
            }
        });
    } finally {
        DbUtils.closeQuietly(connection);
    }
}

From source file:reasoning.TestUtils.java

public static <E> Multiset<Tuple<E>> getTuples(Connection connection, String query) {
    final Multiset<Tuple<E>> result = HashMultiset.create();
    QueryRunner qRunner = new QueryRunner();
    try {//from  w  ww .j a  va  2s .co m
        qRunner.query(connection, query, new ResultSetHandler() {

            @Override
            public Object handle(ResultSet rs) throws SQLException {
                int columns = rs.getMetaData().getColumnCount();
                while (rs.next()) {
                    Tuple<E> t = new Tuple<E>();
                    for (int i = 1; i <= columns; i++) {
                        t.add((E) rs.getObject(i));
                    }
                    result.add(t);
                }
                return null;
            }
        });
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
    return result;
}

From source file:tv.dyndns.kishibe.qmaclone.server.database.DirectDatabase.java

@Override
public void processProblems(final ProblemProcessable processor) throws DatabaseException {
    try {// w  ww.j a v a 2s.  c o m
        runner.query("SELECT * FROM problem", new ResultSetHandler<Void>() {
            @Override
            public Void handle(ResultSet rs) throws SQLException {
                int counter = 0;
                while (rs.next()) {
                    PacketProblem data = toPacketProblem(rs);

                    try {
                        processor.process(data);
                    } catch (Exception e) {
                        // ???????????????
                        logger.log(Level.WARNING,
                                "????????", e);
                        throw new SQLException(e);
                    }

                    if (++counter % 10000 == 0) {
                        System.err.println(counter);
                    }
                }

                return null;
            }
        });
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}

From source file:tv.dyndns.kishibe.qmaclone.server.database.DirectDatabase.java

@Override
public void processProblemMinimums(final ProblemMinimumProcessable processer) throws DatabaseException {
    String sql = "SELECT ID, GENRE, TYPE, GOOD, BAD, RANDOM_FLAG, CREATER, INDICATION FROM problem";
    try {//from w ww  . j a  v a2 s.  c  o m
        runner.query(sql, new ResultSetHandler<Void>() {
            @Override
            public Void handle(ResultSet rs) throws SQLException {
                int counter = 0;
                while (rs.next()) {
                    PacketProblemMinimum problem = toPacketProblemMinimum(rs);

                    try {
                        processer.process(problem);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if (++counter % 10000 == 0) {
                        System.err.println(counter);
                    }
                }

                return null;
            }
        });
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}

From source file:tv.dyndns.kishibe.qmaclone.server.database.DirectDatabase.java

@Override
public Map<Integer, Integer> getUserCodeToIndicatedProblems() throws DatabaseException {
    final Map<Integer, Integer> problemIdToUserCode = Maps.newHashMap();
    try {//from   w  ww  .  j  a  va2 s.com
        runner.query(
                "SELECT problem.ID, creation_log.USER_CODE, creation_log.DATE FROM problem, creation_log "
                        + "WHERE INDICATION != 0 AND problem.ID = creation_log.PROBLEM_ID",
                new ResultSetHandler<Void>() {
                    @Override
                    public Void handle(ResultSet resultSet) throws SQLException {
                        while (resultSet.next()) {
                            int problemId = resultSet.getInt("ID");
                            int userCode = resultSet.getInt("creation_log.USER_CODE");
                            // ????????????
                            problemIdToUserCode.put(problemId, userCode);
                        }
                        return null;
                    }
                });
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }

    Multiset<Integer> userCodes = HashMultiset.create(problemIdToUserCode.values());
    Map<Integer, Integer> userCodeToIndicatedProblems = Maps.newHashMap();
    for (int userCode : userCodes) {
        userCodeToIndicatedProblems.put(userCode, userCodes.count(userCode));
    }
    return userCodeToIndicatedProblems;
}

From source file:velo.adapters.JdbcAdapter.java

/**
 * Perform a query, return true/false upon result
 * @param query The query object with the query to execute
 * @return true/false upon result/*w  w  w.ja  v  a  2  s.  c  o m*/
 * @throws AdapterCommandExecutionException
 */
public boolean isTrueQuery(Query queryManager) throws AdapterCommandExecutionException {

    if (!queryManager.isHasQueries()) {
        throw new AdapterCommandExecutionException("Could not find any queries to execute...");
    }

    //logger.info("Executing isTrueQuery action, checking whether the query returns true/false");
    //logger.info("Executing query: " + query.getQueryString());

    //Before executing, make sure that the connection is not null
    if (getJdbcConnection() == null) {
        throw new AdapterCommandExecutionException("Jdbc query execution FAILED, connection object is null.");
    }

    ResultSetHandler rsh = new ResultSetHandler() {
        public Boolean handle(ResultSet rs) throws SQLException {
            if (!rs.next()) {
                return false;
            }

            ResultSetMetaData meta = rs.getMetaData();
            int cols = meta.getColumnCount();
            Object[] result = new Object[cols];

            for (int i = 0; i < cols; i++) {
                result[i] = rs.getObject(i + 1);
            }
            return true;
        }
    };

    QueryRunner qRunner = new QueryRunner();

    try {
        SingleQuery sq = queryManager.getQueries().iterator().next();
        boolean result = (Boolean) qRunner.query(sq.getQueryString(), sq.getParams(), rsh);
        //            boolean result = (Boolean) qRunner.query(getJdbcConnection(), queryManager.getQueries().iterator().next().getQueryString(), rsh);

        if (result) {
            return true;
        } else {
            return false;
        }
    } catch (SQLException se) {
        String message = "Failed to perform 'isTrueQuery', failed with message: " + se.getMessage();
        //logger.warning(message);

        //Since we cannot thorugh an exception (since true/false must be returned) lets log into the adapter messages the failure message
        getMsgs().warning(message);
        return false;
    }
}