@@ -42,6 +42,7 @@ public class SQLResultSet implements ResultSet {
42
42
private final SQLResultSetMetaData metaData ;
43
43
44
44
private Map <String , Integer > columnByNameLookups ;
45
+ private boolean lastColumnWasNull ;
45
46
46
47
private final Statement statement ;
47
48
private final int maxRows ;
@@ -86,6 +87,24 @@ public List<Object> getCurrentRow() throws SQLException {
86
87
return iterator .getItem ();
87
88
}
88
89
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
+
89
108
@ Override
90
109
public void close () throws SQLException {
91
110
if (isClosed .compareAndSet (false , true )) {
@@ -95,14 +114,8 @@ public void close() throws SQLException {
95
114
96
115
@ Override
97
116
public boolean wasNull () throws SQLException {
98
- return false ;
99
- }
100
-
101
- protected Object getRaw (int columnIndex ) throws SQLException {
102
117
checkNotClosed ();
103
- metaData .checkColumnIndex (columnIndex );
104
- List <Object > row = getCurrentRow ();
105
- return row .get (columnIndex - 1 );
118
+ return lastColumnWasNull ;
106
119
}
107
120
108
121
@ Override
@@ -156,11 +169,6 @@ public int getInt(String columnLabel) throws SQLException {
156
169
return getInt (findColumn (columnLabel ));
157
170
}
158
171
159
- private Number getNumber (int columnIndex ) throws SQLException {
160
- Number raw = (Number ) getRaw (columnIndex );
161
- return raw == null ? 0 : raw ;
162
- }
163
-
164
172
@ Override
165
173
public long getLong (int columnIndex ) throws SQLException {
166
174
return (getNumber (columnIndex )).longValue ();
@@ -193,13 +201,17 @@ public double getDouble(String columnLabel) throws SQLException {
193
201
194
202
@ Override
195
203
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 );
197
209
return scale > -1 ? bigDecimal .setScale (scale ) : bigDecimal ;
198
210
}
199
211
200
212
@ Override
201
213
public BigDecimal getBigDecimal (String columnLabel , int scale ) throws SQLException {
202
- return getBigDecimal (findColumn (columnLabel ));
214
+ return getBigDecimal (findColumn (columnLabel ), scale );
203
215
}
204
216
205
217
@ Override
@@ -224,7 +236,8 @@ public byte[] getBytes(String columnLabel) throws SQLException {
224
236
225
237
@ Override
226
238
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 ());
228
241
}
229
242
230
243
@ Override
@@ -244,7 +257,8 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
244
257
245
258
@ Override
246
259
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 ());
248
262
}
249
263
250
264
@ Override
@@ -264,7 +278,8 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
264
278
265
279
@ Override
266
280
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 ());
268
283
}
269
284
270
285
@ Override
@@ -285,7 +300,8 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
285
300
286
301
@ Override
287
302
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" )));
289
305
}
290
306
291
307
@ Override
@@ -295,7 +311,8 @@ public InputStream getAsciiStream(String columnLabel) throws SQLException {
295
311
296
312
@ Override
297
313
public InputStream getUnicodeStream (int columnIndex ) throws SQLException {
298
- return new ByteArrayInputStream (getString (columnIndex ).getBytes (Charset .forName ("UTF-8" )));
314
+ String string = getString (columnIndex );
315
+ return string == null ? null : new ByteArrayInputStream (string .getBytes (Charset .forName ("UTF-16" )));
299
316
}
300
317
301
318
@ Override
@@ -305,7 +322,8 @@ public InputStream getUnicodeStream(String columnLabel) throws SQLException {
305
322
306
323
@ Override
307
324
public InputStream getBinaryStream (int columnIndex ) throws SQLException {
308
- return new ByteArrayInputStream (getBytes (columnIndex ));
325
+ byte [] bytes = getBytes (columnIndex );
326
+ return bytes == null ? null : new ByteArrayInputStream (bytes );
309
327
}
310
328
311
329
@ Override
@@ -315,12 +333,13 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
315
333
316
334
@ Override
317
335
public Reader getCharacterStream (int columnIndex ) throws SQLException {
318
- return new StringReader (getString (columnIndex ));
336
+ String value = getString (columnIndex );
337
+ return value == null ? null : new StringReader (value );
319
338
}
320
339
321
340
@ Override
322
341
public Reader getCharacterStream (String columnLabel ) throws SQLException {
323
- return new StringReader ( getString (columnLabel ));
342
+ return getCharacterStream ( findColumn (columnLabel ));
324
343
}
325
344
326
345
@ Override
@@ -330,7 +349,7 @@ public Object getObject(int columnIndex) throws SQLException {
330
349
331
350
@ Override
332
351
public Object getObject (String columnLabel ) throws SQLException {
333
- return getRaw (findColumn (columnLabel ));
352
+ return getObject (findColumn (columnLabel ));
334
353
}
335
354
336
355
@ Override
@@ -345,12 +364,16 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
345
364
346
365
@ Override
347
366
public <T > T getObject (int columnIndex , Class <T > type ) throws SQLException {
348
- return type .cast (getRaw (columnIndex ));
367
+ try {
368
+ return type .cast (getRaw (columnIndex ));
369
+ } catch (Exception e ) {
370
+ throw new SQLNonTransientException (e );
371
+ }
349
372
}
350
373
351
374
@ Override
352
375
public <T > T getObject (String columnLabel , Class <T > type ) throws SQLException {
353
- return type . cast ( getRaw ( findColumn (columnLabel )) );
376
+ return getObject ( findColumn (columnLabel ), type );
354
377
}
355
378
356
379
@ Override
0 commit comments