Are enums supported by JDBC?

后端 未结 2 527
情歌与酒
情歌与酒 2021-02-11 20:14

I really can\'t find a nice enum JDBC mapping example. Is enum actually supported by JDBC?

I am working with MySQL. I have an enum column, and would like to map to some

2条回答
  •  不知归路
    2021-02-11 21:11

    JDBC does not support enums.

    You can convert a string to an enum though, so if you have a Java enum you can do something like

     MyEnum enumVal =  MyEnum.valueOf(rs.getString("EnumColumn"));
    

    You'll have to keep your java enum and mysql enum in sync though. MyEnum.valueOf() can throw IllegalArgumentException if there's no mapping from the string, or NullPointerException if you get a null value from the db.

提交回复
热议问题