001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.dbutils;
018    
019    import java.io.PrintWriter;
020    import java.sql.Connection;
021    import java.sql.ResultSet;
022    import java.sql.SQLException;
023    import java.sql.Statement;
024    
025    /**
026     * A collection of JDBC helper methods.  This class is thread safe.
027     */
028    public final class DbUtils {
029    
030        /**
031         * Default constructor.
032         *
033         * Utility classes should not have a public or default constructor,
034         * but this one preserves retro-compatibility.
035         *
036         * @since 1.4
037         */
038        public DbUtils() {
039            // do nothing
040        }
041    
042        /**
043         * Close a <code>Connection</code>, avoid closing if null.
044         *
045         * @param conn Connection to close.
046         * @throws SQLException if a database access error occurs
047         */
048        public static void close(Connection conn) throws SQLException {
049            if (conn != null) {
050                conn.close();
051            }
052        }
053    
054        /**
055         * Close a <code>ResultSet</code>, avoid closing if null.
056         *
057         * @param rs ResultSet to close.
058         * @throws SQLException if a database access error occurs
059         */
060        public static void close(ResultSet rs) throws SQLException {
061            if (rs != null) {
062                rs.close();
063            }
064        }
065    
066        /**
067         * Close a <code>Statement</code>, avoid closing if null.
068         *
069         * @param stmt Statement to close.
070         * @throws SQLException if a database access error occurs
071         */
072        public static void close(Statement stmt) throws SQLException {
073            if (stmt != null) {
074                stmt.close();
075            }
076        }
077    
078        /**
079         * Close a <code>Connection</code>, avoid closing if null and hide
080         * any SQLExceptions that occur.
081         *
082         * @param conn Connection to close.
083         */
084        public static void closeQuietly(Connection conn) {
085            try {
086                close(conn);
087            } catch (SQLException e) { // NOPMD
088                // quiet
089            }
090        }
091    
092        /**
093         * Close a <code>Connection</code>, <code>Statement</code> and
094         * <code>ResultSet</code>.  Avoid closing if null and hide any
095         * SQLExceptions that occur.
096         *
097         * @param conn Connection to close.
098         * @param stmt Statement to close.
099         * @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    }