Applikation bleibt beim beenden hängen

k4ufi

Grünschnabel
Hallo.

Ich habe eine Applikation mit der Netbeans IDE erstellt und wollte damit einen Datenbankzugriff auf knopfdruck machen.

Soweit so gut. Nur wenn ich jetzt das Programm mittels dem X rechts oben beende bzw über das Menü dann hängt sich das komplette Programm auf und kann nur mehr mittel kill -9 getötet werden. Es schreibt auch leider keine Fehler raus...

Anbei der Quellcode:
Code:
/*
 * BonsaiManagementApp.java
 */
package bonsai_management;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class BonsaiManagementApp extends SingleFrameApplication
{

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup()
    {
        show(new MainView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root)
    {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of BonsaiManagementApp
     */
    public static BonsaiManagementApp getApplication()
    {
        return Application.getInstance(BonsaiManagementApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args)
    {
        launch(BonsaiManagementApp.class, args);
    }
}

Code:
/*
 * BonsaiManagementView.java
 */
package bonsai_management;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class MainView extends FrameView
{

    public MainView(SingleFrameApplication app)
    {
        super(app);
        bonsaiListModel = new DefaultListModel();
        initComponents();
    }

    @Action
    public void showAboutBox()
    {
        if( aboutBox == null )
        {
            JFrame mainFrame = BonsaiManagementApp.getApplication().getMainFrame();
            aboutBox = new AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        BonsaiManagementApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        btnShowBonsai = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listAllBonsai = new javax.swing.JList();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bonsai_management.BonsaiManagementApp.class).getContext().getResourceMap(MainView.class);
        btnShowBonsai.setText(resourceMap.getString("btnShowBonsai.text")); // NOI18N
        btnShowBonsai.setName("btnShowBonsai"); // NOI18N
        btnShowBonsai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowBonsaiActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        listAllBonsai.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listAllBonsai.setName("listAllBonsai"); // NOI18N
        jScrollPane1.setViewportView(listAllBonsai);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(btnShowBonsai)
                .addGap(52, 52, 52)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(btnShowBonsai))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bonsai_management.BonsaiManagementApp.class).getContext().getActionMap(MainView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>


    private void btnShowBonsaiActionPerformed(java.awt.event.ActionEvent evt)                                              
    {                                                  
            // TODO Open new Frame or Dialog to show all bonsai
        DBConnection dbcon = new DBConnection();
        try
        {
            dbcon.connect();
        }
        catch( Exception e )
        {
            Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, e);
            System.err.println("Kacke: DBConnection not initialized");
        }

        ArrayList<Bonsai.BonsaiType> bonsaiTypes = dbcon.getBonsaiTypes();

        for( Bonsai.BonsaiType bonsaiType : bonsaiTypes )
        {
            bonsaiListModel.addElement(bonsaiType.getGerman() + " - " + bonsaiType.getBot());
        }

        bonsaiTypes.clear();
        dbcon.close();
        listAllBonsai.setModel(bonsaiListModel);
    }                                             

    // Variables declaration - do not modify
    private javax.swing.JButton btnShowBonsai;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listAllBonsai;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration
    private JDialog aboutBox;
    private DefaultListModel bonsaiListModel;
}

Klasse für die DB Verbindung
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bonsai_management;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kaufman4
 */
public class DBConnection
{

     private final String     hostname;
     private final String     port;
     private final String     dbname;
     private final String     user;
     private final String     password;
     private       Connection connection = null;


     public DBConnection() 
     {
        hostname = "localhost";
        port     = "3306";
        dbname   = "bonsaimanagement";
        user     = "blub";
        password = "blub";
     }

     public void connect() throws SQLException, ClassNotFoundException,
                                  IllegalAccessException, InstantiationException
     {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://"+hostname+":"+port+"/"+dbname;
        connection = DriverManager.getConnection(url, user, password);
     }

     public void close()
     {
        try
        {
            if( connection != null )
                connection.close();
        }
        catch( SQLException ex )
        {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("KAKE: closing connection: " + ex.getMessage() );
        }
     }

     public ArrayList<Bonsai.BonsaiType> getBonsaiTypes()
     {
        Statement statement = null;
        try
        {
            statement = connection.createStatement();
            String sqlQuerry = "SELECT * FROM treetype;";
            ResultSet resultSet = statement.executeQuery(sqlQuerry);            

            ArrayList<Bonsai.BonsaiType> bonsaiTypes = new ArrayList<Bonsai.BonsaiType>();

            while( resultSet.next() )
            {
                Bonsai.BonsaiType bonsaiType = new Bonsai.BonsaiType(resultSet.getString("name_german"), resultSet.getString("name_bot"));
                bonsaiTypes.add(bonsaiType);
            }

            resultSet.close();
            statement.close();

            return bonsaiTypes;
        }
        catch( SQLException ex )
        {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("KAKE: getBonsaiTypes: " + ex.getMessage() );
            return null;
        }
     }
}

vielen Dank für die Hilfe!

lG


edit: Dürft sich um ein Problem mit Ubuntu Handeln, den im Windows funktioniert es komischer weise...
 
Zuletzt bearbeitet:
Zurück