aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Close if SQLException Occured?" Watch "Close if SQLException Occured?" New topic

Close if SQLException Occured?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286

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?
[ February 08, 2002: Message edited by: Gregg Bolinger ]

GenRocket - A Test Data Generation Platform
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
You can just check the connection for null before trying to close it.
catch(SQLEXception problem) {
if(connection != null)
...close connection
catch(some exception)

So much trouble in the world -- Bob Marley
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286

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

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.
I agree. Here's the link:
subject: Close if SQLException Occured?
Similar Threads
strange checkbox problem
Closing Connection Which way ????
Could not locate DB driver
jsf and database connection problem