Here you can find the source of getNullable(final ResultSet resultSet, final T value)
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 |
Parameter | Description |
---|---|
SQLException | if database error or result set closed |
public static <T> T getNullable(final ResultSet resultSet, final T value) throws SQLException
//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; } }