根据组合框中选定的索引动态更新数据库中的值

jucafojl  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(408)

场景是,有一个组合框和两个文本字段。组合框项(模型)从数据库中获取并动态更新组合框。我已经做到了。
现在我需要这样做,当用户从combobox中选择任何项目(模型)时,它的名称和价格应该在textfields中更新。
怎么做?
//这个代码只有一个textfield,我会在后面做一个。

public class Purchases extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    JComboBox comboBox = new JComboBox();
     String model, name;

    /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Purchases frame = new Purchases();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });     
        }

        /**
         * Create the frame.
         */

        public Purchases() {
            try {
        String host;
        host = "jdbc:mysql://localhost:3306/sfs_electronics";// [root on Default schema]";
        String uName = "root";
        String uPass= "";

Connection con = DriverManager.getConnection( host, uName, uPass );
    Statement stmt = con.createStatement( );
    String SQL = "SELECT * FROM purchases";
    ResultSet rs = stmt.executeQuery( SQL );
    while(rs.next())
         {
         model= rs.getString("Model");
         name= rs.getString("Name");
         comboBox.addItem(model);    // Adding Items in ComboBox
        System.out.println(model);
         }
       }
        catch(SQLException e){
            System.out.println(e);
        }

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 501, 420);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10, 10, 465, 146);
        contentPane.add(panel);
        panel.setLayout(null);
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int n=comboBox.getSelectedIndex();
                System.out.println(n);
                System.out.println(comboBox.getSelectedItem());
                textField.setText(?????);  //Here What i need to code that selected models name should be place here. 
            }
        });

        comboBox.setBounds(109, 11, 86, 20);
        panel.add(comboBox);        
        textField = new JTextField();
        textField.setBounds(109, 47, 86, 20);
        panel.add(textField);
        textField.setColumns(10);                   
    }

}
x4shl7ld

x4shl7ld1#

您可以引入一个类购买,其中包含将要使用的db中的所有必需字段

public class Purchase {
  int id;
  String name;
  String model;
...
  public String toString() {
      return model;
  }
}

从db中检索数据时,创建purchase示例,填充结果集中的字段。在组合框中放置购买项目。为了提供正确的显示,您可以添加显示项目模型字段的呈现器(更复杂),或者只重写purchase类的tostring()方法以返回模型字段。
当在组合框中选择somethinf时,所选项目是特定的采购示例,您可以使用name字段反映在文本字段中。

falq053o

falq053o2#

代码将放置在 comboBox.addActionListener .

//Connect database 
String s = comboBox.getSelectedItem().toString();
String SQL = "SELECT * FROM `products` WHERE `Model` = '" + s + "'"; 
ResultSet rs = stmt.executeQuery( SQL );
while(rs.next())
{
    requiredtextfield.setText(rs.getString("ColumnNAme_of_database"));
}

相关问题