Example usage for java.sql Statement isClosed

List of usage examples for java.sql Statement isClosed

Introduction

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

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this Statement object has been closed.

Usage

From source file:org.commonjava.aprox.core.expire.DatabaseLifecycleActions.java

private void close(final ResultSet tableQuery, final Statement stmt, final Connection connection,
        final String url) {
    if (tableQuery != null) {
        try {/*  w  w w .j ava2  s .  c o m*/
            if (!tableQuery.isClosed()) {
                tableQuery.close();
            }
        } catch (final SQLException e) {
            logger.debug("Failed to close database table query: " + url, e);
        }
    }

    if (stmt != null) {
        try {
            if (!stmt.isClosed()) {
                stmt.close();
            }
        } catch (final SQLException e) {
            logger.debug("Failed to close database statement instance: " + url, e);
        }
    }

    if (connection != null) {
        try {
            connection.close();
        } catch (final SQLException e) {
            logger.debug("Failed to close database connection: " + url, e);
        }
    }
}

From source file:com.fer.hr.service.olap.ThinQueryService.java

public void cancel(String name) throws SQLException {
    if (context.containsKey(name)) {
        QueryContext queryContext = context.get(name);
        if (queryContext.contains(ObjectKey.STATEMENT)) {
            Statement stmt = queryContext.getStatement();
            if (stmt != null && !stmt.isClosed()) {
                stmt.cancel();/*from  ww  w. ja v a  2s . com*/
                stmt.close();
            }
            stmt = null;
            queryContext.remove(ObjectKey.STATEMENT);
        }
    }
}

From source file:org.openiot.gsn.storage.StorageManager.java

/**
 * This method executes the provided statement over the connection. If there
 * is an error retruns -1 otherwise it returns the output of the
 * executeUpdate method on the PreparedStatement class which reflects the
 * number of changed rows in the underlying table.
 *
 * @param sql/*w ww.ja va 2 s.c om*/
 * @param connection
 * @return Number of effected rows or -1 if there is an error.
 */
