Hi
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.
miguel lisboa wrote:as to my very first code, is it ok or is there something to catch?
You're passing the SQLException up to the caller, somewhere you'll have to handle it eventually.
I personally don't allow SQLExceptions to escape the DAO layer, unless I can handle them somehow, I wrap them into an unchecked exception and have the topmost layer of code catch them and log them.
Post by:autobot
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards