如何使用mySQL数据库中的信息填充jcombobox?

时间:2021-09-07 07:25:54

Basically the program takes customer information and dumps it in a database. In order to change information I want the user to be able to pick a customer name from a combobox, so the system can then call all the info out of the database on that customer. Accessing the database is fine, putting info in and changing it is fine.. I just cant figure out how to get the combobox to populate with all the customer names.

程序基本上会获取客户信息并将其转储到数据库中。为了更改信息,我希望用户能够从组合框中选择客户名称,这样系统就可以调用该客户的数据库中的所有信息。访问数据库很好,将信息放入并更改它很好..我只是想弄清楚如何让组合框填充所有客户名称。

2 个解决方案

#1


1  

The simplest solution is to create an array of String from the database. Then use that to create the combobox.

最简单的解决方案是从数据库中创建一个String数组。然后用它来创建组合框。

  String[] mydbStrings = .....;
  JComboBox mycombo = new JComboBox(mydbStrings);

But that will give you only some strings. It may be better to define a class that represents the customer, load the customers from the database, ensure that an adequate toString() is defined in the Customer class and create an array of Customers that is used in the Combo Box. That way, the customer names are displayed in the combobox but hwen you select one you have all the customer details readily available.

但那只会给你一些字符串。最好定义一个代表客户的类,从数据库加载客户,确保在Customer类中定义足够的toString()并创建在组合框中使用的Customers数组。这样,客户名称将显示在组合框中,但是如果您选择了一个客户名称,那么您可以随时获得所有客户详细信息。

If you think this is too memory intensive to load all your customer's data at once it is possible to create a smaller custInfo class with just the name and ID. Then use that in the combobox and load the rest of the customer data after it is selected.

如果您认为这太耗费内存以至于无法立即加载所有客户的数据,则可以使用名称和ID创建较小的custInfo类。然后在组合框中使用它,并在选择后加载其余的客户数据。

#2


1  

This should be simple if you know how to populate a normal combobox, and how to pull the data from the database. The combobox by default will take an array of strings in the constructor, so you could just pass an array of string(Customer names), but I would recommend creating a model for the combobox. You could then pass the Customers to the model(most likely some POJO's), and then you will also probably want to create a renderer so that you can display the names as you like. Then when a customer is selected you can query the model to get an id for the selected customer, and then use that to retrieve the needed customer data.

如果您知道如何填充常规组合框以及如何从数据库中提取数据,这应该很简单。默认情况下,组合框将在构造函数中使用一个字符串数组,因此您只需传递一个字符串数组(客户名称),但我建议为组合框创建一个模型。然后,您可以将Customers传递给模型(很可能是某些POJO),然后您可能还想创建一个渲染器,以便您可以根据需要显示名称。然后,当选择客户时,您可以查询模型以获取所选客户的ID,然后使用该ID来检索所需的客户数据。

#1


1  

The simplest solution is to create an array of String from the database. Then use that to create the combobox.

最简单的解决方案是从数据库中创建一个String数组。然后用它来创建组合框。

  String[] mydbStrings = .....;
  JComboBox mycombo = new JComboBox(mydbStrings);

But that will give you only some strings. It may be better to define a class that represents the customer, load the customers from the database, ensure that an adequate toString() is defined in the Customer class and create an array of Customers that is used in the Combo Box. That way, the customer names are displayed in the combobox but hwen you select one you have all the customer details readily available.

但那只会给你一些字符串。最好定义一个代表客户的类,从数据库加载客户,确保在Customer类中定义足够的toString()并创建在组合框中使用的Customers数组。这样,客户名称将显示在组合框中,但是如果您选择了一个客户名称,那么您可以随时获得所有客户详细信息。

If you think this is too memory intensive to load all your customer's data at once it is possible to create a smaller custInfo class with just the name and ID. Then use that in the combobox and load the rest of the customer data after it is selected.

如果您认为这太耗费内存以至于无法立即加载所有客户的数据,则可以使用名称和ID创建较小的custInfo类。然后在组合框中使用它,并在选择后加载其余的客户数据。

#2


1  

This should be simple if you know how to populate a normal combobox, and how to pull the data from the database. The combobox by default will take an array of strings in the constructor, so you could just pass an array of string(Customer names), but I would recommend creating a model for the combobox. You could then pass the Customers to the model(most likely some POJO's), and then you will also probably want to create a renderer so that you can display the names as you like. Then when a customer is selected you can query the model to get an id for the selected customer, and then use that to retrieve the needed customer data.

如果您知道如何填充常规组合框以及如何从数据库中提取数据,这应该很简单。默认情况下,组合框将在构造函数中使用一个字符串数组,因此您只需传递一个字符串数组(客户名称),但我建议为组合框创建一个模型。然后,您可以将Customers传递给模型(很可能是某些POJO),然后您可能还想创建一个渲染器,以便您可以根据需要显示名称。然后,当选择客户时,您可以查询模型以获取所选客户的ID,然后使用该ID来检索所需的客户数据。