public void executeCommand(String sql, Connection connection) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.execute(sql);
    } catch (SQLException error) {
        logger.error(error.getMessage() + " FOR: " + sql, error);
    } finally {
        try {
            if (stmt != null && !stmt.isClosed())
                stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:gsn.storage.StorageManager.java

/**
 * This method executes the provided statement over the connection. If there
 * is an error retruns -1 otherwise it returns the output of the
 * executeUpdate method on the PreparedStatement class which reflects the
 * number of changed rows in the underlying table.
 *
 * @param sql/*from   w  ww  .  j a v a2 s.c om*/
 * @param connection
 * @return Number of effected rows or -1 if there is an error.
 */
public void executeCommand(String sql, Connection connection) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.execute(sql);
    } catch (SQLException error) {
        logger.error(error.getMessage() + " FOR: " + sql, error);
    } finally {
        try {
            if (stmt != null && !stmt.isClosed())
                stmt.close();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

From source file:tinygsn.storage.StorageManager.java

/**
 * This method executes the provided statement over the connection. If there
 * is an error retruns -1 otherwise it returns the output of the executeUpdate
 * method on the PreparedStatement class which reflects the number of changed
 * rows in the underlying table.//from  www .  ja  va  2s.  c  o m
 * 
 * @param sql
 * @param connection
 * @return Number of effected rows or -1 if there is an error.
 */
@SuppressLint("NewApi")
public void executeCommand(String sql, Connection connection) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.execute(sql);
    } catch (SQLException error) {
        // logger.error(error.getMessage() + " FOR: " + sql, error);
    } finally {
        try {
            if (stmt != null && !stmt.isClosed())
                stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.generalbioinformatics.rdf.VirtuosoConnection.java

/**
 * Execute a SPARQL query. The query will be based directly to Virtuoso, so all the
 * virtuoso sparql syntax quirks can be used.
 * <p>/*from   w w w.  j a v a 2  s.  co m*/
 * If you set any prefixes with setPrefixes, they will be included in this query.
 * <p>
 * If you set any cache dir, the query will be saved to File on the first run, 
 * and retrieved on the second run. 
 */
public RecordStream _sparqlSelectDirect(String query) throws StreamException {
    Connection con = null;
    Statement st = null;
    RecordStream rs = null;
    try {
        con = getConnection();
        st = con.createStatement();
        ResultSet result = executeQuery(st, "SPARQL " + prefixes + " " + query);
        rs = new VirtuosoRecordStream(result, st, isManagedConnection() ? con : null);
        return rs;
    } catch (Exception ex) {
        // if we failed to create the stream, clean up and rethrow
        try {
            if (rs != null)
                rs.close();
            if (st != null && !st.isClosed())
                st.close();
            if (isManagedConnection() && !con.isClosed())
                con.close();
        } catch (SQLException inner) {
            /* ignore */ }

        throw new StreamException(ex);
    }
}

From source file:com.sqewd.open.dal.core.persistence.db.AbstractDbPersister.java

private ResultSet select(final String query, final List<KeyValuePair<Class<?>>> types, final int limit,
        final Connection conn) throws Exception {

    NativeJoinGraph jg = new NativeJoinGraph(types, query);

    // Make sure the type for the class is available.
    SQLQuery parser = new SQLQuery(jg);

    String selectsql = parser.parse("", limit);
    log.debug("SELECT SQL [" + selectsql + "]");
    Statement stmnt = conn.createStatement();
    LocalResultSet entities = new LocalResultSet();

    try {/* ww  w.jav a2 s.  c o  m*/
        log.debug("SELECT SQL [" + selectsql + "]");
        ResultSet rs = stmnt.executeQuery(selectsql);
        try {
            entities.create(key, rs);
        } finally {
            if (rs != null && !rs.isClosed()) {
                rs.close();
            }
        }
        return entities;
    } finally {
        if (stmnt != null && !stmnt.isClosed()) {
            stmnt.close();
        }
    }
}

From source file:com.openddal.test.BaseTestCase.java

public void close(Connection connection, Statement statement, ResultSet rs) {
    if (rs != null) {
        try {/* w w  w.j av  a 2  s  .c  om*/
            if (!rs.isClosed())
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (statement != null) {
        try {
            if (!statement.isClosed())
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (connection != null) {
        try {
            if (!connection.isClosed())
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.sentry.binding.hive.authz.SentryConfigTool.java

public void verifyRemoteQuery(String queryStr) throws Exception {
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    Connection conn = DriverManager.getConnection(getJdbcURL(), getUser(), getPassWord());
    Statement stmt = conn.createStatement();
    if (!isSentryEnabledOnHiveServer(stmt)) {
        throw new IllegalStateException("Sentry is not enabled on HiveServer2");
    }//from w w w.j a  v a  2s.  c om
    stmt.execute("set " + HiveAuthzConf.HIVE_SENTRY_MOCK_COMPILATION + "=true");
    try {
        stmt.execute(queryStr);
    } catch (SQLException e) {
        String errMsg = e.getMessage();
        if (errMsg.contains(HiveAuthzConf.HIVE_SENTRY_MOCK_ERROR)) {
            System.out.println("User " + readConfig(stmt, HiveAuthzConf.HIVE_SENTRY_SUBJECT_NAME)
                    + " has privileges to run the query");
            return;
        } else if (errMsg.contains(HiveAuthzConf.HIVE_SENTRY_PRIVILEGE_ERROR_MESSAGE)) {
            printMissingPerms(readConfig(stmt, HiveAuthzConf.HIVE_SENTRY_AUTH_ERRORS));
            throw e;
        } else {
            throw e;
        }
    } finally {
        if (!stmt.isClosed()) {
            stmt.close();
        }
        conn.close();
    }

}

From source file:com.sqewd.open.dal.core.persistence.db.H2DbPersister.java

private void checkSetup() throws Exception {
    XMLConfiguration config = new XMLConfiguration(dbconfig);
    String version = config.getString(_CONFIG_SETUP_VERSION_);
    if (version == null || version.isEmpty())
        throw new Exception(
                "Invalid DB Setup Configuration : Missing parameter [" + _CONFIG_SETUP_VERSION_ + "]");

    if (!checkSchema()) {
        SimpleDbQuery dbq = new SimpleDbQuery();
        List<String> createsql = dbq.getCreateTableDDL(DBVersion.class);
        Connection conn = getConnection(true);
        Statement stmnt = conn.createStatement();
        try {// ww w  .j  av a  2s.  c o  m
            for (String sql : createsql) {
                log.debug("TABLE SQL [" + sql + "]");
                stmnt.execute(sql);
            }

            DBVersion dbv = (DBVersion) DataManager.newInstance(DBVersion.class);
            dbv.setVersion(version);
            save(dbv, false);

            NodeList nl = XMLUtils.search(_CONFIG_SETUP_ENTITIES_, config.getDocument().getDocumentElement());
            if (nl != null && nl.getLength() > 0) {
                for (int ii = 0; ii < nl.getLength(); ii++) {
                    Element elm = (Element) nl.item(ii);
                    String eclass = elm.getAttribute("class");
                    if (eclass != null && !eclass.isEmpty()) {
                        Class<?> cls = Class.forName(eclass);
                        createsql = dbq.getCreateTableDDL(cls);
                        for (String sql : createsql) {
                            log.debug("TABLE SQL [" + sql + "]");
                            stmnt.execute(sql);
                        }
                        createIndex(elm, cls, dbq, stmnt);
                    }
                }
            }

        } finally {
            if (stmnt != null && !stmnt.isClosed())
                stmnt.close();

        }
    } else {
        List<AbstractEntity> versions = read("", DBVersion.class, 1);
        if (versions == null || versions.isEmpty()) {
            throw new Exception("Error retrieving Schema Version. Database might be corrupted.");
        }
        for (AbstractEntity ver : versions) {
            if (ver instanceof DBVersion) {
                DBVersion dbv = (DBVersion) ver;
                if (dbv.getVersion().compareTo(version) != 0) {
                    throw new Exception("Database Version missmatch, Expection version [" + version
                            + "], current DB version [" + dbv.getVersion() + "]");
                }
            }
        }
    }
}