Java Code Examples for java.sql.ResultSet.isClosed()

The following are Jave code examples for showing how to use isClosed() of the java.sql.ResultSet class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: elastic-db-tools-for-java   File: MultiShardResultSet.java   View Source Code Vote up 6 votes
@Override
public boolean isClosed() throws SQLException {
    if (this.results != null) {
        for (LabeledResultSet r : this.results) {
            ResultSet s = r.getResultSet();
            if (s != null) {
                try {
                    if (!s.isClosed()) {
                        return false;
                    }
                }
                catch (SQLException e) {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        }
    }
    return true;
}
 
Example 2
Project: tomcat7   File: StatementCache.java   View Source Code Vote up 5 votes
@Override
public void closeInvoked() {
    //should we cache it
    boolean shouldClose = true;
    if (cacheSize.get() < maxCacheSize) {
        //cache a proxy so that we don't reuse the facade
        CachedStatement proxy = new CachedStatement(getDelegate(),getSql());
        proxy.setCacheKey(getCacheKey());
        try {
            // clear Resultset
            ResultSet result = getDelegate().getResultSet();
            if (result != null && !result.isClosed()) {
                result.close();
            }
            //create a new facade
            Object actualProxy = getConstructor().newInstance(new Object[] { proxy });
            proxy.setActualProxy(actualProxy);
            proxy.setConnection(getConnection());
            proxy.setConstructor(getConstructor());
            if (cacheStatement(proxy)) {
                proxy.cached = true;
                shouldClose = false;
            }
        } catch (Exception x) {
            removeStatement(proxy);
        }
    }
    if (shouldClose) {
        super.closeInvoked();
    }
    closed = true;
    delegate = null;

}
 
Example 3
Project: my-diploma-work   File: ConnectionDB.java   View Source Code Vote up 5 votes
public void closeConnection(Connection con, PreparedStatement ps, ResultSet rs) throws SQLException {
    if (con.isClosed() == false) {
        con.close();
    }
    if (ps.isClosed() == false) {
        ps.close();
    }
    if (rs.isClosed() == false) {
        rs.close();
    }
}
 
Example 4
Project: ClassroomFlipkart   File: DBUtils.java   View Source Code Vote up 5 votes
public static void closeResultSet(ResultSet rs){
    try{
        if(rs != null && !rs.isClosed()){
            rs.close();
        }
    }catch(Exception e){
        //
    }
}
 
Example 5
Project: apache-tomcat-7.0.73-with-comment   File: StatementCache.java   View Source Code Vote up 5 votes
@Override
public void closeInvoked() {
    //should we cache it
    boolean shouldClose = true;
    if (cacheSize.get() < maxCacheSize) {
        //cache a proxy so that we don't reuse the facade
        CachedStatement proxy = new CachedStatement(getDelegate(),getSql());
        proxy.setCacheKey(getCacheKey());
        try {
            // clear Resultset
            ResultSet result = getDelegate().getResultSet();
            if (result != null && !result.isClosed()) {
                result.close();
            }
            //create a new facade
            Object actualProxy = getConstructor().newInstance(new Object[] { proxy });
            proxy.setActualProxy(actualProxy);
            proxy.setConnection(getConnection());
            proxy.setConstructor(getConstructor());
            if (cacheStatement(proxy)) {
                proxy.cached = true;
                shouldClose = false;
            }
        } catch (Exception x) {
            removeStatement(proxy);
        }
    }
    if (shouldClose) {
        super.closeInvoked();
    }
    closed = true;
    delegate = null;

}
 
Example 6
Project: AeroStory   File: PortalPlayerInteraction.java   View Source Code Vote up 5 votes
public boolean hasLevel30Character() {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = DatabaseConnection.getConnection().prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?");
        ps.setInt(1, getPlayer().getAccountID());
        rs = ps.executeQuery();
        while (rs.next()) {
            if (rs.getInt("level") >= 30) {
                return true;
            }
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            if (ps != null && !ps.isClosed()) {
                ps.close();
            }
            if (rs != null && !rs.isClosed()) {
                rs.close();
            }
        } catch (SQLException ex) {
        }
    }
    return false;
}
 
Example 7
Project: calcite-avatica   File: AvaticaStatement.java   View Source Code Vote up 5 votes
/**
 * Executes a prepared statement.
 *
 * @param signature Parsed statement
 * @param isUpdate if the execute is for an update
 *
 * @return as specified by {@link java.sql.Statement#execute(String)}
 * @throws java.sql.SQLException if a database error occurs
 */
protected boolean executeInternal(Meta.Signature signature, boolean isUpdate)
    throws SQLException {
  ResultSet resultSet = executeQueryInternal(signature, isUpdate);
  // user may have cancelled the query
  if (resultSet.isClosed()) {
    return false;
  }
  return true;
}
 
Example 8
Project: QBot   File: QuizController.java   View Source Code Vote up 4 votes
@Override
public QuestionInterface fetchQuestion(long chatID) {
	Connection connection;
	try {
		// lade numberaskedquestions und gameid von dem letzten Spiel mit der chatId
		// lade questiongame von dem Spiel
		// gehe bis zu dem aktuellen numberaskedquestions-Wert und lade die
		// entsprechende Frage
		// inkrementiere numberaskedquestions
		// gib die Frage zurück
		connection = DriverManager.getConnection("jdbc:sqlite:" + QuizController.DB_PATH);
		Statement statement = connection.createStatement();
		int dbGameId, askedQuestions;
		ResultSet gameSet = statement.executeQuery("SELECT gameid, numberaskedquestions FROM game "
				+ "WHERE chatid=" + chatID + " AND gameid=(SELECT MAX(gameid) FROM game)");
		if (gameSet.next()) {
			dbGameId = gameSet.getInt("gameid");
			askedQuestions = gameSet.getInt("numberaskedquestions");

		} else {
			throw new RuntimeException("Es existiert kein Spiel mit der chatId: " + chatID);
		}

		ResultSet questionGameSet = statement
				.executeQuery("SELECT questionid,gameid FROM questiongame WHERE gameid=" + dbGameId);
		int i = 0;
		while (questionGameSet.next() && i < askedQuestions) {
			i++;
		}
		if (!questionGameSet.isClosed()) {
			int questionId = questionGameSet.getInt("questionid");
			Question question = new Question(questionId);
			statement.execute(
					"UPDATE game SET numberaskedquestions=" + (askedQuestions + 1) + " WHERE gameid=" + dbGameId);
			connection.close();
			return question;
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 9
Project: factcast   File: PGFactStream.java   View Source Code Vote up 4 votes
@Override
public void processRow(ResultSet rs) throws SQLException {
    if (isConnected()) {

        if (rs.isClosed()) {
            throw new IllegalStateException(
                    "ResultSet already closed. We should not have got here. THIS IS A BUG!");
        }

        Fact f = PGFact.from(rs);
        final UUID factId = f.id();

        if (postQueryMatcher.test(f)) {
            try {
                subscription.notifyElement(f);
                log.trace("{} onNext called with id={}", request, factId);
            } catch (Throwable e) {
                // debug level, because it happens regularly on
                // disconnecting clients.
                // TODO add sid
                log.debug("{} exception from subscription: {}", request, e.getMessage());

                try {
                    subscription.close();
                } catch (Exception e1) {
                    // TODO add sid
                    log.warn("{} exception while closing subscription: {}", request, e1
                            .getMessage());
                }

                // close result set in order to release DB resources as
                // early as possible
                rs.close();

                throw e;

            }
        } else {
            // TODO add sid
            log.trace("{} filtered id={}", request, factId);
        }
        serial.set(rs.getLong(PGConstants.COLUMN_SER));
    }
}
 
Example 10
Project: elastic-db-tools-for-java   File: MultiShardStatement.java   View Source Code Vote up 4 votes
private MultiShardException validateResultSet(ResultSet r,
        ShardLocation loc) throws SQLException {
    if (r.isClosed()) {
        // ResultSet is already closed. Hence adding an exception in its place.
        return new MultiShardException(loc, new MultiShardResultSetClosedException(
                String.format("The result set for '%1$s' was closed and could not be added.", loc.getDatabase())));
    }
    ResultSetMetaData m = r.getMetaData();
    if (m == null || m.getColumnCount() == 0) {
        // ResultSet does not have proper metadata to read.
        return new MultiShardException(loc, new MultiShardResultSetInternalException(
                String.format("The result set for '%1$s' does not have proper metadata to read and could not be added.", loc.getDatabase())));
    }
    if (this.schemaComparisonTemplate == null) {
        this.schemaComparisonTemplate = r.getMetaData();
        return null;
    }
    for (int i = 1; i <= m.getColumnCount(); i++) {
        // Get the designated column's name.
        String expectedName = this.schemaComparisonTemplate.getColumnName(i);
        String actualName = m.getColumnName(i);
        if (!Objects.equals(expectedName, actualName)) {
            return new MultiShardException(loc, new MultiShardSchemaMismatchException(loc,
                    String.format("Expected schema column name %1$s, but encountered schema column name %2$s.", expectedName, actualName)));
        }

        // Retrieves the designated column's SQL type.
        if (!Objects.equals(this.schemaComparisonTemplate.getColumnType(i), m.getColumnType(i))) {
            return new MultiShardException(loc,
                    new MultiShardSchemaMismatchException(loc,
                            String.format("Mismatched SQL type values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
                                    this.schemaComparisonTemplate.getColumnTypeName(i), m.getColumnTypeName(i))));
        }

        // Get the designated column's specified column size.
        int expectedPrecision = this.schemaComparisonTemplate.getPrecision(i);
        int actualPrecision = m.getPrecision(i);
        if (!Objects.equals(expectedPrecision, actualPrecision)) {
            return new MultiShardException(loc,
                    new MultiShardSchemaMismatchException(loc,
                            String.format("Mismatched nullability values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
                                    expectedPrecision, actualPrecision)));
        }

        // Indicates the nullability of values in the designated column.
        int expectedNullableValue = this.schemaComparisonTemplate.isNullable(i);
        int actualNullableValue = m.isNullable(i);
        if (!Objects.equals(expectedNullableValue, actualNullableValue)) {
            return new MultiShardException(loc,
                    new MultiShardSchemaMismatchException(loc,
                            String.format("Mismatched nullability values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
                                    NullableValue.forValue(expectedNullableValue), NullableValue.forValue(actualNullableValue))));
        }
    }
    return null;
}
 
Example 11
Project: Entrega-Final   File: DaoProduto.java   View Source Code Vote up 4 votes
public static ArrayList<Produto> procurarProduto(String nomeProduto) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM produto WHERE (produto.nome LIKE(?))";

    ArrayList<Produto> listaProdutos = null;

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    ResultSet result = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, "%" + nomeProduto + "%");

        result = preparedStatement.executeQuery();

        while (result.next()) {
            if (listaProdutos == null) {
                listaProdutos = new ArrayList<>();
            }
            Produto produto = new Produto();
            produto.setId(result.getLong("id"));
            produto.setNome(result.getString("nome"));
            produto.setDescricao(result.getString("descricao"));
            produto.setValorCompra(result.getBigDecimal("vl_compra"));
            produto.setValorVenda(result.getBigDecimal("vl_venda"));
            produto.setCategoria(result.getString("categoria"));
            produto.setDtCadastro(result.getDate("dt_cadastro"));

            listaProdutos.add(produto);
        }
    } finally {
        if (result != null && !result.isClosed()) {
            result.close();
        }

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

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

    return listaProdutos;
}
 
Example 12
Project: Entrega-Final   File: DaoProduto.java   View Source Code Vote up 4 votes
public static Produto obterProduto(long id) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM produto WHERE (produto.id=?)";

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    ResultSet result = null;
    try {
        connection = ConnectionUtils.getConnection();

        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setLong(1, id);

        result = preparedStatement.executeQuery();
        if (result.next()) {
            Produto produto = new Produto();
            produto.setId(result.getLong("id"));
            produto.setNome(result.getString("nome"));
            produto.setDescricao(result.getString("descricao"));
            produto.setValorCompra(result.getBigDecimal("vl_compra"));
            produto.setValorVenda(result.getBigDecimal("vl_venda"));
            produto.setCategoria(result.getString("categoria"));
            produto.setDtCadastro(result.getDate("dt_cadastro"));

            return produto;
        }
    } finally {
        if (result != null && !result.isClosed()) {
            result.close();
        }
        if (preparedStatement != null && !preparedStatement.isClosed()) {
            preparedStatement.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }

    return null;
}