Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closing connection problem

 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
It is possible to close a connection in the same method (i.e. creating a statement + ResultSet,..).
But when I am making several methods() within the same class to deal with different types of data + opening and closing the database, I cannot close it anymore with the close() method.
Or is it better to open/close everytime you need to make a connection.
I thought it was better to make a seperate method to close the connection.
Here is my method to close the connection.
The instance databaseConnection is declared as a global variabel in the class.

public void closeVeldDAO(){
// close connection

try{
if (databaseConnection != null){

databaseConnection.close();
System.out.println ("na connection.close");
}
}
catch(SQLException sqle){
System.err.println(sqle.getMessage());
}
}
When I refer to this method I'll get back a nullPointerException.
Anybody any advice ?
thanks,
Kristof
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristof

Originally posted by Kristof Camelbeke:
When I refer to this method I'll get back a nullPointerException.

When? or do you mean you do get a NullPointerException when you call the method? If you getting an actual error what is the error message?
I dont see anything in there that could cause that Exception what do the method calls look like that call it. How do you open the connection? Is this an app or a servlet?
If it's a servlet just open it in init and close it in destroy, in an app you could create a static variable to represent the connection then close it in finalize.
sorry if this doesn't help too much but some info is needed. - I think.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic