View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils;
18  
19  import java.io.PrintWriter;
20  import java.sql.Connection;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.sql.Statement;
24  
25  /**
26   * A collection of JDBC helper methods.  This class is thread safe.
27   */
28  public final class DbUtils {
29  
30      /**
31       * Default constructor.
32       *
33       * Utility classes should not have a public or default constructor,
34       * but this one preserves retro-compatibility.
35       *
36       * @since 1.4
37       */
38      public DbUtils() {
39          // do nothing
40      }
41  
42      /**
43       * Close a <code>Connection</code>, avoid closing if null.
44       *
45       * @param conn Connection to close.
46       * @throws SQLException if a database access error occurs
47       */
48      public static void close(Connection conn) throws SQLException {
49          if (conn != null) {
50              conn.close();
51          }
52      }
53  
54      /**
55       * Close a <code>ResultSet</code>, avoid closing if null.
56       *
57       * @param rs ResultSet to close.
58       * @throws SQLException if a database access error occurs
59       */
60      public static void close(ResultSet rs) throws SQLException {
61          if (rs != null) {
62              rs.close();
63          }
64      }
65  
66      /**
67       * Close a <code>Statement</code>, avoid closing if null.
68       *
69       * @param stmt Statement to close.
70       * @throws SQLException if a database access error occurs
71       */
72      public static void close(Statement stmt) throws SQLException {
73          if (stmt != null) {
74              stmt.close();
75          }
76      }
77  
78      /**
79       * Close a <code>Connection</code>, avoid closing if null and hide
80       * any SQLExceptions that occur.
81       *
82       * @param conn Connection to close.
83       */
84      public static void closeQuietly(Connection conn) {
85          try {
86              close(conn);
87          } catch (SQLException e) { // NOPMD
88              // quiet
89          }
90      }
91  
92      /**
93       * Close a <code>Connection</code>, <code>Statement</code> and
94       * <code>ResultSet</code>.  Avoid closing if null and hide any
95       * SQLExceptions that occur.
96       *
97       * @param conn Connection to close.
98       * @param stmt Statement to close.
99       * @param rs ResultSet to close.
100      */
101     public static void closeQuietly(Connection conn, Statement stmt,
102             ResultSet rs) {
103 
104         try {
105             closeQuietly(rs);
106         } finally {
107             try {
108                 closeQuietly(stmt);
109             } finally {
110                 closeQuietly(conn);
111             }
112         }
113 
114     }
115 
116     /**
117      * Close a <code>ResultSet</code>, avoid closing if null and hide any
118      * SQLExceptions that occur.
119      *
120      * @param rs ResultSet to close.
121      */
122     public static void closeQuietly(ResultSet rs) {
123         try {
124             close(rs);
125         } catch (SQLException e) { // NOPMD
126             // quiet
127         }
128     }
129 
130     /**
131      * Close a <code>Statement</code>, avoid closing if null and hide
132      * any SQLExceptions that occur.
133      *
134      * @param stmt Statement to close.
135      */
136     public static void closeQuietly(Statement stmt) {
137         try {
138             close(stmt);
139         } catch (SQLException e) { // NOPMD
140             // quiet
141         }
142     }
143 
144     /**
145      * Commits a <code>Connection</code> then closes it, avoid closing if null.
146      *
147      * @param conn Connection to close.
148      * @throws SQLException if a database access error occurs
149      */
150     public static void commitAndClose(Connection conn) throws SQLException {
151         if (conn != null) {
152             try {
153                 conn.commit();
154             } finally {
155                 conn.close();
156             }
157         }
158     }
159 
160     /**
161      * Commits a <code>Connection</code> then closes it, avoid closing if null
162      * and hide any SQLExceptions that occur.
163      *
164      * @param conn Connection to close.
165      */
166     public static void commitAndCloseQuietly(Connection conn) {
167         try {
168             commitAndClose(conn);
169         } catch (SQLException e) { // NOPMD
170             // quiet
171         }
172     }
173 
174     /**
175      * Loads and registers a database driver class.
176      * If this succeeds, it returns true, else it returns false.
177      *
178      * @param driverClassName of driver to load
179      * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
180      */
181     public static boolean loadDriver(String driverClassName) {
182         return loadDriver(DbUtils.class.getClassLoader(), driverClassName);
183     }
184 
185     /**
186      * Loads and registers a database driver class.
187      * If this succeeds, it returns true, else it returns false.
188      *
189      * @param classLoader the class loader used to load the driver class
190      * @param driverClassName of driver to load
191      * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
192      * @since 1.4
193      */
194     public static boolean loadDriver(ClassLoader classLoader, String driverClassName) {
195         try {
196             classLoader.loadClass(driverClassName).newInstance();
197             return true;
198 
199         } catch (IllegalAccessException e) {
200             // Constructor is private, OK for DriverManager contract
201             return true;
202 
203         } catch (Exception e) {
204             return false;
205 
206         }
207     }
208 
209     /**
210      * Print the stack trace for a SQLException to STDERR.
211      *
212      * @param e SQLException to print stack trace of
213      */
214     public static void printStackTrace(SQLException e) {
215         printStackTrace(e, new PrintWriter(System.err));
216     }
217 
218     /**
219      * Print the stack trace for a SQLException to a
220      * specified PrintWriter.
221      *
222      * @param e SQLException to print stack trace of
223      * @param pw PrintWriter to print to
224      */
225     public static void printStackTrace(SQLException e, PrintWriter pw) {
226 
227         SQLException next = e;
228         while (next != null) {
229             next.printStackTrace(pw);
230             next = next.getNextException();
231             if (next != null) {
232                 pw.println("Next SQLException:");
233             }
234         }
235     }
236 
237     /**
238      * Print warnings on a Connection to STDERR.
239      *
240      * @param conn Connection to print warnings from
241      */
242     public static void printWarnings(Connection conn) {
243         printWarnings(conn, new PrintWriter(System.err));
244     }
245 
246     /**
247      * Print warnings on a Connection to a specified PrintWriter.
248      *
249      * @param conn Connection to print warnings from
250      * @param pw PrintWriter to print to
251      */
252     public static void printWarnings(Connection conn, PrintWriter pw) {
253         if (conn != null) {
254             try {
255                 printStackTrace(conn.getWarnings(), pw);
256             } catch (SQLException e) {
257                 printStackTrace(e, pw);
258             }
259         }
260     }
261 
262     /**
263      * Rollback any changes made on the given connection.
264      * @param conn Connection to rollback.  A null value is legal.
265      * @throws SQLException if a database access error occurs
266      */
267     public static void rollback(Connection conn) throws SQLException {
268         if (conn != null) {
269             conn.rollback();
270         }
271     }
272 
273     /**
274      * Performs a rollback on the <code>Connection</code> then closes it,
275      * avoid closing if null.
276      *
277      * @param conn Connection to rollback.  A null value is legal.
278      * @throws SQLException if a database access error occurs
279      * @since DbUtils 1.1
280      */
281     public static void rollbackAndClose(Connection conn) throws SQLException {
282         if (conn != null) {
283             try {
284                 conn.rollback();
285             } finally {
286                 conn.close();
287             }
288         }
289     }
290 
291     /**
292      * Performs a rollback on the <code>Connection</code> then closes it,
293      * avoid closing if null and hide any SQLExceptions that occur.
294      *
295      * @param conn Connection to rollback.  A null value is legal.
296      * @since DbUtils 1.1
297      */
298     public static void rollbackAndCloseQuietly(Connection conn) {
299         try {
300             rollbackAndClose(conn);
301         } catch (SQLException e) { // NOPMD
302             // quiet
303         }
304     }
305 
306 }