publicCustomEnumTypeHandler(Class<E> type) { if (type == null) thrownewIllegalArgumentException("Type argument cannot be null"); this.type = type; E[] enums = type.getEnumConstants(); if (enums == null) thrownewIllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); }
@Override publicvoidsetNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)throws SQLException { ps.setInt(i, parameter.getValue()); }
@Override public E getNullableResult(ResultSet rs, String columnName)throws SQLException { inti= rs.getInt(columnName);
if (rs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownewIllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } } }
@Override public E getNullableResult(ResultSet rs, int columnIndex)throws SQLException { inti= rs.getInt(columnIndex); if (rs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownewIllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } } }
@Override public E getNullableResult(CallableStatement cs, int columnIndex)throws SQLException { inti= cs.getInt(columnIndex); if (cs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownewIllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } }