0

I have a query to fetch the name and id of companies. I am able to fetch all the companies but my issue is with the id of the companies. I have a combo box to display list of companies and another label which displays the id of the company selected in the combo box.

Here is my issue, when i change the selected company in the combo box, the label showing the id still remains displays the id of the initial company selected. Is there a way that i could continuously query the database to update the id?

PS I display companies in the combo box with reference no.

public AssignCompanies() {
    initComponents();
    conn = DBConnection.ConnectDB();

    FindCompanyID();
}

public void FindCompanyID() {
    String sql = "Select * from Company where ID = "+FindSelectedCompany.getSelectedItem();

    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()) {
            String getSelectedCompany = rs.getString("Reference Number");
            seats.setText(getSelectedCompany);
        }
    }
}         
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 2
    *Continuously* query the database? Why? Why not just update the label when the combo box changes? Doesn't the combo box (or at least an in-memory list) contain the value you're looking for? – David Aug 20 '17 at 10:35

1 Answers1

0

You don't need to query the database everytime a user changes the selection in combo box, you can do the following:

  • Get ids and names of all the companies, and dynamically add option elements into combo box with value being id and label being name of the company
  • Configure an onchange event on that combo box, here's an example
  • Write a javscript function to update the value in another combo box in the onchange event configured in previous step (here's an example of how to update the value)
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102