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.unitils.thirdparty.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: * Close a <code>Connection</code>, avoid closing if null.
032: *
033: * @param conn Connection to close.
034: * @throws SQLException if a database access error occurs
035: */
036: public static void close(Connection conn) throws SQLException {
037: if (conn != null) {
038: conn.close();
039: }
040: }
041:
042: /**
043: * Close a <code>ResultSet</code>, avoid closing if null.
044: *
045: * @param rs ResultSet to close.
046: * @throws SQLException if a database access error occurs
047: */
048: public static void close(ResultSet rs) throws SQLException {
049: if (rs != null) {
050: rs.close();
051: }
052: }
053:
054: /**
055: * Close a <code>Statement</code>, avoid closing if null.
056: *
057: * @param stmt Statement to close.
058: * @throws SQLException if a database access error occurs
059: */
060: public static void close(Statement stmt) throws SQLException {
061: if (stmt != null) {
062: stmt.close();
063: }
064: }
065:
066: /**
067: * Close a <code>Connection</code>, avoid closing if null and hide
068: * any SQLExceptions that occur.
069: *
070: * @param conn Connection to close.
071: */
072: public static void closeQuietly(Connection conn) {
073: try {
074: close(conn);
075: } catch (SQLException e) {
076: // quiet
077: }
078: }
079:
080: /**
081: * Close a <code>Connection</code>, <code>Statement</code> and
082: * <code>ResultSet</code>. Avoid closing if null and hide any
083: * SQLExceptions that occur.
084: *
085: * @param conn Connection to close.
086: * @param stmt Statement to close.
087: * @param rs ResultSet to close.
088: */
089: public static void closeQuietly(Connection conn, Statement stmt,
090: ResultSet rs) {
091: try {
092: closeQuietly(rs);
093: } finally {
094: try {
095: closeQuietly(stmt);
096: } finally {
097: closeQuietly(conn);
098: }
099: }
100:
101: }
102:
103: /**
104: * Close a <code>ResultSet</code>, avoid closing if null and hide any
105: * SQLExceptions that occur.
106: *
107: * @param rs ResultSet to close.
108: */
109: public static void closeQuietly(ResultSet rs) {
110: try {
111: close(rs);
112: } catch (SQLException e) {
113: // quiet
114: }
115: }
116:
117: /**
118: * Close a <code>Statement</code>, avoid closing if null and hide
119: * any SQLExceptions that occur.
120: *
121: * @param stmt Statement to close.
122: */
123: public static void closeQuietly(Statement stmt) {
124: try {
125: close(stmt);
126: } catch (SQLException e) {
127: // quiet
128: }
129: }
130:
131: /**
132: * Commits a <code>Connection</code> then closes it, avoid closing if null.
133: *
134: * @param conn Connection to close.
135: * @throws SQLException if a database access error occurs
136: */
137: public static void commitAndClose(Connection conn)
138: throws SQLException {
139: if (conn != null) {
140: try {
141: conn.commit();
142: } finally {
143: conn.close();
144: }
145: }
146: }
147:
148: /**
149: * Commits a <code>Connection</code> then closes it, avoid closing if null
150: * and hide any SQLExceptions that occur.
151: *
152: * @param conn Connection to close.
153: */
154: public static void commitAndCloseQuietly(Connection conn) {
155: try {
156: commitAndClose(conn);
157: } catch (SQLException e) {
158: // quiet
159: }
160: }
161:
162: /**
163: * Loads and registers a database driver class.
164: * If this succeeds, it returns true, else it returns false.
165: *
166: * @param driverClassName of driver to load
167: * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
168: */
169: public static boolean loadDriver(String driverClassName) {
170: try {
171: Class.forName(driverClassName).newInstance();
172: return true;
173:
174: } catch (ClassNotFoundException e) {
175: return false;
176:
177: } catch (IllegalAccessException e) {
178: // Constructor is private, OK for DriverManager contract
179: return true;
180:
181: } catch (InstantiationException e) {
182: return false;
183:
184: } catch (Throwable e) {
185: return false;
186: }
187: }
188:
189: /**
190: * Print the stack trace for a SQLException to STDERR.
191: *
192: * @param e SQLException to print stack trace of
193: */
194: public static void printStackTrace(SQLException e) {
195: printStackTrace(e, new PrintWriter(System.err));
196: }
197:
198: /**
199: * Print the stack trace for a SQLException to a
200: * specified PrintWriter.
201: *
202: * @param e SQLException to print stack trace of
203: * @param pw PrintWriter to print to
204: */
205: public static void printStackTrace(SQLException e, PrintWriter pw) {
206: SQLException next = e;
207: while (next != null) {
208: next.printStackTrace(pw);
209: next = next.getNextException();
210: if (next != null) {
211: pw.println("Next SQLException:");
212: }
213: }
214: }
215:
216: /**
217: * Print warnings on a Connection to STDERR.
218: *
219: * @param conn Connection to print warnings from
220: */
221: public static void printWarnings(Connection conn) {
222: printWarnings(conn, new PrintWriter(System.err));
223: }
224:
225: /**
226: * Print warnings on a Connection to a specified PrintWriter.
227: *
228: * @param conn Connection to print warnings from
229: * @param pw PrintWriter to print to
230: */
231: public static void printWarnings(Connection conn, PrintWriter pw) {
232: if (conn != null) {
233: try {
234: printStackTrace(conn.getWarnings(), pw);
235: } catch (SQLException e) {
236: printStackTrace(e, pw);
237: }
238: }
239: }
240:
241: /**
242: * Rollback any changes made on the given connection.
243: *
244: * @param conn Connection to rollback. A null value is legal.
245: * @throws SQLException if a database access error occurs
246: */
247: public static void rollback(Connection conn) throws SQLException {
248: if (conn != null) {
249: conn.rollback();
250: }
251: }
252:
253: /**
254: * Performs a rollback on the <code>Connection</code> then closes it,
255: * avoid closing if null.
256: *
257: * @param conn Connection to rollback. A null value is legal.
258: * @throws SQLException if a database access error occurs
259: * @since DbUtils 1.1
260: */
261: public static void rollbackAndClose(Connection conn)
262: throws SQLException {
263: if (conn != null) {
264: try {
265: conn.rollback();
266: } finally {
267: conn.close();
268: }
269: }
270: }
271:
272: /**
273: * Performs a rollback on the <code>Connection</code> then closes it,
274: * avoid closing if null and hide any SQLExceptions that occur.
275: *
276: * @param conn Connection to rollback. A null value is legal.
277: * @since DbUtils 1.1
278: */
279: public static void rollbackAndCloseQuietly(Connection conn) {
280: try {
281: rollbackAndClose(conn);
282: } catch (SQLException e) {
283: // quiet
284: }
285: }
286:
287: }
|