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

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

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

  1. public class Purchases extends JFrame {
  2. private JPanel contentPane;
  3. private JTextField textField;
  4. JComboBox comboBox = new JComboBox();
  5. String model, name;
  6. /**
  7. * Launch the application.
  8. */
  9. public static void main(String[] args) {
  10. EventQueue.invokeLater(new Runnable() {
  11. public void run() {
  12. try {
  13. Purchases frame = new Purchases();
  14. frame.setVisible(true);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. });
  20. }
  21. /**
  22. * Create the frame.
  23. */
  24. public Purchases() {
  25. try {
  26. String host;
  27. host = "jdbc:mysql://localhost:3306/sfs_electronics";// [root on Default schema]";
  28. String uName = "root";
  29. String uPass= "";
  30. Connection con = DriverManager.getConnection( host, uName, uPass );
  31. Statement stmt = con.createStatement( );
  32. String SQL = "SELECT * FROM purchases";
  33. ResultSet rs = stmt.executeQuery( SQL );
  34. while(rs.next())
  35. {
  36. model= rs.getString("Model");
  37. name= rs.getString("Name");
  38. comboBox.addItem(model); // Adding Items in ComboBox
  39. System.out.println(model);
  40. }
  41. }
  42. catch(SQLException e){
  43. System.out.println(e);
  44. }
  45. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. setBounds(100, 100, 501, 420);
  47. contentPane = new JPanel();
  48. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  49. setContentPane(contentPane);
  50. contentPane.setLayout(null);
  51. JPanel panel = new JPanel();
  52. panel.setBounds(10, 10, 465, 146);
  53. contentPane.add(panel);
  54. panel.setLayout(null);
  55. comboBox.addActionListener(new ActionListener() {
  56. public void actionPerformed(ActionEvent arg0) {
  57. int n=comboBox.getSelectedIndex();
  58. System.out.println(n);
  59. System.out.println(comboBox.getSelectedItem());
  60. textField.setText(?????); //Here What i need to code that selected models name should be place here.
  61. }
  62. });
  63. comboBox.setBounds(109, 11, 86, 20);
  64. panel.add(comboBox);
  65. textField = new JTextField();
  66. textField.setBounds(109, 47, 86, 20);
  67. panel.add(textField);
  68. textField.setColumns(10);
  69. }
  70. }
x4shl7ld

x4shl7ld1#

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

  1. public class Purchase {
  2. int id;
  3. String name;
  4. String model;
  5. ...
  6. public String toString() {
  7. return model;
  8. }
  9. }

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

falq053o

falq053o2#

代码将放置在 comboBox.addActionListener .

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

相关问题