aspose file tools
The moose likes JDBC and the fly likes Closing connection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Closing connection problem" Watch "Closing connection problem" New topic
Author

Closing connection problem

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
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

Joined: May 10, 2001
Posts: 3244
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.


Dave
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Closing connection problem
 
Similar Threads
error: Communications link failure due to underlying exception
Dont know what is happenning
Closing DB issue in new method
JSP redirect
JDBC Question