File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes cleanup when swing app exits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "cleanup when swing app exits" Watch "cleanup when swing app exits" New topic
Author

cleanup when swing app exits

Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
I have a swing app that opens a database connection. I need to close the connection when the program exits. Is there an easy way to do this?
Jeff Chang
Greenhorn

Joined: Aug 23, 2001
Posts: 23
Just call:
// Close the result set
rs.close();
// Close the statement
stmt.close();
// Close the connection
con.close();
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Jeff - I should have been clearer on the description of my problem. I know how to close the objects. What I need to figure out where to put the code in order to guarantee that it gets executed. Since the app is interactive, the user can exit at anytime by clicking the 'x' in the corner of the window. If I just put the close() code in the main() method it won't get executed.
Dave
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You should have a windowClosing(WindowEvent w) in your application. This way, when your frame gets closed for any reason, this method will get called. You should put your "Cleanup Code" in here, or call a cleanup method from there.


GenRocket - Experts at Building Test Data
Dave Segal
Greenhorn

Joined: Aug 13, 2002
Posts: 20
Gregg - that solved it. Thanks. Dave
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

 
wood burning stoves
 
subject: cleanup when swing app exits