Class SqlNullCheckedResultSet
- java.lang.Object
-
- org.apache.commons.dbutils.wrappers.SqlNullCheckedResultSet
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
public class SqlNullCheckedResultSet extends java.lang.Object implements java.lang.reflect.InvocationHandlerDecorates aResultSetwith checks for a SQL NULL value on eachgetXXXmethod. If a column value obtained by agetXXXmethod 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 Javanullvalue, which can be overridden for instances of the class.Usage example:
</p>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();Unlike some other classes in DbUtils, this class is NOT thread-safe.
-
-
Field Summary
Fields Modifier and Type Field Description private static ProxyFactoryfactoryThe factory to create proxies with.private static java.lang.StringGET_NULL_PREFIXThegetNullstring prefix.private java.io.InputStreamnullAsciiStreamprivate java.math.BigDecimalnullBigDecimalprivate java.io.InputStreamnullBinaryStreamprivate java.sql.BlobnullBlobprivate booleannullBooleanprivate bytenullByteprivate byte[]nullBytesprivate java.io.ReadernullCharacterStreamprivate java.sql.ClobnullClobprivate java.sql.DatenullDateprivate doublenullDoubleprivate floatnullFloatprivate intnullIntprivate longnullLongprivate static java.util.Map<java.lang.String,java.lang.reflect.Method>nullMethodsMaps normal method names (ie.private java.lang.ObjectnullObjectprivate java.sql.RefnullRefprivate shortnullShortprivate java.lang.StringnullStringprivate java.sql.TimenullTimeprivate java.sql.TimestampnullTimestampprivate java.net.URLnullURLprivate java.sql.ResultSetrsThe wrapped result.
-
Constructor Summary
Constructors Constructor Description SqlNullCheckedResultSet(java.sql.ResultSet rs)Constructs a new instance ofSqlNullCheckedResultSetto wrap the specifiedResultSet.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.InputStreamgetNullAsciiStream()Returns the value when a SQL null is encountered as the result of invoking agetAsciiStreammethod.java.math.BigDecimalgetNullBigDecimal()Returns the value when a SQL null is encountered as the result of invoking agetBigDecimalmethod.java.io.InputStreamgetNullBinaryStream()Returns the value when a SQL null is encountered as the result of invoking agetBinaryStreammethod.java.sql.BlobgetNullBlob()Returns the value when a SQL null is encountered as the result of invoking agetBlobmethod.booleangetNullBoolean()Returns the value when a SQL null is encountered as the result of invoking agetBooleanmethod.bytegetNullByte()Returns the value when a SQL null is encountered as the result of invoking agetBytemethod.byte[]getNullBytes()Returns the value when a SQL null is encountered as the result of invoking agetBytesmethod.java.io.ReadergetNullCharacterStream()Returns the value when a SQL null is encountered as the result of invoking agetCharacterStreammethod.java.sql.ClobgetNullClob()Returns the value when a SQL null is encountered as the result of invoking agetClobmethod.java.sql.DategetNullDate()Returns the value when a SQL null is encountered as the result of invoking agetDatemethod.doublegetNullDouble()Returns the value when a SQL null is encountered as the result of invoking agetDoublemethod.floatgetNullFloat()Returns the value when a SQL null is encountered as the result of invoking agetFloatmethod.intgetNullInt()Returns the value when a SQL null is encountered as the result of invoking agetIntmethod.longgetNullLong()Returns the value when a SQL null is encountered as the result of invoking agetLongmethod.java.lang.ObjectgetNullObject()Returns the value when a SQL null is encountered as the result of invoking agetObjectmethod.java.sql.RefgetNullRef()Returns the value when a SQL null is encountered as the result of invoking agetRefmethod.shortgetNullShort()Returns the value when a SQL null is encountered as the result of invoking agetShortmethod.java.lang.StringgetNullString()Returns the value when a SQL null is encountered as the result of invoking agetStringmethod.java.sql.TimegetNullTime()Returns the value when a SQL null is encountered as the result of invoking agetTimemethod.java.sql.TimestampgetNullTimestamp()Returns the value when a SQL null is encountered as the result of invoking agetTimestampmethod.java.net.URLgetNullURL()Returns the value when a SQL null is encountered as the result of invoking agetURLmethod.java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)Intercepts calls toget*methods and calls the appropriategetNull*method if theResultSetreturnednull.voidsetNullAsciiStream(java.io.InputStream nullAsciiStream)Sets the value to return when a SQL null is encountered as the result of invoking agetAsciiStreammethod.voidsetNullBigDecimal(java.math.BigDecimal nullBigDecimal)Sets the value to return when a SQL null is encountered as the result of invoking agetBigDecimalmethod.voidsetNullBinaryStream(java.io.InputStream nullBinaryStream)Sets the value to return when a SQL null is encountered as the result of invoking agetBinaryStreammethod.voidsetNullBlob(java.sql.Blob nullBlob)Sets the value to return when a SQL null is encountered as the result of invoking agetBlobmethod.voidsetNullBoolean(boolean nullBoolean)Sets the value to return when a SQL null is encountered as the result of invoking agetBooleanmethod.voidsetNullByte(byte nullByte)Sets the value to return when a SQL null is encountered as the result of invoking agetBytemethod.voidsetNullBytes(byte[] nullBytes)Sets the value to return when a SQL null is encountered as the result of invoking agetBytesmethod.voidsetNullCharacterStream(java.io.Reader nullCharacterStream)Sets the value to return when a SQL null is encountered as the result of invoking agetCharacterStreammethod.voidsetNullClob(java.sql.Clob nullClob)Sets the value to return when a SQL null is encountered as the result of invoking agetClobmethod.voidsetNullDate(java.sql.Date nullDate)Sets the value to return when a SQL null is encountered as the result of invoking agetDatemethod.voidsetNullDouble(double nullDouble)Sets the value to return when a SQL null is encountered as the result of invoking agetDoublemethod.voidsetNullFloat(float nullFloat)Sets the value to return when a SQL null is encountered as the result of invoking agetFloatmethod.voidsetNullInt(int nullInt)Sets the value to return when a SQL null is encountered as the result of invoking agetIntmethod.voidsetNullLong(long nullLong)Sets the value to return when a SQL null is encountered as the result of invoking agetLongmethod.voidsetNullObject(java.lang.Object nullObject)Sets the value to return when a SQL null is encountered as the result of invoking agetObjectmethod.voidsetNullRef(java.sql.Ref nullRef)Sets the value to return when a SQL null is encountered as the result of invoking agetRefmethod.voidsetNullShort(short nullShort)Sets the value to return when a SQL null is encountered as the result of invoking agetShortmethod.voidsetNullString(java.lang.String nullString)Sets the value to return when a SQL null is encountered as the result of invoking agetStringmethod.voidsetNullTime(java.sql.Time nullTime)Sets the value to return when a SQL null is encountered as the result of invoking agetTimemethod.voidsetNullTimestamp(java.sql.Timestamp nullTimestamp)Sets the value to return when a SQL null is encountered as the result of invoking agetTimestampmethod.voidsetNullURL(java.net.URL nullURL)Sets the value to return when a SQL null is encountered as the result of invoking agetURLmethod.static java.sql.ResultSetwrap(java.sql.ResultSet rs)Wraps theResultSetin an instance of this class.
-
-
-
Field Detail
-
nullMethods
private static final java.util.Map<java.lang.String,java.lang.reflect.Method> nullMethods
Maps normal method names (ie. "getBigDecimal") to the corresponding null Method object (ie. getNullBigDecimal).
-
GET_NULL_PREFIX
private static final java.lang.String GET_NULL_PREFIX
ThegetNullstring prefix.- Since:
- 1.4
- See Also:
- Constant Field Values
-
factory
private static final ProxyFactory factory
The factory to create proxies with.
-
nullAsciiStream
private java.io.InputStream nullAsciiStream
-
nullBigDecimal
private java.math.BigDecimal nullBigDecimal
-
nullBinaryStream
private java.io.InputStream nullBinaryStream
-
nullBlob
private java.sql.Blob nullBlob
-
nullBoolean
private boolean nullBoolean
-
nullByte
private byte nullByte
-
nullBytes
private byte[] nullBytes
-
nullCharacterStream
private java.io.Reader nullCharacterStream
-
nullClob
private java.sql.Clob nullClob
-
nullDate
private java.sql.Date nullDate
-
nullDouble
private double nullDouble
-
nullFloat
private float nullFloat
-
nullInt
private int nullInt
-
nullLong
private long nullLong
-
nullObject
private java.lang.Object nullObject
-
nullRef
private java.sql.Ref nullRef
-
nullShort
private short nullShort
-
nullString
private java.lang.String nullString
-
nullTime
private java.sql.Time nullTime
-
nullTimestamp
private java.sql.Timestamp nullTimestamp
-
nullURL
private java.net.URL nullURL
-
rs
private final java.sql.ResultSet rs
The wrapped result.
-
-
Method Detail
-
wrap
public static java.sql.ResultSet wrap(java.sql.ResultSet rs)
Wraps theResultSetin an instance of this class. This is equivalent to:ProxyFactory.instance().createResultSet(new SqlNullCheckedResultSet(rs));
- Parameters:
rs- TheResultSetto wrap.- Returns:
- wrapped ResultSet
-
getNullAsciiStream
public java.io.InputStream getNullAsciiStream()
Returns the value when a SQL null is encountered as the result of invoking agetAsciiStreammethod.- Returns:
- the value
-
getNullBigDecimal
public java.math.BigDecimal getNullBigDecimal()
Returns the value when a SQL null is encountered as the result of invoking agetBigDecimalmethod.- Returns:
- the value
-
getNullBinaryStream
public java.io.InputStream getNullBinaryStream()
Returns the value when a SQL null is encountered as the result of invoking agetBinaryStreammethod.- Returns:
- the value
-
getNullBlob
public java.sql.Blob getNullBlob()
Returns the value when a SQL null is encountered as the result of invoking agetBlobmethod.- Returns:
- the value
-
getNullBoolean
public boolean getNullBoolean()
Returns the value when a SQL null is encountered as the result of invoking agetBooleanmethod.- Returns:
- the value
-
getNullByte
public byte getNullByte()
Returns the value when a SQL null is encountered as the result of invoking agetBytemethod.- Returns:
- the value
-
getNullBytes
public byte[] getNullBytes()
Returns the value when a SQL null is encountered as the result of invoking agetBytesmethod.- Returns:
- the value
-
getNullCharacterStream
public java.io.Reader getNullCharacterStream()
Returns the value when a SQL null is encountered as the result of invoking agetCharacterStreammethod.- Returns:
- the value
-
getNullClob
public java.sql.Clob getNullClob()
Returns the value when a SQL null is encountered as the result of invoking agetClobmethod.- Returns:
- the value
-
getNullDate
public java.sql.Date getNullDate()
Returns the value when a SQL null is encountered as the result of invoking agetDatemethod.- Returns:
- the value
-
getNullDouble
public double getNullDouble()
Returns the value when a SQL null is encountered as the result of invoking agetDoublemethod.- Returns:
- the value
-
getNullFloat
public float getNullFloat()
Returns the value when a SQL null is encountered as the result of invoking agetFloatmethod.- Returns:
- the value
-
getNullInt
public int getNullInt()
Returns the value when a SQL null is encountered as the result of invoking agetIntmethod.- Returns:
- the value
-
getNullLong
public long getNullLong()
Returns the value when a SQL null is encountered as the result of invoking agetLongmethod.- Returns:
- the value
-
getNullObject
public java.lang.Object getNullObject()
Returns the value when a SQL null is encountered as the result of invoking agetObjectmethod.- Returns:
- the value
-
getNullRef
public java.sql.Ref getNullRef()
Returns the value when a SQL null is encountered as the result of invoking agetRefmethod.- Returns:
- the value
-
getNullShort
public short getNullShort()
Returns the value when a SQL null is encountered as the result of invoking agetShortmethod.- Returns:
- the value
-
getNullString
public java.lang.String getNullString()
Returns the value when a SQL null is encountered as the result of invoking agetStringmethod.- Returns:
- the value
-
getNullTime
public java.sql.Time getNullTime()
Returns the value when a SQL null is encountered as the result of invoking agetTimemethod.- Returns:
- the value
-
getNullTimestamp
public java.sql.Timestamp getNullTimestamp()
Returns the value when a SQL null is encountered as the result of invoking agetTimestampmethod.- Returns:
- the value
-
getNullURL
public java.net.URL getNullURL()
Returns the value when a SQL null is encountered as the result of invoking agetURLmethod.- Returns:
- the value
-
invoke
public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.ThrowableIntercepts calls toget*methods and calls the appropriategetNull*method if theResultSetreturnednull.- Specified by:
invokein interfacejava.lang.reflect.InvocationHandler- Parameters:
proxy- Not used; all method calls go to the internal result setmethod- The method to invoke on the result setargs- The arguments to pass to the result set- Returns:
- null checked result
- Throws:
java.lang.Throwable- error- See Also:
InvocationHandler.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
-
setNullAsciiStream
public void setNullAsciiStream(java.io.InputStream nullAsciiStream)
Sets the value to return when a SQL null is encountered as the result of invoking agetAsciiStreammethod.- Parameters:
nullAsciiStream- the value
-
setNullBigDecimal
public void setNullBigDecimal(java.math.BigDecimal nullBigDecimal)
Sets the value to return when a SQL null is encountered as the result of invoking agetBigDecimalmethod.- Parameters:
nullBigDecimal- the value
-
setNullBinaryStream
public void setNullBinaryStream(java.io.InputStream nullBinaryStream)
Sets the value to return when a SQL null is encountered as the result of invoking agetBinaryStreammethod.- Parameters:
nullBinaryStream- the value
-
setNullBlob
public void setNullBlob(java.sql.Blob nullBlob)
Sets the value to return when a SQL null is encountered as the result of invoking agetBlobmethod.- 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 agetBooleanmethod.- 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 agetBytemethod.- 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 agetBytesmethod.- Parameters:
nullBytes- the value
-
setNullCharacterStream
public void setNullCharacterStream(java.io.Reader nullCharacterStream)
Sets the value to return when a SQL null is encountered as the result of invoking agetCharacterStreammethod.- Parameters:
nullCharacterStream- the value
-
setNullClob
public void setNullClob(java.sql.Clob nullClob)
Sets the value to return when a SQL null is encountered as the result of invoking agetClobmethod.- Parameters:
nullClob- the value
-
setNullDate
public void setNullDate(java.sql.Date nullDate)
Sets the value to return when a SQL null is encountered as the result of invoking agetDatemethod.- 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 agetDoublemethod.- 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 agetFloatmethod.- 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 agetIntmethod.- 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 agetLongmethod.- Parameters:
nullLong- the value
-
setNullObject
public void setNullObject(java.lang.Object nullObject)
Sets the value to return when a SQL null is encountered as the result of invoking agetObjectmethod.- Parameters:
nullObject- the value
-
setNullRef
public void setNullRef(java.sql.Ref nullRef)
Sets the value to return when a SQL null is encountered as the result of invoking agetRefmethod.- 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 agetShortmethod.- Parameters:
nullShort- the value
-
setNullString
public void setNullString(java.lang.String nullString)
Sets the value to return when a SQL null is encountered as the result of invoking agetStringmethod.- Parameters:
nullString- the value
-
setNullTime
public void setNullTime(java.sql.Time nullTime)
Sets the value to return when a SQL null is encountered as the result of invoking agetTimemethod.- Parameters:
nullTime- the value
-
setNullTimestamp
public void setNullTimestamp(java.sql.Timestamp nullTimestamp)
Sets the value to return when a SQL null is encountered as the result of invoking agetTimestampmethod.- Parameters:
nullTimestamp- the value
-
setNullURL
public void setNullURL(java.net.URL nullURL)
Sets the value to return when a SQL null is encountered as the result of invoking agetURLmethod.- Parameters:
nullURL- the value
-
-