class DBUtils {

    @SuppressWarnings("unchecked")
    public static <T>T getValueFromResultSet(int index, final ResultSet resultSet, final Class<T> type){

        try {
            if (java.sql.Date.class.isAssignableFrom(type)) {
                final long time = resultSet.getLong(index);
                return (T)new java.sql.Date(time);

            } else if (Time.class.isAssignableFrom(type)) {
                final long time = resultSet.getLong(index);
                return (T)new java.sql.Time(time);

            } else if (Timestamp.class.isAssignableFrom(type)) {
                final long time = resultSet.getLong(index);
                return (T)new java.sql.Timestamp(time);

            }else if (Date.class.isAssignableFrom(type)) {
                final long time = resultSet.getLong(index);
                return (T)new Date(time);

            } else if (Long.class.isAssignableFrom(type)) {
                return (T) Long.valueOf(resultSet.getLong(index));

            } else if (Integer.class.isAssignableFrom(type)) {
                return (T) Integer.valueOf(resultSet.getInt(index));

            } else if (Short.class.isAssignableFrom(type)) {
                return (T) Short.valueOf(resultSet.getShort(index));

            } else if (Float.class.isAssignableFrom(type)) {
                return (T) Float.valueOf(resultSet.getFloat(index));

            } else if (Double.class.isAssignableFrom(type)) {
                return (T) Double.valueOf(resultSet.getDouble(index));

            } else if (Boolean.class.isAssignableFrom(type)) {
                return (T) Boolean.valueOf(resultSet.getBoolean(index));

            } else if (BigDecimal.class.isAssignableFrom(type)) {
                return (T) resultSet.getBigDecimal(index);

            } else if (CharSequence.class.isAssignableFrom(type)) {
                return (T) resultSet.getString(index);

            } else if (byte[].class.isAssignableFrom(type)) {
                return (T) resultSet.getBytes(index);

            } else {
                throw new IllegalStateException("Type " + type + " not supported.");
            }
        }catch (Exception e){
            throw new IllegalStateException("Unable to read the value from the resultSet. Index:" + index + ", type: " + type, e);
        }
    }

}