• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Close if SQLException Occured?

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I open a connection to my Database and there was some sort of a problem with the stmt or result set and an SQLException occured, do I need to close the Connection to the Database in the SQLException Block?

Is this correct or is ther another way?
Thanks
[ February 08, 2002: Message edited by: Gregg Bolinger ]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just check the connection for null before trying to close it.
catch(SQLEXception problem) {
try{
if(connection != null)
...close connection
}
catch(some exception)
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it's really the same as my code, only I am checking the connection first. Right?
Ok, thanks for the info.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg,
your code looks like the norm(with Bosun's suggestion included in the inner try/catch block).
<unsubstantiated hearsay> Some people use finally blocks, but I've heard there can be some problems with that too</unsubstantiated hearsay> Maybe someone can verify that.
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic