如何在Java中显示数据库中的所有枚举值

时间:2022-09-25 10:54:22

I have 3 values stored as ENUM in my MySQL database.

我在MySQL数据库中有3个值存储为ENUM。

What I basically want to do is retrieve all this three items and store them in a JComboBox. with the selected enum item from the database as the selected item in the combobox.

我基本上想要做的是检索所有这三个项目并将它们存储在JComboBox中。使用数据库中选定的枚举项作为组合框中的选定项。

At this moment I retrieve just only the current value as string from database and use this method to put all items in the combo box.

此时我只从数据库中检索当前值作为字符串,并使用此方法将所有项目放在组合框中。

private enum statusTypes {Beschikbaar, verhuurd, onderhoud};
txtstatus = new JComboBox();
txtstatus.setModel(new DefaultComboBoxModel(statusTypes.values()));

The way to get the item from the database is like

从数据库中获取项目的方法就像

String s = model.getStatus();

So how can I tell Java to put the value I get as the first value of my combo box?

那么我如何告诉Java将我得到的值作为我的组合框的第一个值?

1 个解决方案

#1


2  

You need to convert your String to the Enum:

您需要将String转换为Enum:

//consider using Java naming convention
private enum StatusTypes {BESCHIKBAAR, VERHUURD, ONDERHOUD};
...
String s = model.getStatus();
...
StatusTypes status = StatusTypes.valueOf(s);

#1


2  

You need to convert your String to the Enum:

您需要将String转换为Enum:

//consider using Java naming convention
private enum StatusTypes {BESCHIKBAAR, VERHUURD, ONDERHOUD};
...
String s = model.getStatus();
...
StatusTypes status = StatusTypes.valueOf(s);