• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Closing connection problem

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic