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:org.apache.jackrabbit.core.persistence.bundle.BundleDbPersistenceManager.java

/**
 * {@inheritDoc}/*from   w  w w .j  av  a2  s  . co m*/
 */
protected synchronized NodePropBundle loadBundle(NodeId id) throws ItemStateException {
    ResultSet rs = null;
    InputStream in = null;
    try {
        Statement stmt = connectionManager.executeStmt(bundleSelectSQL, getKey(id.getUUID()));
        rs = stmt.getResultSet();
        if (!rs.next()) {
            return null;
        }
        Blob b = rs.getBlob(1);
        // JCR-1039: pre-fetch/buffer blob data
        long length = b.length();
        byte[] bytes = new byte[(int) length];
        in = b.getBinaryStream();
        int read, pos = 0;
        while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
            pos += read;
        }
        DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));
        NodePropBundle bundle = binding.readBundle(din, id);
        bundle.setSize(length);
        return bundle;
    } catch (Exception e) {
        String msg = "failed to read bundle: " + id + ": " + e;
        log.error(msg);
        throw new ItemStateException(msg, e);
    } finally {
        IOUtils.closeQuietly(in);
        closeResultSet(rs);
    }
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void deleteRegisteredGateway(String pGatewayRegisteredName) {
    StringBuilder tmpIgnoredOutput = new StringBuilder();
    if (isRegisteredGateway(pGatewayRegisteredName, tmpIgnoredOutput)) {
        java.sql.Connection conn = null;
        try {// ww w . ja v  a2s . c o  m
            Class.forName(jdbcdriverClassName).newInstance();
            conn = DriverManager.getConnection(connString, usrStr, pwdStr);
            String echomessage = "";
            if (!conn.isClosed()) {
                //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    stmt = conn.createStatement();

                    if (stmt.execute(
                            "DELETE FROM `" + dbSchemaStr + "`.`registeredgateway` WHERE registeredName=\'"
                                    + pGatewayRegisteredName + "\'")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }

                } catch (SQLException ex) {
                    // handle any errors
                    System.err.println("SQLException3: " + ex.getMessage());
                    System.err.println("SQLState3: " + ex.getSQLState());
                    System.err.println("VendorError3: " + ex.getErrorCode());
                } finally {
                    // it is a good idea to release
                    // resources in a finally{} block
                    // in reverse-order of their creation
                    // if they are no-longer needed
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        rs = null;
                    }
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        stmt = null;
                    }
                }
            } else {
                echomessage = "Error accessing DB server...";
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

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

/**
 * Cette fonction permet de retourner toutes les langues utilises par les
 * Concepts d'un thsaurus/*  ww  w.j a  v a 2s. co m*/
 *
 * @param ds
 * @param idThesaurus
 * @return Objet class NodeConceptTree
 */
public ArrayList<String> getAllUsedLanguagesOfThesaurus(HikariDataSource ds, String idThesaurus) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;
    ArrayList<String> tabIdLang = new ArrayList<>();

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select distinct lang from term where id_thesaurus = '" + idThesaurus + "'";

                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                while (resultSet.next()) {
                    tabIdLang.add(resultSet.getString("lang"));
                }
            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting All Used languages of Concepts of thesaurus  : " + idThesaurus, sqle);
    }
    return tabIdLang;
}

From source file:org.apache.hadoop.hive.jdbc.TestJdbcDriver.java

public void testShowRoleGrant() throws SQLException {
    Statement stmt = con.createStatement();

    // drop role. ignore error.
    try {/*from  w w  w.ja  v a2s  . c om*/
        stmt.execute("drop role role1");
    } catch (Exception ex) {
        LOG.warn("Ignoring error during drop role: " + ex);
    }

    stmt.execute("create role role1");
    stmt.execute("grant role role1 to user hive_test_user");
    stmt.execute("show role grant user hive_test_user");

    ResultSet res = stmt.getResultSet();
    assertTrue(res.next());
    assertEquals("public", res.getString(1));
    assertTrue(res.next());
    assertEquals("role1", res.getString(1));
    res.close();
}

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

