aspose 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 15286
    
    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 - A Test Data Generation Platform
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: 15286
    
    6

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cleanup when swing app exits
 
Similar Threads
POOLMAN - HELP
Removing temporary files
Performance Problems with DB Connection
Help please!!! Is there a method for closing sessions java-side?
Connection Pooling Help Needed