user5260030
user5260030

Reputation:

Java : JDBC Connection Issue When Click On Button

I'm facing a problem in this program there are two GUI's.When user click on button it checks for a database connection when connection become successful then second GUI appear which has JComboBox. But the problem is it doesn't show the catalogs of mysql in JComboBox.
Main Method:

public class Main {

    public static void main(String[] args) {


        Gui obj = new Gui();



    }

}

First Gui

public class Gui extends JFrame {

    Connector c = new Connector();

    private JButton b1;

    public Gui() {

        b1 = new JButton("Click To Connect");
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                if (c.getConnect() == true) {
                    dispose();

                    new Gui2();
                }

            }

        });

        add(b1);
        setLayout(new FlowLayout());
        setSize(300, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }

}

Connection Class

public class Connector {

    private Connection conn;

    public boolean getConnect() {

        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "john", "root");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());

        }

        if (conn == null) {
            System.out.println("Connection Failed");

            return false;
        }

        System.out.println("Connection Success");

        return true;

    }

}

ComboBox GUI

public class Gui2 extends JFrame {
    private JComboBox box;

    Connection connection;

    public Gui2() {

        box = new JComboBox();

        opencatalog();

        add(box);
        setLayout(new FlowLayout());
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    private void opencatalog() {

        try {
            DatabaseMetaData meta = connection.getMetaData();

            ResultSet rs = meta.getCatalogs();

            List ct = new ArrayList();

            while (rs.next()) {

                ct.add(rs.getString(1));

            }

            rs.close();
            box.setModel(new DefaultComboBoxModel(ct.toArray()));

            box.setSelectedItem(connection.getCatalog());

            box.setEnabled(ct.size() > 0);
        }

        catch (Exception e) {
            System.out.println(e.toString());

        }
        box.setEnabled(false);

    }

}

Upvotes: 1

Views: 293

Answers (1)

SatyaTNV
SatyaTNV

Reputation: 4135

  1. Connector class

change the return type to Connection and return conn.

public Connection getConnect() {

....
return conn
}
  1. Gui class, change the condition

     public void actionPerformed(ActionEvent arg0) { 
                Connection conn= c.getConnect();  
                if (conn!=null) {
                    new Gui2(conn);//pass connection object here
                    dispose();
                }
    
            }
    
  2. Gui2 class, constructor should be

    public Gui2(Connection conn)
    {
       connection=conn;
       box = new JComboBox();
       .................
    }
    
  3. Gui2 class,

     box.setEnabled(true);//should be enabled,
    

Upvotes: 1

Related Questions