如何用mySQL数据库中的信息填充jcombobox? [英] How do I populate a jcombobox with information from a mySQL database?

查看:136
本文介绍了如何用mySQL数据库中的信息填充jcombobox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,该程序获取客户信息并将其转储到数据库中. 为了更改信息,我希望用户能够选择一个客户名称 从组合框中选择,以便系统随后可以从该客户的数据库中调用所有信息.访问数据库很好,可以放入信息并进行更改..我只是想不出如何让组合框填充所有客户名称.

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.

推荐答案

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

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

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

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

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.

如果您认为这占用大量内存,无法立即加载所有客户数据,则可以仅使用名称和ID创建较小的custInfo类.然后在组合框中使用该数据集,并在选定数据后加载其余的客户数据.

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.

这篇关于如何用mySQL数据库中的信息填充jcombobox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