Get record using JComboBox from database and get another record to textfield following combobox record?

1.2k views Asked by At

Can I know how we can get record using JComboBox from database and at the same time get another record from database to JTextField following what we choose in combobox?Both records are in database. Example:Get employees name from database using combo box, make selection using the combo box, then get employee id in text field... Thank you.

I'm trying to make a Java frame program having a text area, a combo box link to database which is stock items name and a text field for fill in stock items id. User either use combo box choose items name show items id in text field or fill in items id to show the item name in the combo box automatically.

How do I do that actually?

try {
     // Load the driver
     Class.forName(
             "sun.jdbc.odbc.JdbcOdbcDriver");
     Connection cn = DriverManager.getConnection(db);
     System.out.println("Database connected.");
     s = cn.createStatement();
     ResultSet rs1=s.executeQuery("select StockName from StockDetail");
      while (rs1.next()){ 
        itemCombo.addItem(rs1.getString("StockName"));
    }
      /*String sl;
     sl = "select StockID from StockDetail where StockName='" + stockno.getText();
      ResultSet rs2 = s.executeQuery(sl);
      while(rs2.next()){
      stockno.setText(rs2.getString("StockID"));
      }*/
      rs1.close();
      s.close();
      cn.close();
 } catch(Exception e) {
     System.out.println(e.toString());
 }
0

There are 0 answers