wood burning stoves 2.0*
The moose likes JDBC and the fly likes Close if SQLException Occured? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Close if SQLException Occured?" Watch "Close if SQLException Occured?" New topic
Author

Close if SQLException Occured?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

GenRocket - Experts at Building Test Data
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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)


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jul 09, 2001
Posts: 1879

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
 
 
subject: Close if SQLException Occured?