/**
 * Cette fonction permet de savoir si le thesaurus existe ou non
 *
 * @param ds//from   ww  w  . ja  v a  2  s.c  o  m
 * @param idThesaurus
 * @return boolean
 */
public boolean isThesaurusExiste(HikariDataSource ds, String idThesaurus) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;
    boolean existe = false;

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select id_thesaurus from thesaurus where " + " id_thesaurus = '" + idThesaurus
                        + "'";
                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                if (resultSet.next()) {
                    existe = resultSet.getRow() != 0;
                }

            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while asking if thesaurus exist : " + idThesaurus, sqle);
    }
    return existe;
}

From source file:vitro.vspEngine.service.persistence.DBCommons.java

synchronized public void insertRegisteredGateway(String pGatewayRegisteredName, String pFriendlyName) {
    StringBuilder tmpIgnoredOutput = new StringBuilder();
    if (!isRegisteredGateway(pGatewayRegisteredName, tmpIgnoredOutput)) {
        if (pFriendlyName == null || pFriendlyName.trim().isEmpty()) {
            pFriendlyName = "unnamed island";
        }//w w w  . ja  va  2s. c o  m
        java.sql.Connection conn = null;
        try {
            Class.forName(jdbcdriverClassName).newInstance();
            conn = DriverManager.getConnection(connString, usrStr, pwdStr);
            String echomessage = "";
            if (!conn.isClosed()) {
                //echomessage =  "Successfully connected to "+ "MySQL server using TCP/IP...";
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    stmt = conn.createStatement();

                    if (stmt.execute("INSERT `" + dbSchemaStr
                            + "`.`registeredgateway`(registeredName, friendlyName) VALUES (\'"
                            + pGatewayRegisteredName + "\',\'" + pFriendlyName + "\')")) {
                        rs = stmt.getResultSet(); // TODO: this is not needed here...
                    }

                } catch (SQLException ex) {
                    // handle any errors
                    System.err.println("SQLException3: " + ex.getMessage());
                    System.err.println("SQLState3: " + ex.getSQLState());
                    System.err.println("VendorError3: " + ex.getErrorCode());
                } finally {
                    // it is a good idea to release
                    // resources in a finally{} block
                    // in reverse-order of their creation
                    // if they are no-longer needed
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        rs = null;
                    }
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (SQLException sqlEx) {
                        } // ignore
                        stmt = null;
                    }
                }
            } else {
                echomessage = "Error accessing DB server...";
            }
        } catch (Exception e) {
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

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

/**
 * Retourne la liste des traductions d'un thesaurus sous forme de MAP (lang
 * + title)//  w w  w. ja va 2s.  c o  m
 *
 * @param ds
 * @param idThesaurus
 * @return
 */
public Map getMapTraduction(HikariDataSource ds, String idThesaurus) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;
    Map map = new HashMap();
    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select lang, title from thesaurus_label" + " where id_thesaurus = '"
                        + idThesaurus + "'";
                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                if (resultSet != null) {
                    while (resultSet.next()) {
                        map.put(resultSet.getString("lang"), resultSet.getString("title"));
                    }
                }

            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Map of thesaurus : " + map.toString(), sqle);
    }
    return map;
}

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

public NodeFacet getThisFacet(HikariDataSource ds, int idFacet, String idThesaurus, String lang) {
    Connection conn;/*from w  ww . j a  v a2 s  . c  om*/
    Statement stmt;
    ResultSet resultSet;
    NodeFacet nf = new NodeFacet();

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "SELECT node_label.lexical_value, thesaurus_array.id_concept_parent FROM node_label, thesaurus_array"
                        + " WHERE node_label.facet_id=thesaurus_array.facet_id" + " and node_label.facet_id ='"
                        + idFacet + "'" + " and node_label.lang = '" + lang + "'"
                        + " and node_label.id_thesaurus = '" + idThesaurus + "'"
                        + " order by node_label.lexical_value DESC";

                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                resultSet.next();
                nf.setIdFacet(idFacet);
                nf.setIdConceptParent(resultSet.getString("id_concept_parent"));
                if (resultSet.getRow() == 0) {
                    nf.setLexicalValue("");
                } else {
                    nf.setLexicalValue(resultSet.getString("lexical_value"));
                }
            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Facet : " + idFacet, sqle);
    }

    return nf;
}

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

