Example usage for java.sql ResultSet isClosed

List of usage examples for java.sql ResultSet isClosed

Introduction

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

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this ResultSet object has been closed.

Usage

From source file:com.test.db.MySqlConnection.java

/**
 * Closes active MySQL connection//from   www .  j  a va2 s.c o m
 *
 * @param resultSet the active connection
 */
public static void close(ResultSet resultSet) {
    if (resultSet == null)
        return;

    try {
        if (resultSet.isClosed())
            return;

        resultSet.close();
    } catch (SQLException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:com.splicemachine.homeless.TestUtils.java

public static int printResult(String statement, ResultSet rs, PrintStream out) throws SQLException {
    if (rs.isClosed()) {
        return 0;
    }//from  w w  w  . java2  s .  c  o  m
    int resultSetSize = 0;
    out.println();
    out.println(statement);
    List<Map> maps = TestUtils.resultSetToOrderedMaps(rs);
    if (maps.size() > 0) {
        List<String> keys = new ArrayList<String>(maps.get(0).keySet());
        for (String col : keys) {
            out.print(" " + col + " |");
        }
        out.println();
        for (int i = 0; i < keys.size(); ++i) {
            out.print("-----");
        }
        out.println();
        for (Map map : maps) {
            ++resultSetSize;
            for (String key : keys) {
                out.print(" " + map.get(key) + " |");
            }
            out.println();
        }
    }
    out.println("--------------------");
    out.println(resultSetSize + " rows");
    return resultSetSize;
}

From source file:org.apache.hadoop.raid.DBUtils.java

public static void close(ResultSet generatedKeys, PreparedStatement[] pstmts, Connection conn) {
    if (generatedKeys != null) {
        try {//from   www  . jav a  2 s. com
            generatedKeys.close();
        } catch (Exception e) {
            LOG.warn("Error to close ResultSet", e);
        } finally {
            try {
                if (!generatedKeys.isClosed()) {
                    LOG.warn("ResultSet is not closed");
                    DBUtils.numDBOpenObjects++;
                }
            } catch (Exception ignore) {
                DBUtils.numDBOpenObjects++;
            }
        }
    }
    if (pstmts != null && pstmts.length > 0) {
        for (PreparedStatement pstmt : pstmts) {
            if (pstmt == null) {
                continue;
            }
            try {
                pstmt.close();
            } catch (Exception e) {
                LOG.warn("Error to close PreparedStatement", e);
            } finally {
                try {
                    if (!pstmt.isClosed()) {
                        LOG.warn("PreparedStatement is not closed");
                        DBUtils.numDBOpenObjects++;
                    }
                } catch (Exception ignore) {
                    DBUtils.numDBOpenObjects++;
                }
            }
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (Exception e) {
            LOG.warn("Error to close Connection", e);
        } finally {
            try {
                if (!conn.isClosed()) {
                    LOG.warn("Connection is not closed");
                    DBUtils.numDBOpenObjects++;
                }
            } catch (Exception ignore) {
                DBUtils.numDBOpenObjects++;
            }
        }
    }
}

From source file:org.biokoframework.system.repository.sql.util.SqlStatementsHelper.java

public static <DE extends DomainEntity> ArrayList<DE> retrieveEntities(ResultSet result, Class<DE> entityClass,
        SqlTypesTranslator translator, IEntityBuilderService entityBuilder) throws SQLException {
    ArrayList<DE> entities = new ArrayList<DE>();

    Set<Entry<String, Field>> fields = new HashSet<Map.Entry<String, Field>>();
    try {//  ww  w .  ja  v a 2  s .c o m
        fields = ComponingFieldsFactory.createWithAnnotation(entityClass).entrySet();
    } catch (Exception exception) {
        // THIS SHOULD NEVER HAPPEN
        LOGGER.error("retrieving fields:", exception);
        exception.printStackTrace();
    }

    while (!result.isClosed() && result.next()) {
        Fields entityFields = new Fields();

        for (Entry<String, Field> aFieldEntry : fields) {
            String fieldName = aFieldEntry.getKey();
            entityFields.put(fieldName,
                    translator.convertFromDBValue(fieldName, result, aFieldEntry.getValue()));
        }
        entityFields.put(DomainEntity.ID, translator.convertFromDBValue(DomainEntity.ID, result, null));

        entities.add(entityBuilder.getInstance(entityClass, entityFields));
    }

    return entities;
}

From source file:org.okinawaopenlabs.orientdb.client.ConnectionManagerJdbc.java

synchronized public void close(ResultSet rs) throws SQLException {
    if (rs != null && !rs.isClosed()) {
        DbUtils.close(rs);/* w ww. java  2 s  .  c o m*/
    }
}

From source file:com.reydentx.core.client.MySQLClient.java

public void close(java.sql.ResultSet rs) {
    try {/*from  w  ww.j  a  v  a2s .c  om*/
        if (rs != null && !rs.isClosed()) {
            rs.close();
        }
    } catch (Exception ex) {
        _logger.error(ex.getMessage(), ex);
    }
}

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 {//from  ww  w. j  a  v  a 2 s  .co  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:org.sentinel.instrumentationserver.metadata.MetadataDAO.java

/**
 * Get the ID of the data set of metadata for a SHA 256 hash of an APK.
 *//*w w w  . j  a v  a2  s  . c o m*/
private long getMetadataId(String sha256Hash) {
    connectToDatabase();

    String sqlStatementGetMetadataIdFromHash = QueryBuilder.getQueryToGetMetadataIdFromSha256Hash();
    try {
        PreparedStatement preparedStatement = databaseConnection
                .prepareStatement(sqlStatementGetMetadataIdFromHash);
        preparedStatement.setString(1, sha256Hash);
        ResultSet resultSet = preparedStatement.executeQuery();
        long id = -1;
        if (!resultSet.isClosed()) {
            id = resultSet.getLong(1);
            resultSet.close();
        }
        preparedStatement.close();
        return id;

    } catch (SQLException e) {
        e.printStackTrace();
    }

    return -1;
}

From source file:org.sonar.server.db.ResultSetIteratorTest.java

@Test
public void create_iterator_from_result_set() throws Exception {
    dbTester.prepareDbUnit(getClass(), "feed.xml");

    PreparedStatement stmt = connection.prepareStatement("select * from fake order by id");
    ResultSet rs = stmt.executeQuery();
    FirstIntColumnIterator iterator = new FirstIntColumnIterator(rs);

    assertThat(iterator.next()).isEqualTo(10);
    assertThat(iterator.next()).isEqualTo(20);
    assertThat(iterator.next()).isEqualTo(30);

    iterator.close();//from  w  ww . j  av a 2  s .co m
    assertThat(rs.isClosed()).isTrue();
    stmt.close();
}

From source file:com.flexoodb.engines.FlexDBDataEngine.java

public Collection<Object> runQuery(String query, Class c, boolean usedefaultimplementation) throws Exception {
    Vector v = new Vector();
    Connection conn = null;/*w  ww .j  a  v a2 s. c  o m*/
    try {
        conn = (Connection) _pool.getConnection();
        String tablename = query.split("\\s")[3];

        if (checkTable(tablename, conn, false)) {
            StringBuffer q = new StringBuffer();
            boolean idonly = true;

            if (query.toUpperCase().indexOf("WHERE") > 0) {
                String sub = query.substring(query.toUpperCase().indexOf("WHERE"));
                String[] s = (FlexUtils.replaceString(sub, "'", "")).split("\\s");
                for (int i = 0; i < s.length; i++) {
                    String[] qa = s[i].split("=");
                    if (qa.length == 2) {
                        if (qa[0].equals("id")) {
                            q.append("(id='" + qa[1] + "')");
                        } else {
                            String e = qa[0];
                            if (e.startsWith("(")) {
                                q.append("(");
                                e = FlexUtils.replaceString(FlexUtils.replaceString(e, "(", ""), ")", "");
                            }
                            String val = qa[1];
                            boolean closeit = false;
                            if (val.endsWith(")")) {
                                closeit = true;
                                val = FlexUtils.replaceString(FlexUtils.replaceString(val, "(", ""), ")", "");
                            }

                            q.append("(element='" + e + "' and value='" + val + "')" + (closeit ? ")" : ""));
                            idonly = false;
                        }
                    } else {
                        if (qa.length == 1) {
                            q.append(" " + qa[0] + " ");
                        }
                    }
                }
            }

            PreparedStatement ps = null;
            boolean searchindex = false;
            if (idonly) {
                ps = (PreparedStatement) conn
                        .prepareStatement("select distinct id from " + tablename + " " + q.toString());
            } else {
                ps = (PreparedStatement) conn
                        .prepareStatement("select distinct id from " + tablename + "_index " + q.toString());
                searchindex = true;
            }

            ResultSet rec = ps.executeQuery();
            // check if a record was found
            while (rec != null && !rec.isClosed() && rec.next()) {
                String id = rec.getString("id");
                try {
                    Object o = null;
                    PreparedStatement ps2 = (PreparedStatement) conn
                            .prepareStatement("select content from " + tablename + " where id='" + id + "'");
                    ResultSet res = ps2.executeQuery();
                    // check if a record was found
                    if (res != null && res.next()) {
                        o = _flexutils.getObject(res.getString("content"), c);
                        ps2.close();
                    } else {
                        ps2.close();
                        if (searchindex) {
                            // then the values found must be orphans! we delete the index contents
                            removeValues(id, tablename, conn);
                        }
                    }

                    if (o != null) {
                        v.add(o);
                    }
                } catch (Exception g) {
                    throw g;
                }
            }
        }
    } catch (Exception f) {
        throw f;
    } finally {
        try {
            if (conn != null) {
                _pool.releaseConnection(conn);
            }

        } catch (Exception g) {
        }
    }
    return v;
}