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