/**
 * Retourne la liste des langues sous forme de MAP (nom + id) si le
 * thesaurus n'existe pas dans la langue demande, on rcupre seulement son
 * id//from  ww  w  .  j ava  2 s . com
 *
 * @param ds
 * @param idLang
 * @return
 */
public Map getListThesaurus(HikariDataSource ds, String idLang) {

    Connection conn;
    Statement stmt;
    ResultSet resultSet;
    Map map = new HashMap();
    ArrayList tabIdThesaurus = new ArrayList();

    try {
        // Get connection from pool
        conn = ds.getConnection();
        try {
            stmt = conn.createStatement();
            try {
                String query = "select DISTINCT id_thesaurus from thesaurus";

                stmt.executeQuery(query);
                resultSet = stmt.getResultSet();
                if (resultSet != null) {
                    while (resultSet.next()) {
                        tabIdThesaurus.add(resultSet.getString("id_thesaurus"));
                    }
                    for (Object tabIdThesauru : tabIdThesaurus) {
                        query = "select title from thesaurus_label where" + " id_thesaurus = '" + tabIdThesauru
                                + "'" + " and lang = '" + idLang + "'";
                        stmt.executeQuery(query);
                        resultSet = stmt.getResultSet();
                        if (resultSet != null) {
                            resultSet.next();
                            if (resultSet.getRow() == 0) {
                                map.put("(" + tabIdThesauru + ")", tabIdThesauru);
                            } else {
                                map.put(resultSet.getString("title") + "(" + tabIdThesauru + ")",
                                        tabIdThesauru);
                            }

                        } else {
                        }
                    }

                }

            } finally {
                stmt.close();
            }
        } finally {
            conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        log.error("Error while getting Map of thesaurus : " + map.toString(), sqle);
    }
    return map;
}

From source file:gobblin.source.extractor.extract.jdbc.JdbcExtractor.java

/**
 * Execute query using JDBC simple Statement Set fetch size
 *
 * @param cmds commands - query, fetch size
 * @return JDBC ResultSet//w  ww .j a  v a 2 s. co m
 * @throws Exception
 */
private CommandOutput<?, ?> executeSql(List<Command> cmds) {
    String query = null;
    int fetchSize = 0;

    for (Command cmd : cmds) {
        if (cmd instanceof JdbcCommand) {
            JdbcCommandType type = (JdbcCommandType) cmd.getCommandType();
            switch (type) {
            case QUERY:
                query = cmd.getParams().get(0);
                break;
            case FETCHSIZE:
                fetchSize = Integer.parseInt(cmd.getParams().get(0));
                break;
            default:
                this.log.error("Command " + type.toString() + " not recognized");
                break;
            }
        }
    }

    this.log.info("Executing query:" + query);
    ResultSet resultSet = null;
    try {
        this.jdbcSource = createJdbcSource();
        this.dataConnection = this.jdbcSource.getConnection();
        Statement statement = this.dataConnection.createStatement();

        if (fetchSize != 0 && this.getExpectedRecordCount() > 2000) {
            statement.setFetchSize(fetchSize);
        }
        final boolean status = statement.execute(query);
        if (status == false) {
            this.log.error("Failed to execute sql:" + query);
        }
        resultSet = statement.getResultSet();
    } catch (Exception e) {
        this.log.error("Failed to execute sql:" + query + " ;error-" + e.getMessage(), e);
    }

    CommandOutput<JdbcCommand, ResultSet> output = new JdbcCommandOutput();
    output.put((JdbcCommand) cmds.get(0), resultSet);
    return output;
}