0

i have read number of posts in which people says that does closing connection in java closes resultset, statements or etc. but i didn't find answer to my question.

i am asking that i am not doing Con.close() but i am doing rst.close() so will it close connection or not?

so i just wanna know that does connection get closed when we close ResultSet or not?

mega6382
  • 9,211
  • 17
  • 48
  • 69
  • i dont think so – GURMEET SINGH Oct 27 '17 at 20:03
  • i have read the same post but it does not tells what i am asking – GURMEET SINGH Oct 27 '17 at 20:03
  • 2
    What would make you think it did? Did you **read the documentation**? --- [`ResultSet.close()`](https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html#close--): *A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed* --- [`Statement.close()`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#close--): *When a Statement object is closed, its current ResultSet object, if one exists, is also closed.* --- Both say that Statement closes ResultSet, not the other way, so why would you even think that? – Andreas Oct 27 '17 at 20:33
  • 1
    @Andreas You might want to look at JDBC 4.1's [`Statement.closeOnCompletion`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#closeOnCompletion--), closing of a `ResultSet` can close a `Statement`, if that was explicitly requested. But that doesn't extend to `Connection`. – Mark Rotteveel Oct 29 '17 at 07:16
  • @MarkRotteveel *"if that was explicitly requested"*. If you wrote code to specifically request that closing the `ResultSet` would close the `Statement`, why would you (i.e. OP) ask if it did. You'd already know. – Andreas Oct 29 '17 at 07:19
  • Your comment says "not the other way", I just point out that it is possible. And honestly, given the OP has posted no code, who knows what he did. – Mark Rotteveel Oct 29 '17 at 07:21

1 Answers1

2

In a word - no.

Closing the ResultSet just closes the cursor associated with it so you can't call next() on it. The Connection object is not affected, and can be used to execute new statements.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • ok thx. for ur help – GURMEET SINGH Oct 27 '17 at 20:05
  • You might want to look at JDBC 4.1's [`Statement.closeOnCompletion`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#closeOnCompletion--), closing of a `ResultSet` can close a `Statement`, if that was explicitly requested. But that doesn't extend to `Connection`. – Mark Rotteveel Oct 29 '17 at 07:17