Example usage for java.sql Statement getResultSet

List of usage examples for java.sql Statement getResultSet

Introduction

In this page you can find the example usage for java.sql Statement getResultSet.

Prototype

ResultSet getResultSet() throws SQLException;

Source Link

Document

Retrieves the current result as a ResultSet object.

Usage

From source file:mom.trd.opentheso.bdd.helper.FacetHelper.java

/**
 * Cette fonction permet de retourner la liste des Id des Facettes ranges sous un concept
 *
 * @param ds/*from  w w  w .j  av a2s  . c om*/
 * @param idConcept
 * @param idThesaurus
 * @return ArrayList of Id Facet (int)
 */
public ArrayList<Integer> getIdFacetUnderConcept(HikariDataSource ds, String idConcept, String idThesaurus) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;

    ArrayList<Integer> listIdFacet = new ArrayList();

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select facet_id from thesaurus_array" + " where id_thesaurus = '" + idThesaurus
                        + "'" + " and id_concept_parent = '" + idConcept + "'";
                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                while (resultSet.next()) {
                    listIdFacet.add(resultSet.getInt("facet_id"));
                }

            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Ids of Facet for Concept : " + idConcept, sqle);
    }
    return listIdFacet;
}

From source file:com.cloud.utils.db.Transaction.java

protected void removeUpTo(String type, Object ref) {
    boolean rollback = false;
    Iterator<StackElement> it = _stack.iterator();
    while (it.hasNext()) {
        StackElement item = it.next();/*from  w w  w  . ja v  a 2 s .c om*/

        it.remove();

        try {
            if (item.type == type && (ref == null || item.ref == ref)) {
                break;
            }

            if (item.type == CURRENT_TXN) {
                if (s_logger.isTraceEnabled()) {
                    s_logger.trace("Releasing the current txn: " + (item.ref != null ? item.ref : ""));
                }
            } else if (item.type == CREATE_CONN) {
                closeConnection();
            } else if (item.type == START_TXN) {
                if (item.ref == null) {
                    rollback = true;
                } else {
                    try {
                        _conn.rollback((Savepoint) ref);
                        rollback = false;
                    } catch (final SQLException e) {
                        s_logger.warn("Unable to rollback Txn.", e);
                    }
                }
            } else if (item.type == STATEMENT) {
                try {
                    if (s_stmtLogger.isTraceEnabled()) {
                        s_stmtLogger.trace("Closing: " + ref.toString());
                    }
                    Statement stmt = (Statement) ref;
                    try {
                        ResultSet rs = stmt.getResultSet();
                        if (rs != null) {
                            rs.close();
                        }
                    } catch (SQLException e) {
                        s_stmtLogger.trace("Unable to close resultset");
                    }
                    stmt.close();
                } catch (final SQLException e) {
                    s_stmtLogger.trace("Unable to close statement: " + item);
                }
            } else if (item.type == ATTACHMENT) {
                TransactionAttachment att = (TransactionAttachment) item.ref;
                if (s_logger.isTraceEnabled()) {
                    s_logger.trace("Cleaning up " + att.getName());
                }
                att.cleanup();
            }
        } catch (Exception e) {
            s_logger.error("Unable to clean up " + item, e);
        }
    }

    if (rollback) {
        rollback();
    }
}

From source file:gobblin.data.management.conversion.hive.validation.ValidationJob.java

/***
 * Execute Hive queries using {@link HiveJdbcConnector} and validate results.
 * @param queries Queries to execute./*w  w  w.  j  a va2s.  co  m*/
 */
@SuppressWarnings("unused")
private List<Long> getValidationOutputFromHiveJdbc(List<String> queries) throws IOException {
    if (null == queries || queries.size() == 0) {
        log.warn("No queries specified to be executed");
        return Collections.emptyList();
    }
    Statement statement = null;
    List<Long> rowCounts = Lists.newArrayList();
    Closer closer = Closer.create();

    try {
        HiveJdbcConnector hiveJdbcConnector = HiveJdbcConnector.newConnectorWithProps(props);
        statement = hiveJdbcConnector.getConnection().createStatement();

        for (String query : queries) {
            log.info("Executing query: " + query);
            boolean result = statement.execute(query);
            if (result) {
                ResultSet resultSet = statement.getResultSet();
                if (resultSet.next()) {
                    rowCounts.add(resultSet.getLong(1));
                }
            } else {
                log.warn("Query output for: " + query + " : " + result);
            }
        }

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            closer.close();
        } catch (Exception e) {
            log.warn("Could not close HiveJdbcConnector", e);
        }
        if (null != statement) {
            try {
                statement.close();
            } catch (SQLException e) {
                log.warn("Could not close Hive statement", e);
            }
        }
    }

    return rowCounts;
}

