File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes 2 simple questions.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "2 simple questions.." Watch "2 simple questions.." New topic

2 simple questions..

Grub de Bliek

Joined: Sep 09, 2003
Posts: 19
1. When I close my statement and my connection, do I lose the ResultSet I just queried?
2. Look at this piece of code:
//querying code....
} catch(SQLException e) {
} finally {
if (statement != null)
if (connection != null)
This can't be done cuz both close() methods must be handled with an SQLException as well. The way I see it is that I *must* catch that SQLException one level higher, but I don't want that. All my exceptions must be handled in this method. Is there a way to solve this?

When the compiler isn't happy, nobody is happy...
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

You want to encapsulate each close() call in their own try/catch blocks so they are each executed even if one throws an exception.

[How To Ask Questions On JavaRanch]
Rudy Dakota
Ranch Hand

Joined: Jul 27, 2002
Posts: 54
the answer to your first question: yes!
Good riding,
Grub de Bliek

Joined: Sep 09, 2003
Posts: 19
Ok thanks!
I knew I could put a try/catch block in a finally block, but I was thinking that prevented the finally block from executing. I see that isn't correct, if an exception occurs in a finally block, the rest of the code is executed anyway.
I agree. Here's the link:
subject: 2 simple questions..
It's not a secret anymore!