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); } }  static Closeable wrapAsCloseable(final ResultSet resultSet); static boolean setAutoCommit(final Connection connection, final boolean value); static void commit(final Connection connection); static boolean rollback(final Connection connection); static void closeQuietly(java.sql.ResultSet resultSet); static void closeQuietly(Statement statement); static void closeQuietly(Connection connection); static String getDBTypeForClass(final Class<?> valueType); static void setValueToPreparedStatement(int index, final PreparedStatement preparedStatement, Object value); static int setValuesToPreparedStatement(final int startIndex, final PreparedStatement preparedStatement, final Iterable values); @SuppressWarnings("unchecked") static T getValueFromResultSet(int index, final ResultSet resultSet, final Class<T> type); static String sanitizeForTableName(String input);  }
@Test public void testGetValueFromResultSet() throws Exception{ ResultSet resultSet = mock(ResultSet.class); when(resultSet.getInt(1)).thenReturn(Integer.MAX_VALUE); DBUtils.getValueFromResultSet(1, resultSet, Integer.class); verify(resultSet, times(1)).getInt(1); when(resultSet.getLong(2)).thenReturn(Long.MAX_VALUE); DBUtils.getValueFromResultSet(2, resultSet, Long.class); verify(resultSet, times(1)).getLong(2); when(resultSet.getShort(3)).thenReturn(Short.MAX_VALUE); DBUtils.getValueFromResultSet(3, resultSet, Short.class); verify(resultSet, times(1)).getShort(3); when(resultSet.getBoolean(4)).thenReturn(Boolean.FALSE); DBUtils.getValueFromResultSet(4, resultSet, Boolean.class); verify(resultSet, times(1)).getBoolean(4); when(resultSet.getFloat(5)).thenReturn(Float.MAX_VALUE); DBUtils.getValueFromResultSet(5, resultSet, Float.class); verify(resultSet, times(1)).getFloat(5); when(resultSet.getDouble(6)).thenReturn(Double.MAX_VALUE); DBUtils.getValueFromResultSet(6, resultSet, Double.class); verify(resultSet, times(1)).getDouble(6); when(resultSet.getString(7)).thenReturn("7 string"); DBUtils.getValueFromResultSet(7, resultSet, String.class); verify(resultSet, times(1)).getString(7); when(resultSet.getString(8)).thenReturn("8 CharSequence"); DBUtils.getValueFromResultSet(8, resultSet, CharSequence.class); verify(resultSet, times(1)).getString(8); when(resultSet.getBigDecimal(9)).thenReturn(new BigDecimal("1")); DBUtils.getValueFromResultSet(9, resultSet, BigDecimal.class); verify(resultSet, times(1)).getBigDecimal(9); final long now = System.currentTimeMillis(); when(resultSet.getLong(10)).thenReturn(now); DBUtils.getValueFromResultSet(10, resultSet, java.util.Date.class); verify(resultSet, times(1)).getLong(10); when(resultSet.getLong(11)).thenReturn(now); DBUtils.getValueFromResultSet(11, resultSet, java.sql.Date.class); verify(resultSet, times(1)).getLong(11); when(resultSet.getLong(12)).thenReturn(now); DBUtils.getValueFromResultSet(12, resultSet, Time.class); verify(resultSet, times(1)).getLong(12); when(resultSet.getLong(13)).thenReturn(now); DBUtils.getValueFromResultSet(13, resultSet, Timestamp.class); verify(resultSet, times(1)).getLong(13); when(resultSet.getBytes(14)).thenReturn("bytes".getBytes()); DBUtils.getValueFromResultSet(14, resultSet, byte[].class); verify(resultSet, times(1)).getBytes(14); }