From source file:mom.trd.opentheso.bdd.helper.FacetHelper.java

/**
 * Cette fonction permet de retourner la liste des Id des Facettes qui contiennent un concept
 *
 * @param ds// w  w w .  j  ava2 s .  c  o  m
 * @param idConcept
 * @param idThesaurus
 * @return ArrayList of Id Facet (int)
 */
public ArrayList<Integer> getIdFacetOfConcept(HikariDataSource ds, String idConcept, String idThesaurus) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;

    ArrayList<Integer> listIdFacet = new ArrayList();

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select thesaurusarrayid from thesaurus_array_concept"
                        + " where id_thesaurus = '" + idThesaurus + "'" + " and id_concept = '" + idConcept
                        + "'";
                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                while (resultSet.next()) {
                    listIdFacet.add(resultSet.getInt("thesaurusarrayid"));
                }

            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Ids of Facet for Concept : " + idConcept, sqle);
    }
    return listIdFacet;
}

From source file:org.apache.jackrabbit.core.persistence.db.DatabasePersistenceManager.java

/**
 * {@inheritDoc}//from   w ww.  j  a  v a2  s.c  om
 */
public boolean exists(NodeId id) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }

    synchronized (nodeStateSelectExistSQL) {
        ResultSet rs = null;
        try {
            Statement stmt = executeStmt(nodeStateSelectExistSQL, new Object[] { id.toString() });
            rs = stmt.getResultSet();

            // a node state exists if the result has at least one entry
            return rs.next();
        } catch (Exception e) {
            String msg = "failed to check existence of node state: " + id;
            log.error(msg, e);
            throw new ItemStateException(msg, e);
        } finally {
            closeResultSet(rs);
        }
    }
}

From source file:org.apache.jackrabbit.core.persistence.db.DatabasePersistenceManager.java

/**
 * {@inheritDoc}/*  www . j av a  2  s . co m*/
 */
public boolean exists(PropertyId id) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }

    synchronized (propertyStateSelectExistSQL) {
        ResultSet rs = null;
        try {
            Statement stmt = executeStmt(propertyStateSelectExistSQL, new Object[] { id.toString() });
            rs = stmt.getResultSet();

            // a property state exists if the result has at least one entry
            return rs.next();
        } catch (Exception e) {
            String msg = "failed to check existence of property state: " + id;
            log.error(msg, e);
            throw new ItemStateException(msg, e);
        } finally {
            closeResultSet(rs);
        }
    }
}

From source file:org.apache.jackrabbit.core.persistence.db.DatabasePersistenceManager.java

/**
 * {@inheritDoc}/*from   w  w  w.ja v  a2  s .  c  o m*/
 */
public NodeState load(NodeId id) throws NoSuchItemStateException, ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }

    synchronized (nodeStateSelectSQL) {
        ResultSet rs = null;
        InputStream in = null;
        try {
            Statement stmt = executeStmt(nodeStateSelectSQL, new Object[] { id.toString() });
            rs = stmt.getResultSet();
            if (!rs.next()) {
                throw new NoSuchItemStateException(id.toString());
            }

            in = rs.getBinaryStream(1);
            NodeState state = createNew(id);
            Serializer.deserialize(state, in);

            return state;
        } catch (Exception e) {
            if (e instanceof NoSuchItemStateException) {
                throw (NoSuchItemStateException) e;
            }
            String msg = "failed to read node state: " + id;
            log.error(msg, e);
            throw new ItemStateException(msg, e);
        } finally {
            IOUtils.closeQuietly(in);
            closeResultSet(rs);
        }
    }
}

From source file:mom.trd.opentheso.bdd.helper.FacetHelper.java

/**
 * Cette fonction permet de rcuprer les Id des Concepts Parents qui continennent des Facettes
 *
 * @param ds// w  w w . j  a  v a 2 s. co m
 * @param idThesaurus
 * @param lang
 * @return ArrayList of IdConcepts
 */
