1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbutils;
18
19 import static java.sql.DriverManager.registerDriver;
20
21 import java.io.PrintWriter;
22 import java.lang.reflect.Constructor;
23 import java.sql.Connection;
24 import java.sql.Driver;
25 import java.sql.DriverPropertyInfo;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.sql.SQLFeatureNotSupportedException;
29 import java.sql.Statement;
30 import java.util.Properties;
31 import java.util.logging.Logger;
32
33
34
35
36 public final class DbUtils {
37
38
39
40
41
42
43 static final class DriverProxy implements Driver {
44
45
46
47
48 private final Driver adapted;
49
50
51
52
53
54
55 public DriverProxy(final Driver adapted) {
56 this.adapted = adapted;
57 }
58
59
60
61
62 @Override
63 public boolean acceptsURL(final String url) throws SQLException {
64 return adapted.acceptsURL(url);
65 }
66
67
68
69
70 @Override
71 public Connection connect(final String url, final Properties info) throws SQLException {
72 return adapted.connect(url, info);
73 }
74
75
76
77
78 @Override
79 public int getMajorVersion() {
80 return adapted.getMajorVersion();
81 }
82
83
84
85
86 @Override
87 public int getMinorVersion() {
88 return adapted.getMinorVersion();
89 }
90
91
92
93
94 @Override
95 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
96 return adapted.getParentLogger();
97 }
98
99
100
101
102 @Override
103 public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws SQLException {
104 return adapted.getPropertyInfo(url, info);
105 }
106
107
108
109
110 @Override
111 public boolean jdbcCompliant() {
112 return adapted.jdbcCompliant();
113 }
114
115 }
116
117
118
119
120
121
122
123 public static void close(final Connection conn) throws SQLException {
124 if (conn != null) {
125 conn.close();
126 }
127 }
128
129
130
131
132
133
134
135 public static void close(final ResultSet resultSet) throws SQLException {
136 if (resultSet != null) {
137 resultSet.close();
138 }
139 }
140
141
142
143
144
145
146
147 public static void close(final Statement stmt) throws SQLException {
148 if (stmt != null) {
149 stmt.close();
150 }
151 }
152
153
154
155
156
157
158
159 public static void closeQuietly(final Connection conn) {
160 try {
161 close(conn);
162 } catch (final SQLException e) {
163
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176 public static void closeQuietly(final Connection conn, final Statement stmt,
177 final ResultSet rs) {
178
179 try {
180 closeQuietly(rs);
181 } finally {
182 try {
183 closeQuietly(stmt);
184 } finally {
185 closeQuietly(conn);
186 }
187 }
188
189 }
190
191
192
193
194
195
196
197 public static void closeQuietly(final ResultSet resultSet) {
198 try {
199 close(resultSet);
200 } catch (final SQLException e) {
201
202 }
203 }
204
205
206
207
208
209
210
211 public static void closeQuietly(final Statement stmt) {
212 try {
213 close(stmt);
214 } catch (final SQLException e) {
215
216 }
217 }
218
219
220
221
222
223
224
225 public static void commitAndClose(final Connection conn) throws SQLException {
226 if (conn != null) {
227 try {
228 conn.commit();
229 } finally {
230 conn.close();
231 }
232 }
233 }
234
235
236
237
238
239
240
241 public static void commitAndCloseQuietly(final Connection conn) {
242 try {
243 commitAndClose(conn);
244 } catch (final SQLException e) {
245
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258 public static boolean loadDriver(final ClassLoader classLoader, final String driverClassName) {
259 try {
260 final Class<?> loadedClass = classLoader.loadClass(driverClassName);
261
262 if (!Driver.class.isAssignableFrom(loadedClass)) {
263 return false;
264 }
265
266 @SuppressWarnings("unchecked")
267 final
268 Class<Driver> driverClass = (Class<Driver>) loadedClass;
269 final Constructor<Driver> driverConstructor = driverClass.getConstructor();
270
271
272 @SuppressWarnings("deprecation")
273
274
275 final boolean isConstructorAccessible = driverConstructor.isAccessible();
276 if (!isConstructorAccessible) {
277 driverConstructor.setAccessible(true);
278 }
279
280 try {
281 final Driver driver = driverConstructor.newInstance();
282 registerDriver(new DriverProxy(driver));
283 } finally {
284 driverConstructor.setAccessible(isConstructorAccessible);
285 }
286
287 return true;
288 } catch (final Exception e) {
289 return false;
290 }
291 }
292
293
294
295
296
297
298
299
300 public static boolean loadDriver(final String driverClassName) {
301 return loadDriver(DbUtils.class.getClassLoader(), driverClassName);
302 }
303
304
305
306
307
308
309 public static void printStackTrace(final SQLException e) {
310 printStackTrace(e, new PrintWriter(System.err));
311 }
312
313
314
315
316
317
318
319
320 public static void printStackTrace(final SQLException e, final PrintWriter pw) {
321
322 SQLException next = e;
323 while (next != null) {
324 next.printStackTrace(pw);
325 next = next.getNextException();
326 if (next != null) {
327 pw.println("Next SQLException:");
328 }
329 }
330 }
331
332
333
334
335
336
337 public static void printWarnings(final Connection conn) {
338 printWarnings(conn, new PrintWriter(System.err));
339 }
340
341
342
343
344
345
346
347 public static void printWarnings(final Connection conn, final PrintWriter pw) {
348 if (conn != null) {
349 try {
350 printStackTrace(conn.getWarnings(), pw);
351 } catch (final SQLException e) {
352 printStackTrace(e, pw);
353 }
354 }
355 }
356
357
358
359
360
361
362 public static void rollback(final Connection conn) throws SQLException {
363 if (conn != null) {
364 conn.rollback();
365 }
366 }
367
368
369
370
371
372
373
374
375
376
377 public static void rollbackAndClose(final Connection conn) throws SQLException {
378 if (conn != null) {
379 try {
380 conn.rollback();
381 } finally {
382 conn.close();
383 }
384 }
385 }
386
387
388
389
390
391
392
393
394 public static void rollbackAndCloseQuietly(final Connection conn) {
395 try {
396 rollbackAndClose(conn);
397 } catch (final SQLException e) {
398
399 }
400 }
401
402
403
404
405
406
407
408
409 public static void rollbackQuietly(final Connection conn) {
410 try {
411 rollback(conn);
412 } catch (final SQLException e) {
413
414 }
415 }
416
417
418
419
420
421
422
423
424
425 public DbUtils() {
426
427 }
428
429 }