File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cleanup when swing app exits

 
Dave Segal
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just call:
// Close the result set
rs.close();
// Close the statement
stmt.close();
// Close the connection
con.close();
 
Dave Segal
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Segal
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg - that solved it. Thanks. Dave
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic