Search code examples
javamysqlswingjcombobox

JCombobox value retrieve from MySql Data base


I use the following code for retrieve the data from my Sql DB.retrieve StudName and StudId from Data Base.StudName will display JComboBox.But how to show the StudId in Messagebox.If I select the first record in combo box means i need the corresponding Id in Message box.how to do this? thanks in Advance

public class FrmeA extends javax.swing.JFrame  {

          FrmA1 f1=new FrmA1();
          Statement TmpFlxTSt=null;
          ResultSet TmpFlxTRs=null;
          GContnStr GCS=new GContnStr();

        public FrmeA() {
             initComponents();
        }
    public void combo(){

         try{
                    GCS.GContnStr();
                    TmpFlxTSt= GCS.GCotnStr.createStatement();
                    String select = "Select StudId,StudName from studentmaster";
                    TmpFlxTRs = TmpFlxTSt.executeQuery(select);
                      while(TmpFlxTRs.next()){
                       cbx.addItem(TmpFlxTRs.getString("StudName"));
                       Object comboitem=cbx.getSelectedItem();

                   }
                        TmpFlxTRs.close();
                        TmpFlxTSt.close();
            }
               catch(Exception e){
                   System.out.println(e);

        }

    }

Solution

  • retrieve StudName and StudId from Data Base.StudName will display JComboBox.But how to show the StudId in Messagebox.

    You can use a JOptionPane for this purpose, something like this would serve,

    String message = TmpFlxTRs.getString("StudId");
    JOptionPane.showMessageDialog(parent, message);
    

    If I select the first record in combo box means i need the corresponding Id in Message box.how to do this?

    Configure an actionlistener for this purpose or an ItenStateChanged listener would be better.