Class SqlNullCheckedResultSet

java.lang.Object
org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSet
All Implemented Interfaces:
InvocationHandler

public class SqlNullCheckedResultSet extends Object implements InvocationHandler
Decorates a ResultSet with checks for a SQL NULL value on each getXXX method. If a column value obtained by a getXXX method is not SQL NULL, the column value is returned. If the column value is SQL null, an alternate value is returned. The alternate value defaults to the Java null value, which can be overridden for instances of the class.

Usage example:

 Connection conn = // somehow get a connection
 Statement stmt = conn.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT col1, col2 FROM table1");

 // Wrap the result set for SQL NULL checking
 SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
 wrapper.setNullString("---N/A---"); // Set null string
 wrapper.setNullInt(-999); // Set null integer
 rs = ProxyFactory.instance().createResultSet(wrapper);

 while (rs.next()) {
     // If col1 is SQL NULL, value returned will be "---N/A---"
     String col1 = rs.getString("col1");
     // If col2 is SQL NULL, value returned will be -999
     int col2 = rs.getInt("col2");
 }
 rs.close();
 
</p>

Unlike some other classes in DbUtils, this class is NOT thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final ProxyFactory
    The factory to create proxies with.
    private static final String
    The getNull string prefix.
    private InputStream
     
    private BigDecimal
     
    private InputStream
     
    private Blob
     
    private boolean
     
    private byte
     
    private byte[]
     
    private Reader
     
    private Clob
     
    private Date
     
    private double
     
    private float
     
    private int
     
    private long
     
    private static final Map<String,Method>
    Maps normal method names (ie.
    private Object
     
    private Ref
     
    private short
     
    private String
     
    private Time
     
    private Timestamp
     
    private URL
     
    private final ResultSet
    The wrapped result.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new instance of SqlNullCheckedResultSet to wrap the specified ResultSet.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value when a SQL null is encountered as the result of invoking a getAsciiStream method.
    Returns the value when a SQL null is encountered as the result of invoking a getBigDecimal method.
    Returns the value when a SQL null is encountered as the result of invoking a getBinaryStream method.
    Returns the value when a SQL null is encountered as the result of invoking a getBlob method.
    boolean
    Returns the value when a SQL null is encountered as the result of invoking a getBoolean method.
    byte
    Returns the value when a SQL null is encountered as the result of invoking a getByte method.
    byte[]
    Returns the value when a SQL null is encountered as the result of invoking a getBytes method.
    Returns the value when a SQL null is encountered as the result of invoking a getCharacterStream method.
    Returns the value when a SQL null is encountered as the result of invoking a getClob method.
    Returns the value when a SQL null is encountered as the result of invoking a getDate method.
    double
    Returns the value when a SQL null is encountered as the result of invoking a getDouble method.
    float
    Returns the value when a SQL null is encountered as the result of invoking a getFloat method.
    int
    Returns the value when a SQL null is encountered as the result of invoking a getInt method.
    long
    Returns the value when a SQL null is encountered as the result of invoking a getLong method.
    Returns the value when a SQL null is encountered as the result of invoking a getObject method.
    Returns the value when a SQL null is encountered as the result of invoking a getRef method.
    short
    Returns the value when a SQL null is encountered as the result of invoking a getShort method.
    Returns the value when a SQL null is encountered as the result of invoking a getString method.
    Returns the value when a SQL null is encountered as the result of invoking a getTime method.
    Returns the value when a SQL null is encountered as the result of invoking a getTimestamp method.
    Returns the value when a SQL null is encountered as the result of invoking a getURL method.
    invoke(Object proxy, Method method, Object[] args)
    Intercepts calls to get* methods and calls the appropriate getNull* method if the ResultSet returned null.
    void
    Sets the value to return when a SQL null is encountered as the result of invoking a getAsciiStream method.
    void
    setNullBigDecimal(BigDecimal nullBigDecimal)
    Sets the value to return when a SQL null is encountered as the result of invoking a getBigDecimal method.
    void
    setNullBinaryStream(InputStream nullBinaryStream)
    Sets the value to return when a SQL null is encountered as the result of invoking a getBinaryStream method.
    void
    setNullBlob(Blob nullBlob)
    Sets the value to return when a SQL null is encountered as the result of invoking a getBlob method.
    void
    setNullBoolean(boolean nullBoolean)
    Sets the value to return when a SQL null is encountered as the result of invoking a getBoolean method.
    void
    setNullByte(byte nullByte)
    Sets the value to return when a SQL null is encountered as the result of invoking a getByte method.
    void
    setNullBytes(byte[] nullBytes)
    Sets the value to return when a SQL null is encountered as the result of invoking a getBytes method.
    void
    setNullCharacterStream(Reader nullCharacterStream)
    Sets the value to return when a SQL null is encountered as the result of invoking a getCharacterStream method.
    void
    setNullClob(Clob nullClob)
    Sets the value to return when a SQL null is encountered as the result of invoking a getClob method.
    void
    setNullDate(Date nullDate)
    Sets the value to return when a SQL null is encountered as the result of invoking a getDate method.
    void
    setNullDouble(double nullDouble)
    Sets the value to return when a SQL null is encountered as the result of invoking a getDouble method.
    void
    setNullFloat(float nullFloat)
    Sets the value to return when a SQL null is encountered as the result of invoking a getFloat method.
    void
    setNullInt(int nullInt)
    Sets the value to return when a SQL null is encountered as the result of invoking a getInt method.
    void
    setNullLong(long nullLong)
    Sets the value to return when a SQL null is encountered as the result of invoking a getLong method.
    void
    setNullObject(Object nullObject)
    Sets the value to return when a SQL null is encountered as the result of invoking a getObject method.
    void
    setNullRef(Ref nullRef)
    Sets the value to return when a SQL null is encountered as the result of invoking a getRef method.
    void
    setNullShort(short nullShort)
    Sets the value to return when a SQL null is encountered as the result of invoking a getShort method.
    void
    setNullString(String nullString)
    Sets the value to return when a SQL null is encountered as the result of invoking a getString method.
    void
    setNullTime(Time nullTime)
    Sets the value to return when a SQL null is encountered as the result of invoking a getTime method.
    void
    setNullTimestamp(Timestamp nullTimestamp)
    Sets the value to return when a SQL null is encountered as the result of invoking a getTimestamp method.
    void
    setNullURL(URL nullURL)
    Sets the value to return when a SQL null is encountered as the result of invoking a getURL method.
    static ResultSet
    Wraps the ResultSet in an instance of this class.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • nullMethods

      private static final Map<String,Method> nullMethods
      Maps normal method names (ie. "getBigDecimal") to the corresponding null Method object (ie. getNullBigDecimal).
    • GET_NULL_PREFIX

      private static final String GET_NULL_PREFIX
      The getNull string prefix.
      Since:
      1.4
      See Also:
    • factory

      private static final ProxyFactory factory
      The factory to create proxies with.
    • nullAsciiStream

      private InputStream nullAsciiStream
    • nullBigDecimal

      private BigDecimal nullBigDecimal
    • nullBinaryStream

      private InputStream nullBinaryStream
    • nullBlob

      private Blob nullBlob
    • nullBoolean

      private boolean nullBoolean
    • nullByte

      private byte nullByte
    • nullBytes

      private byte[] nullBytes
    • nullCharacterStream

      private Reader nullCharacterStream
    • nullClob

      private Clob nullClob
    • nullDate

      private Date nullDate
    • nullDouble

      private double nullDouble
    • nullFloat

      private float nullFloat
    • nullInt

      private int nullInt
    • nullLong

      private long nullLong
    • nullObject

      private Object nullObject
    • nullRef

      private Ref nullRef
    • nullShort

      private short nullShort
    • nullString

      private String nullString
    • nullTime

      private Time nullTime
    • nullTimestamp

      private Timestamp nullTimestamp
    • nullURL

      private URL nullURL
    • rs

      private final ResultSet rs
      The wrapped result.
  • Constructor Details

    • SqlNullCheckedResultSet

      public SqlNullCheckedResultSet(ResultSet rs)
      Constructs a new instance of SqlNullCheckedResultSet to wrap the specified ResultSet.
      Parameters:
      rs - ResultSet to wrap
  • Method Details

    • wrap

      public static ResultSet wrap(ResultSet rs)
      Wraps the ResultSet in an instance of this class. This is equivalent to:
       ProxyFactory.instance().createResultSet(new SqlNullCheckedResultSet(rs));
       
      Parameters:
      rs - The ResultSet to wrap.
      Returns:
      wrapped ResultSet
    • getNullAsciiStream

      public InputStream getNullAsciiStream()
      Returns the value when a SQL null is encountered as the result of invoking a getAsciiStream method.
      Returns:
      the value
    • getNullBigDecimal

      public BigDecimal getNullBigDecimal()
      Returns the value when a SQL null is encountered as the result of invoking a getBigDecimal method.
      Returns:
      the value
    • getNullBinaryStream

      public InputStream getNullBinaryStream()
      Returns the value when a SQL null is encountered as the result of invoking a getBinaryStream method.
      Returns:
      the value
    • getNullBlob

      public Blob getNullBlob()
      Returns the value when a SQL null is encountered as the result of invoking a getBlob method.
      Returns:
      the value
    • getNullBoolean

      public boolean getNullBoolean()
      Returns the value when a SQL null is encountered as the result of invoking a getBoolean method.
      Returns:
      the value
    • getNullByte

      public byte getNullByte()
      Returns the value when a SQL null is encountered as the result of invoking a getByte method.
      Returns:
      the value
    • getNullBytes

      public byte[] getNullBytes()
      Returns the value when a SQL null is encountered as the result of invoking a getBytes method.
      Returns:
      the value
    • getNullCharacterStream

      public Reader getNullCharacterStream()
      Returns the value when a SQL null is encountered as the result of invoking a getCharacterStream method.
      Returns:
      the value
    • getNullClob

      public Clob getNullClob()
      Returns the value when a SQL null is encountered as the result of invoking a getClob method.
      Returns:
      the value
    • getNullDate

      public Date getNullDate()
      Returns the value when a SQL null is encountered as the result of invoking a getDate method.
      Returns:
      the value
    • getNullDouble

      public double getNullDouble()
      Returns the value when a SQL null is encountered as the result of invoking a getDouble method.
      Returns:
      the value
    • getNullFloat

      public float getNullFloat()
      Returns the value when a SQL null is encountered as the result of invoking a getFloat method.
      Returns:
      the value
    • getNullInt

      public int getNullInt()
      Returns the value when a SQL null is encountered as the result of invoking a getInt method.
      Returns:
      the value
    • getNullLong

      public long getNullLong()
      Returns the value when a SQL null is encountered as the result of invoking a getLong method.
      Returns:
      the value
    • getNullObject

      public Object getNullObject()
      Returns the value when a SQL null is encountered as the result of invoking a getObject method.
      Returns:
      the value
    • getNullRef

      public Ref getNullRef()
      Returns the value when a SQL null is encountered as the result of invoking a getRef method.
      Returns:
      the value
    • getNullShort

      public short getNullShort()
      Returns the value when a SQL null is encountered as the result of invoking a getShort method.
      Returns:
      the value
    • getNullString

      public String getNullString()
      Returns the value when a SQL null is encountered as the result of invoking a getString method.
      Returns:
      the value
    • getNullTime

      public Time getNullTime()
      Returns the value when a SQL null is encountered as the result of invoking a getTime method.
      Returns:
      the value
    • getNullTimestamp

      public Timestamp getNullTimestamp()
      Returns the value when a SQL null is encountered as the result of invoking a getTimestamp method.
      Returns:
      the value
    • getNullURL

      public URL getNullURL()
      Returns the value when a SQL null is encountered as the result of invoking a getURL method.
      Returns:
      the value
    • invoke

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      Intercepts calls to get* methods and calls the appropriate getNull* method if the ResultSet returned null.
      Specified by:
      invoke in interface InvocationHandler
      Parameters:
      proxy - Not used; all method calls go to the internal result set
      method - The method to invoke on the result set
      args - The arguments to pass to the result set
      Returns:
      null checked result
      Throws:
      Throwable - error
      See Also:
    • setNullAsciiStream

      public void setNullAsciiStream(InputStream nullAsciiStream)
      Sets the value to return when a SQL null is encountered as the result of invoking a getAsciiStream method.
      Parameters:
      nullAsciiStream - the value
    • setNullBigDecimal

      public void setNullBigDecimal(BigDecimal nullBigDecimal)
      Sets the value to return when a SQL null is encountered as the result of invoking a getBigDecimal method.
      Parameters:
      nullBigDecimal - the value
    • setNullBinaryStream

      public void setNullBinaryStream(InputStream nullBinaryStream)
      Sets the value to return when a SQL null is encountered as the result of invoking a getBinaryStream method.
      Parameters:
      nullBinaryStream - the value
    • setNullBlob

      public void setNullBlob(Blob nullBlob)
      Sets the value to return when a SQL null is encountered as the result of invoking a getBlob method.
      Parameters:
      nullBlob - the value
    • setNullBoolean

      public void setNullBoolean(boolean nullBoolean)
      Sets the value to return when a SQL null is encountered as the result of invoking a getBoolean method.
      Parameters:
      nullBoolean - the value
    • setNullByte

      public void setNullByte(byte nullByte)
      Sets the value to return when a SQL null is encountered as the result of invoking a getByte method.
      Parameters:
      nullByte - the value
    • setNullBytes

      public void setNullBytes(byte[] nullBytes)
      Sets the value to return when a SQL null is encountered as the result of invoking a getBytes method.
      Parameters:
      nullBytes - the value
    • setNullCharacterStream

      public void setNullCharacterStream(Reader nullCharacterStream)
      Sets the value to return when a SQL null is encountered as the result of invoking a getCharacterStream method.
      Parameters:
      nullCharacterStream - the value
    • setNullClob

      public void setNullClob(Clob nullClob)
      Sets the value to return when a SQL null is encountered as the result of invoking a getClob method.
      Parameters:
      nullClob - the value
    • setNullDate

      public void setNullDate(Date nullDate)
      Sets the value to return when a SQL null is encountered as the result of invoking a getDate method.
      Parameters:
      nullDate - the value
    • setNullDouble

      public void setNullDouble(double nullDouble)
      Sets the value to return when a SQL null is encountered as the result of invoking a getDouble method.
      Parameters:
      nullDouble - the value
    • setNullFloat

      public void setNullFloat(float nullFloat)
      Sets the value to return when a SQL null is encountered as the result of invoking a getFloat method.
      Parameters:
      nullFloat - the value
    • setNullInt

      public void setNullInt(int nullInt)
      Sets the value to return when a SQL null is encountered as the result of invoking a getInt method.
      Parameters:
      nullInt - the value
    • setNullLong

      public void setNullLong(long nullLong)
      Sets the value to return when a SQL null is encountered as the result of invoking a getLong method.
      Parameters:
      nullLong - the value
    • setNullObject

      public void setNullObject(Object nullObject)
      Sets the value to return when a SQL null is encountered as the result of invoking a getObject method.
      Parameters:
      nullObject - the value
    • setNullRef

      public void setNullRef(Ref nullRef)
      Sets the value to return when a SQL null is encountered as the result of invoking a getRef method.
      Parameters:
      nullRef - the value
    • setNullShort

      public void setNullShort(short nullShort)
      Sets the value to return when a SQL null is encountered as the result of invoking a getShort method.
      Parameters:
      nullShort - the value
    • setNullString

      public void setNullString(String nullString)
      Sets the value to return when a SQL null is encountered as the result of invoking a getString method.
      Parameters:
      nullString - the value
    • setNullTime

      public void setNullTime(Time nullTime)
      Sets the value to return when a SQL null is encountered as the result of invoking a getTime method.
      Parameters:
      nullTime - the value
    • setNullTimestamp

      public void setNullTimestamp(Timestamp nullTimestamp)
      Sets the value to return when a SQL null is encountered as the result of invoking a getTimestamp method.
      Parameters:
      nullTimestamp - the value
    • setNullURL

      public void setNullURL(URL nullURL)
      Sets the value to return when a SQL null is encountered as the result of invoking a getURL method.
      Parameters:
      nullURL - the value