1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbcp2;
18
19 import java.io.InputStream;
20 import java.io.Reader;
21 import java.math.BigDecimal;
22 import java.net.URL;
23 import java.sql.Array;
24 import java.sql.Blob;
25 import java.sql.Clob;
26 import java.sql.Connection;
27 import java.sql.DatabaseMetaData;
28 import java.sql.Date;
29 import java.sql.Ref;
30 import java.sql.ResultSet;
31 import java.sql.RowId;
32 import java.sql.SQLException;
33 import java.sql.SQLFeatureNotSupportedException;
34 import java.sql.SQLXML;
35 import java.sql.Statement;
36 import java.sql.Time;
37 import java.sql.Timestamp;
38 import java.util.concurrent.Executor;
39 import java.util.logging.Logger;
40
41 import javax.sql.CommonDataSource;
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class Jdbc41Bridge {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static void abort(final Connection connection, final Executor executor) throws SQLException {
72 try {
73 connection.abort(executor);
74 } catch (final AbstractMethodError e) {
75 connection.close();
76 }
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public static void closeOnCompletion(final Statement statement) throws SQLException {
93 try {
94 statement.closeOnCompletion();
95 } catch (final AbstractMethodError e) {
96 if (statement.isClosed()) {
97 throw new SQLException("Statement closed");
98 }
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static boolean generatedKeyAlwaysReturned(final DatabaseMetaData databaseMetaData) throws SQLException {
117 try {
118 return databaseMetaData.generatedKeyAlwaysReturned();
119 } catch (final AbstractMethodError e) {
120
121 return false;
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public static int getNetworkTimeout(final Connection connection) throws SQLException {
139 try {
140 return connection.getNetworkTimeout();
141 } catch (final AbstractMethodError e) {
142 return 0;
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 @SuppressWarnings("unchecked")
166 public static <T> T getObject(final ResultSet resultSet, final int columnIndex, final Class<T> type)
167 throws SQLException {
168 try {
169 return resultSet.getObject(columnIndex, type);
170 } catch (final AbstractMethodError e) {
171 if (type == String.class) {
172 return (T) resultSet.getString(columnIndex);
173 }
174
175 if (type == Integer.class) {
176 return (T) Integer.valueOf(resultSet.getInt(columnIndex));
177 }
178 if (type == Long.class) {
179 return (T) Long.valueOf(resultSet.getLong(columnIndex));
180 }
181 if (type == Double.class) {
182 return (T) Double.valueOf(resultSet.getDouble(columnIndex));
183 }
184 if (type == Float.class) {
185 return (T) Float.valueOf(resultSet.getFloat(columnIndex));
186 }
187 if (type == Short.class) {
188 return (T) Short.valueOf(resultSet.getShort(columnIndex));
189 }
190 if (type == BigDecimal.class) {
191 return (T) resultSet.getBigDecimal(columnIndex);
192 }
193 if (type == Byte.class) {
194 return (T) Byte.valueOf(resultSet.getByte(columnIndex));
195 }
196
197 if (type == Date.class) {
198 return (T) resultSet.getDate(columnIndex);
199 }
200 if (type == Time.class) {
201 return (T) resultSet.getTime(columnIndex);
202 }
203 if (type == Timestamp.class) {
204 return (T) resultSet.getTimestamp(columnIndex);
205 }
206
207 if (type == InputStream.class) {
208 return (T) resultSet.getBinaryStream(columnIndex);
209 }
210 if (type == Reader.class) {
211 return (T) resultSet.getCharacterStream(columnIndex);
212 }
213
214 if (type == Object.class) {
215 return (T) resultSet.getObject(columnIndex);
216 }
217 if (type == Boolean.class) {
218 return (T) Boolean.valueOf(resultSet.getBoolean(columnIndex));
219 }
220 if (type == Array.class) {
221 return (T) resultSet.getArray(columnIndex);
222 }
223 if (type == Blob.class) {
224 return (T) resultSet.getBlob(columnIndex);
225 }
226 if (type == Clob.class) {
227 return (T) resultSet.getClob(columnIndex);
228 }
229 if (type == Ref.class) {
230 return (T) resultSet.getRef(columnIndex);
231 }
232 if (type == RowId.class) {
233 return (T) resultSet.getRowId(columnIndex);
234 }
235 if (type == SQLXML.class) {
236 return (T) resultSet.getSQLXML(columnIndex);
237 }
238 if (type == URL.class) {
239 return (T) resultSet.getURL(columnIndex);
240 }
241 throw new SQLFeatureNotSupportedException(
242 String.format("resultSet=%s, columnIndex=%,d, type=%s", resultSet, columnIndex, type));
243 }
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 @SuppressWarnings("unchecked")
263 public static <T> T getObject(final ResultSet resultSet, final String columnLabel, final Class<T> type)
264 throws SQLException {
265 try {
266 return resultSet.getObject(columnLabel, type);
267 } catch (final AbstractMethodError e) {
268
269 if (type == Integer.class) {
270 return (T) Integer.valueOf(resultSet.getInt(columnLabel));
271 }
272 if (type == Long.class) {
273 return (T) Long.valueOf(resultSet.getLong(columnLabel));
274 }
275 if (type == Double.class) {
276 return (T) Double.valueOf(resultSet.getDouble(columnLabel));
277 }
278 if (type == Float.class) {
279 return (T) Float.valueOf(resultSet.getFloat(columnLabel));
280 }
281 if (type == Short.class) {
282 return (T) Short.valueOf(resultSet.getShort(columnLabel));
283 }
284 if (type == BigDecimal.class) {
285 return (T) resultSet.getBigDecimal(columnLabel);
286 }
287 if (type == Byte.class) {
288 return (T) Byte.valueOf(resultSet.getByte(columnLabel));
289 }
290
291 if (type == Date.class) {
292 return (T) resultSet.getDate(columnLabel);
293 }
294 if (type == Time.class) {
295 return (T) resultSet.getTime(columnLabel);
296 }
297 if (type == Timestamp.class) {
298 return (T) resultSet.getTimestamp(columnLabel);
299 }
300
301 if (type == InputStream.class) {
302 return (T) resultSet.getBinaryStream(columnLabel);
303 }
304 if (type == Reader.class) {
305 return (T) resultSet.getCharacterStream(columnLabel);
306 }
307
308 if (type == Object.class) {
309 return (T) resultSet.getObject(columnLabel);
310 }
311 if (type == Boolean.class) {
312 return (T) Boolean.valueOf(resultSet.getBoolean(columnLabel));
313 }
314 if (type == Array.class) {
315 return (T) resultSet.getArray(columnLabel);
316 }
317 if (type == Blob.class) {
318 return (T) resultSet.getBlob(columnLabel);
319 }
320 if (type == Clob.class) {
321 return (T) resultSet.getClob(columnLabel);
322 }
323 if (type == Ref.class) {
324 return (T) resultSet.getRef(columnLabel);
325 }
326 if (type == RowId.class) {
327 return (T) resultSet.getRowId(columnLabel);
328 }
329 if (type == SQLXML.class) {
330 return (T) resultSet.getSQLXML(columnLabel);
331 }
332 if (type == URL.class) {
333 return (T) resultSet.getURL(columnLabel);
334 }
335 throw new SQLFeatureNotSupportedException(
336 String.format("resultSet=%s, columnLabel=%s, type=%s", resultSet, columnLabel, type));
337 }
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public static Logger getParentLogger(final CommonDataSource commonDataSource) throws SQLFeatureNotSupportedException {
353 try {
354 return commonDataSource.getParentLogger();
355 } catch (final AbstractMethodError e) {
356 throw new SQLFeatureNotSupportedException("javax.sql.CommonDataSource#getParentLogger()");
357 }
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383 public static ResultSet getPseudoColumns(final DatabaseMetaData databaseMetaData, final String catalog,
384 final String schemaPattern, final String tableNamePattern, final String columnNamePattern)
385 throws SQLException {
386 try {
387 return databaseMetaData.getPseudoColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
388 } catch (final AbstractMethodError e) {
389
390 return null;
391 }
392 }
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407 public static String getSchema(final Connection connection) throws SQLException {
408 try {
409 return connection.getSchema();
410 } catch (final AbstractMethodError e) {
411
412 return null;
413 }
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430 public static boolean isCloseOnCompletion(final Statement statement) throws SQLException {
431 try {
432 return statement.isCloseOnCompletion();
433 } catch (final AbstractMethodError e) {
434 if (statement.isClosed()) {
435 throw new SQLException("Statement closed");
436 }
437 return false;
438 }
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457 public static void setNetworkTimeout(final Connection connection, final Executor executor, final int milliseconds)
458 throws SQLException {
459 try {
460 connection.setNetworkTimeout(executor, milliseconds);
461 } catch (final AbstractMethodError ignored) {
462
463 }
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480 public static void setSchema(final Connection connection, final String schema) throws SQLException {
481 try {
482 connection.setSchema(schema);
483 } catch (final AbstractMethodError ignored) {
484
485 }
486 }
487
488 }