@@ -42,6 +42,7 @@ public class SQLResultSet implements ResultSet {
4242 private final SQLResultSetMetaData metaData ;
4343
4444 private Map <String , Integer > columnByNameLookups ;
45+ private boolean lastColumnWasNull ;
4546
4647 private final Statement statement ;
4748 private final int maxRows ;
@@ -86,6 +87,24 @@ public List<Object> getCurrentRow() throws SQLException {
8687 return iterator .getItem ();
8788 }
8889
90+ protected Object getRaw (int columnIndex ) throws SQLException {
91+ checkNotClosed ();
92+ metaData .checkColumnIndex (columnIndex );
93+ List <Object > row = getCurrentRow ();
94+ Object value = row .get (columnIndex - 1 );
95+ lastColumnWasNull = (value == null );
96+ return value ;
97+ }
98+
99+ protected Number getNumber (int columnIndex ) throws SQLException {
100+ Number raw = (Number ) getRaw (columnIndex );
101+ return raw == null ? 0 : raw ;
102+ }
103+
104+ protected Number getNullableNumber (int columnIndex ) throws SQLException {
105+ return (Number ) getRaw (columnIndex );
106+ }
107+
89108 @ Override
90109 public void close () throws SQLException {
91110 if (isClosed .compareAndSet (false , true )) {
@@ -95,14 +114,8 @@ public void close() throws SQLException {
95114
96115 @ Override
97116 public boolean wasNull () throws SQLException {
98- return false ;
99- }
100-
101- protected Object getRaw (int columnIndex ) throws SQLException {
102117 checkNotClosed ();
103- metaData .checkColumnIndex (columnIndex );
104- List <Object > row = getCurrentRow ();
105- return row .get (columnIndex - 1 );
118+ return lastColumnWasNull ;
106119 }
107120
108121 @ Override
@@ -156,11 +169,6 @@ public int getInt(String columnLabel) throws SQLException {
156169 return getInt (findColumn (columnLabel ));
157170 }
158171
159- private Number getNumber (int columnIndex ) throws SQLException {
160- Number raw = (Number ) getRaw (columnIndex );
161- return raw == null ? 0 : raw ;
162- }
163-
164172 @ Override
165173 public long getLong (int columnIndex ) throws SQLException {
166174 return (getNumber (columnIndex )).longValue ();
@@ -193,13 +201,17 @@ public double getDouble(String columnLabel) throws SQLException {
193201
194202 @ Override
195203 public BigDecimal getBigDecimal (int columnIndex , int scale ) throws SQLException {
196- BigDecimal bigDecimal = new BigDecimal (getString (columnIndex ));
204+ String raw = getString (columnIndex );
205+ if (raw == null ) {
206+ return null ;
207+ }
208+ BigDecimal bigDecimal = new BigDecimal (raw );
197209 return scale > -1 ? bigDecimal .setScale (scale ) : bigDecimal ;
198210 }
199211
200212 @ Override
201213 public BigDecimal getBigDecimal (String columnLabel , int scale ) throws SQLException {
202- return getBigDecimal (findColumn (columnLabel ));
214+ return getBigDecimal (findColumn (columnLabel ), scale );
203215 }
204216
205217 @ Override
@@ -224,7 +236,8 @@ public byte[] getBytes(String columnLabel) throws SQLException {
224236
225237 @ Override
226238 public Date getDate (int columnIndex ) throws SQLException {
227- return new java .sql .Date (getLong (columnIndex ));
239+ Number time = getNullableNumber (columnIndex );
240+ return time == null ? null : new java .sql .Date (time .longValue ());
228241 }
229242
230243 @ Override
@@ -244,7 +257,8 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
244257
245258 @ Override
246259 public Time getTime (int columnIndex ) throws SQLException {
247- return new java .sql .Time (getLong (columnIndex ));
260+ Number time = getNullableNumber (columnIndex );
261+ return time == null ? null : new java .sql .Time (time .longValue ());
248262 }
249263
250264 @ Override
@@ -264,7 +278,8 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
264278
265279 @ Override
266280 public Timestamp getTimestamp (int columnIndex ) throws SQLException {
267- return new java .sql .Timestamp (getLong (columnIndex ));
281+ Number time = getNullableNumber (columnIndex );
282+ return time == null ? null : new java .sql .Timestamp (time .longValue ());
268283 }
269284
270285 @ Override
@@ -285,7 +300,8 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
285300
286301 @ Override
287302 public InputStream getAsciiStream (int columnIndex ) throws SQLException {
288- return new ByteArrayInputStream (getString (columnIndex ).getBytes (Charset .forName ("ASCII" )));
303+ String string = getString (columnIndex );
304+ return string == null ? null : new ByteArrayInputStream (string .getBytes (Charset .forName ("ASCII" )));
289305 }
290306
291307 @ Override
@@ -306,7 +322,8 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException {
306322
307323 @ Override
308324 public InputStream getBinaryStream (int columnIndex ) throws SQLException {
309- return new ByteArrayInputStream (getBytes (columnIndex ));
325+ byte [] bytes = getBytes (columnIndex );
326+ return bytes == null ? null : new ByteArrayInputStream (bytes );
310327 }
311328
312329 @ Override
@@ -316,12 +333,13 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
316333
317334 @ Override
318335 public Reader getCharacterStream (int columnIndex ) throws SQLException {
319- return new StringReader (getString (columnIndex ));
336+ String value = getString (columnIndex );
337+ return value == null ? null : new StringReader (value );
320338 }
321339
322340 @ Override
323341 public Reader getCharacterStream (String columnLabel ) throws SQLException {
324- return new StringReader ( getString (columnLabel ));
342+ return getCharacterStream ( findColumn (columnLabel ));
325343 }
326344
327345 @ Override
@@ -331,7 +349,7 @@ public Object getObject(int columnIndex) throws SQLException {
331349
332350 @ Override
333351 public Object getObject (String columnLabel ) throws SQLException {
334- return getRaw (findColumn (columnLabel ));
352+ return getObject (findColumn (columnLabel ));
335353 }
336354
337355 @ Override
@@ -346,12 +364,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
346364
347365 @ Override
348366 public <T > T getObject (int columnIndex , Class <T > type ) throws SQLException {
349- return type .cast (getRaw (columnIndex ));
367+ try {
368+ return type .cast (getRaw (columnIndex ));
369+ } catch (Exception e ) {
370+ throw new SQLNonTransientException (e );
371+ }
350372 }
351373
352374 @ Override
353375 public <T > T getObject (String columnLabel , Class <T > type ) throws SQLException {
354- return type . cast ( getRaw ( findColumn (columnLabel )) );
376+ return getObject ( findColumn (columnLabel ), type );
355377 }
356378
357379 @ Override
0 commit comments