public ArrayList<NodeConceptTree> getIdParentOfFacet(HikariDataSource ds, String idThesaurus, String lang) {
    Connection conn;
    Statement stmt;
    ResultSet resultSet;
    ArrayList<String> listIdC = new ArrayList<>();
    ArrayList<NodeConceptTree> tabIdConcept = null;

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "SELECT DISTINCT id_concept_parent" + " FROM thesaurus_array WHERE"
                        + " id_thesaurus = '" + idThesaurus + "'";

                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                tabIdConcept = new ArrayList<>();
                while (resultSet.next()) {
                    listIdC.add(resultSet.getString("id_concept_parent"));
                }
                for (String idC : listIdC) {
                    query = "SELECT term.lexical_value FROM term, preferred_term"
                            + " WHERE preferred_term.id_term = term.id_term"
                            + " and preferred_term.id_concept ='" + idC + "'" + " and term.lang = '" + lang
                            + "'" + " and term.id_thesaurus = '" + idThesaurus + "'"
                            + " order by lexical_value DESC";

                    stmt.executeQuery(query);
                    resultSet = stmt.getResultSet();
                    resultSet.next();
                    NodeConceptTree nct = new NodeConceptTree();
                    nct.setIdConcept(idC);
                    nct.setIdLang(lang);
                    nct.setIdThesaurus(idThesaurus);
                    if (resultSet.getRow() == 0) {
                        nct.setTitle("");
                    } else {
                        nct.setTitle(resultSet.getString("lexical_value"));
                    }
                    tabIdConcept.add(nct);
                }
            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Facet of Thesaurus : " + idThesaurus, sqle);
    }

    return tabIdConcept;
}

From source file:org.apache.jackrabbit.core.persistence.db.DatabasePersistenceManager.java

/**
 * {@inheritDoc}/*from w w w. ja va  2  s.  c o m*/
 */
public boolean existsReferencesTo(NodeId targetId) throws ItemStateException {
    if (!initialized) {
        throw new IllegalStateException("not initialized");
    }

    synchronized (nodeReferenceSelectExistSQL) {
        ResultSet rs = null;
        try {
            Statement stmt = executeStmt(nodeReferenceSelectExistSQL, new Object[] { targetId.toString() });
            rs = stmt.getResultSet();

            // a reference exists if the result has at least one entry
            return rs.next();
        } catch (Exception e) {
            String msg = "failed to check existence of node references: " + targetId;
            log.error(msg, e);
            throw new ItemStateException(msg, e);
        } finally {
            closeResultSet(rs);
        }
    }
}

From source file:capture.PostgreSQLDatabase.java

public boolean resumeLastOperation() {
    Connection con = this.getConnection();
    Statement stmt;
    ResultSet rs;//w w  w . j  a  va2s .c  o  m
    String operationid = null;
    String serverip = ConfigManager.getInstance().getConfigOption("server-listen-address");
    boolean result = false;
    long count = 0;
    try {
        Element e;
        stmt = con.createStatement();

        //find the oldest operation which still has unvisited urls.
        stmt.executeQuery("SELECT DISTINCT a.operation_id from url_operation a, operation b, honeypot c "
                + "WHERE a.operation_id=b.operation_id AND b.honeypot_id=c.honeypot_id AND a.status_id IS NULL "
                + " AND c.ipaddress=\'" + serverip + "\' order by operation_id DESC");
        rs = stmt.getResultSet();
        if (rs.next()) {
            operationid = rs.getString(1);
            System.out.println("System is going to inspect urls in the operation: " + operationid);
            Database.getInstance().setCurrentOperation(operationid);
            setSystemStatus(true);

            //update visit start time for operation if it hasn't set yet
            SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.S");
            String date = sf.format(new Date());
            stmt.executeUpdate("UPDATE operation SET visitstarttime=to_timestamp(\'" + date
                    + "\','DD/MM/YYYY HH24:MI:SS.MS\') " + "WHERE operation_id=" + operationid
                    + " AND visitstarttime IS NULL");

            //get all urls which haven't been visited yet
            stmt.executeQuery("SELECT url.url_id, url.url FROM url, url_operation "
                    + "WHERE url_operation.url_id=url.url_id AND (url_operation.status_id IS NULL) AND url_operation.operation_id="
                    + operationid);
            rs = stmt.getResultSet();
            while (rs.next()) {
                e = new Element();
                e.name = "url";
                e.attributes.put("add", "");
                e.attributes.put("id", rs.getString(1));
                e.attributes.put("url", rs.getString(2));
                EventsController.getInstance().notifyEventObservers(e);
                count++;
            }
        }
        stmt.close();
        con.close();
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("******** RESUME: " + count + " URLs have been loaded!********");
    return result;
}