Is there any way that I don't use lots of IF statements?,right now it is according to Index of combo box items.

else if(e.getSource().equals(jcb1))
        {
            int selectedindex = jcb1.getSelectedIndex();
            String comb_string = (String)jcb1.getSelectedItem();            
            if(selectedindex==1)
            {
                try {

                //String sql_command ="select * , count(*) over (partition by 1) total_rows from Employer where Employername = ? ";             
                String sql_command ="select * from Employer where Employername = ? ";
                    PreparedStatement st=con.prepareStatement(sql_command);                 
                    st.setString(1,comb_string);
                    Result = st.executeQuery();
                    int test =0;
                    String Store="";
                    String Response ="";
                    test++;
                    if(Result.next())
                    {                       
                        String add1=Result.getString(3);
                        first.setText(add1);
                        String add2=Result.getString(28);
                        second.setText(add2);
                        String add3=Result.getString(4);
                        third.setText(add3);
                        String add4=Result.getString(6);
                        fourth.setText(add4);

                        if(test !=0)
                        {
                            /*Result.absolute(test);
                            DisplayData();*/
                            Response = "Number of Records that Matches :"+test+Store;
                            JOptionPane.showMessageDialog(null, Response);              
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null,"Could not be found");
                        }


                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }                   
            }           
        }   

Dani AI

Generated

Your current handler checks the combo index and repeats DB + UI code for each case. That works, but it quickly becomes hard to maintain. As suggested, separating cases into methods is a step forward — a more robust pattern is to avoid index-based branching entirely by putting the data into objects (or IDs) and wiring the UI to those objects.

Populate the JComboBox with employer objects (or small id/display pairs) once, then on selection simply pull the selected object and populate fields. Example pattern:

class Employer {
  private final int id;
  private final String name, addr, phone, contact;
  // constructor + getters
  public String toString() { return name; } // what the combo shows
}

DefaultComboBoxModel<Employer> model =
  new DefaultComboBoxModel<>(employers.toArray(new Employer[0]));
jcb1.setModel(model);

jcb1.addActionListener(e -> {
  Employer sel = (Employer) jcb1.getSelectedItem();
  if (sel != null) {
    first.setText(sel.getAddr());
    second.setText(sel.getPhone());
    third.setText(sel.getContact());
  }
});

If you must query the DB on selection, do that work off the Event Dispatch Thread with a SwingWorker and pass back an Employer instance to the UI thread. Another option for fixed, small sets is a Map<Integer,Runnable> or Map<String,Consumer<Employer>> to map choices to handlers — but those are more verbose than using an object model.

Practical tips: load lists once when possible, store and use a unique ID (not just display text), use column names instead of magic column indices, always close ResultSet/PreparedStatement in try-with-resources, and never run blocking DB calls on the EDT. These changes remove the cascade of ifs, make the code testable, and keep the UI responsive.

You don't give a lot of context for this question, but it looks like you should be using a switch based on the selected index, and in that calling separate methods for each of the possible cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.