CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Jun 2008
    Posts
    61

    [RESOLVED] how to disconnect to database in this case?

    I'm working with JDBC.

    1) I want to disconnect to database in a destructor because my class uses the connection during execution time. Please give me a hand where I should put my disconnectToDatabase() method.

    2) I googled for that and I used finalize() method but when I closed my frame, it didn't show the message dialog. I think there is memory leak.
    So in fact, when is finalize() method invoked? How to disconnect to database in this case?


    Code:
    public class LeftPane extends JPanel
    {
            private DatabaseLoader databaseLoader;
    	
            public LeftPane()
            {
                    try {
    			databaseLoader = new DatabaseLoader();			
    		} catch (ClassNotFoundException e) {
    			System.err.println("Have no a driver");
    		}
    
                    ...
    
                    // connect to database
    		try {
    			databaseLoader.connectToDatabase();		
    			...
    		} catch (SQLException e) {
    			System.err.println("Database error");
    		}
                    ...
             }
    
             protected void finalize() throws Throwable 
    	{		
    		JOptionPane.showMessageDialog(null, "End"); // it doesn't show when I close my app.
    
                    // disconnect to database
    		databaseLoader.disconnectToDatabase();
    		super.finalize();
    	};
    }
    Thank you.
    Last edited by Emerald214; November 21st, 2009 at 04:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured