I am a learner in java . I want to connect my MYSQL database table to a combo box. I created 3 combo box on my jpanel.When I select first two combo box items its automatically call the data base and show the particular content from my table in the last combo box.

Like when we choose the country combo box in a web page its automatically listed all the states belong to the particular country.

So pleas send some example code for my problem

What you ask consist of many technologies. A small example is not going to be enough and none here is going to spend time writing code that has already been written many times in this forum.

Where are you having problems with? Displaying the combo boxes, firing the "change" event, writing sql, connecting to the database.

Start by writing some code. Write the first 2 combo boxes and when one of those two is clicked write some code that prints their values.

Once you have done that write a method that connects to the database and retrieves the data you want. Then call that method when one of those boxes is clicked clicked at the place where you printed the values of the boxes.

try
                {
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql://localhost:3307/dbname","root","root");
                }
                catch(ClassNotFoundException e)  {
            System.err.println("Failed to load driver");
            e.printStackTrace();
        }catch(SQLException e){
            System.err.println("Unable to connect");
            e.printStackTrace();}

private void s_comboItemStateChanged(java.awt.event.ItemEvent evt) {
        // TODO add your handling code here:
        if(evt.getSource()==f_combo)
        {
            try
            {
                Statement st=con.createStatement();
                ResultSet rs=st.executeQuery("SELECT * FROM tablename WHERE column = '" +f_combo.getSelectedItem()+"'");

                while(rs.next())
                {
                    s_combo.addItem(rs.getInt("3"));
                }
            }
            catch(SQLException sq)
            {
                sq.printStackTrace();
            }
        }
    }

f_combo contains branches and s_combo link to my database table which contains all subjects. When I select a particular branch from f_combo then all subjects of that branch add in the s_combo. But my code does not retrieve the data from my table to s_combo. Please help me on this

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.