i'm aware of new and old way of closing connections:
the old way (here i'm unsure if i really get it closed and if i need to catch something...) please note i'm saying this because sometimes i want to manually delete WEB-INF dir and get an error message saying mysql driver is in use, when i have everything closed and terminated!
and here is the try with resources way: is it correct?
You're not closing the connection in the "try with resources" version at all.
The Connection interface is AutoCloseable, so it is possible to close a connection using the new "try with resources" syntax, and it would be the preferred way. But you aren't doing so. You would need to move the Connection declaration into the try statement.
miguel lisboa wrote:do you think i need to close the statement too?
I had thought you'd ask...
Short answer: yes.
Long answer: it is true that it is probably not strictly necessary with most JDBC drivers; if a connection is closed, the statement's resources will be - in most cases - freed too. But you cannot be really sure about this unless it is explicitly stated in the documentation of the JDBC driver you use. Simply closing the statement is easier than verifying you don't need to close it. And double so when using the try-with-resources syntax, where you don't have to call it explicitly, just move the declaration to the try () section.
Joined: Feb 08, 2004
i'm using mysql, so i believe i'm quite safe without closing the statement; anyway thanks for the advice and explanation
as to my very first code, is it ok or is there something to catch?