Java SQL ResultSet Read getNullable(final ResultSet resultSet, final T value)

Here you can find the source of getNullable(final ResultSet resultSet, final T value)

Description

Returns the given value or null if the last column lookup resulted in a null value.

License

Educational Community License

Parameter

Parameter Description
resultSet the row of data which will be checked for null
value the result of the row lookup (e.g. resultSet.getLong("column_name"))
T the type of the provided value

Exception

Parameter Description
SQLException if database error or result set closed

Return

the value or null if the column lookup was null

Declaration

public static <T> T getNullable(final ResultSet resultSet, final T value) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Educational Community License 

import java.sql.ResultSet;

import java.sql.SQLException;

public class Main {
    /**/*from   w  ww.  j  a v a 2  s  . co m*/
     * Returns the given value or null if the last column lookup resulted in a null value.
     * This is a workaround for two things: typecasting the return value of {@link ResultSet#getObject(String)} for getNullable columns
     * <ol>
     * <li>{@link ResultSet#getLong(String)} and other primitive type lookups will turn null into the default values
     * (e.g. null boolean will be false, null long will be 0)</li>
     * <li>Type casting the result of {@link ResultSet#getLong(String)} can result in type cast exceptions if the
     * database driver returns an Integer and you cast to Long.</li>
     * </ol>
     *
     * @param resultSet the row of data which will be checked for null
     * @param value     the result of the row lookup (e.g. resultSet.getLong("column_name"))
     * @param <T>       the type of the provided value
     * @return the value or null if the column lookup was null
     * @throws SQLException if database error or result set closed
     */
    public static <T> T getNullable(final ResultSet resultSet, final T value) throws SQLException {
        return resultSet.wasNull() ? null : value;
    }
}

Related

  1. getLocalDate(ResultSet rs, String columnName)
  2. getMap(ResultSet resultSet)
  3. getMap(ResultSet resultSet)
  4. getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)
  5. getMetaData(ResultSet rs)
  6. getNullableBooleanFromResultSet(ResultSet rset, Enum field)
  7. getNullableByte(ResultSet resultSet, String columnLabel)
  8. getNullableInt(ResultSet resultSet, String column, int fallback)
  9. getNullableLong(ResultSet rs, String columnName)