根据 Combobox 中的选定索引动态更新数据库中的值
Dynamically update values form database against selected index in Combobox
场景是,有组合框和 2 个文本字段。组合框项目(模型)从数据库中获取并动态更新组合框。 我做到了
现在我需要在用户 select Combobox 中的任何项目(模型)时执行此操作,然后应在文本字段中更新其名称和价格。
怎么做?
//此代码只有一个文本字段,我将在后面制作一个。
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);
}
}
您可以引入一个 class 采购,其中包含您将要使用的数据库中的所有必要字段
public class Purchase {
int id;
String name;
String model;
...
public String toString() {
return model;
}
}
从数据库中检索数据时,创建 Purchase 实例来填充结果集中的字段。将购买的物品放在组合框中。要提供正确的显示,您可以添加显示项目模型字段的渲染器(更复杂),或者只是将 Purchase class 的 toString() 方法覆盖到 return 模型字段。
当在组合框中选择 somethinf 时,所选项目是特定的购买实例,您可以使用名称字段反映在文本字段中。
代码将放在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"));
}
场景是,有组合框和 2 个文本字段。组合框项目(模型)从数据库中获取并动态更新组合框。 我做到了
现在我需要在用户 select Combobox 中的任何项目(模型)时执行此操作,然后应在文本字段中更新其名称和价格。
怎么做?
//此代码只有一个文本字段,我将在后面制作一个。
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);
}
}
您可以引入一个 class 采购,其中包含您将要使用的数据库中的所有必要字段
public class Purchase {
int id;
String name;
String model;
...
public String toString() {
return model;
}
}
从数据库中检索数据时,创建 Purchase 实例来填充结果集中的字段。将购买的物品放在组合框中。要提供正确的显示,您可以添加显示项目模型字段的渲染器(更复杂),或者只是将 Purchase class 的 toString() 方法覆盖到 return 模型字段。
当在组合框中选择 somethinf 时,所选项目是特定的购买实例,您可以使用名称字段反映在文本字段中。
代码将放在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"));
}