MySQL has its own ENUM type . And as @Jigar pointed out, you can use Enum.getName to get a Java String representing an enumeration to save, get a field from the database as a string, and use Enum.valueOf to load the enum constant back.
You can also use Enum.ordinal() + 1 to get an integer to save (MySQL also supports integer indexes directly with the ENUM type) and issues a query like:
SELECT enum_col-1 FROM tbl_name;
to get the inverse ordinal (it will retrieve -1 for strings with invalid enum values).
Plus MyEnumType value = MyEnumType.values()[ordinal] to convert it back to a Java enumeration type.
In any case, I think the String-based solution is cleaner and more elegant, as well as safer in the long run (if you ever need to reorder java Enum or add new enum values in the middle of existing ones, you will thank yourself for going to start with a String strategy).
source share