• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 simple questions..

 
Grub de Bliek
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
e.printStackTrace();
} finally {
if (statement != null)
statement.close();
if (connection != null)
connection.close();
}
}
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?
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to encapsulate each close() call in their own try/catch blocks so they are each executed even if one throws an exception.
 
Rudy Dakota
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gerben,
the answer to your first question: yes!
Good riding,
Rudy.
 
Grub de Bliek
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic