使用常规语句(只是语句)我可以将 java 枚举放入查询中并且它工作得很好。使用准备好的语句我不能这样做吗?
最佳答案
MySQL 将其枚举类型视为查询的字符串。所以你应该能够使用 PreparedStatement.setString()
方法并将枚举名称传递给它:
preparedStatement.setString(1, MY_ENUM.name());
当然,这假设您的 java 和 MySQL 枚举的名称匹配。
注意 name()
被选择而不是 toString()
作为,根据文档:
name() This method is designed primarily for use in specialized situations where correctness depends on getting the exact name, which will not vary from release to release.
关于准备好的语句中的 Java 枚举到 mysql 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639269/