001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib.util;
049:
050: import java.sql.Statement;
051: import java.sql.ResultSet;
052: import java.sql.Connection;
053: import java.sql.SQLWarning;
054: import java.sql.SQLException;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058:
059: /** Useful SQL utilities.
060:
061: @author Anthony Eden
062: @author kalman
063: */
064:
065: public class SQLUtilities {
066:
067: private static Log log = LogFactory.getLog(SQLUtilities.class);
068:
069: /** Close the given JDBC ResultSet if it is not null.
070:
071: @param rs The JDBC ResultSet
072: */
073:
074: public static void close(ResultSet rs) {
075: if (rs != null) {
076: try {
077: rs.close();
078: } catch (Exception e) {
079: log
080: .error("Error closing result set: "
081: + e.getMessage());
082: }
083: }
084: }
085:
086: /** Close the given JDBC statement if it is not null.
087:
088: @param stmt The JDBC Statement
089: */
090:
091: public static void close(Statement stmt) {
092: if (stmt != null) {
093: try {
094: stmt.close();
095: } catch (Exception e) {
096: log.error("Error closing statement: " + e.getMessage());
097: }
098: }
099: }
100:
101: /** Close the given JDBC connection if it is not null.
102:
103: @param c The JDBC connection
104: */
105:
106: public static void close(Connection c) {
107: if (c != null) {
108: try {
109: c.close();
110: } catch (Exception e) {
111: log
112: .error("Error closing connection: "
113: + e.getMessage());
114: }
115: }
116: }
117:
118: /** Convert the given SQLWarning into a String.
119:
120: @param sqlw The SQLWarning
121: */
122:
123: public static String toString(SQLWarning sqlw) {
124: StringBuffer buffer = new StringBuffer();
125: String newLine = System.getProperty("line.separator");
126: do {
127: buffer.append("error code = ").append(sqlw.getErrorCode())
128: .append(newLine);
129: buffer.append("localized message = ").append(
130: sqlw.getLocalizedMessage()).append(newLine);
131: buffer.append("message = ").append(sqlw.getMessage())
132: .append(newLine);
133: buffer.append("sqlstate = ").append(sqlw.getSQLState())
134: .append(newLine);
135: sqlw = sqlw.getNextWarning();
136: } while (sqlw != null);
137:
138: return buffer.toString();
139: }
140:
141: /** Convert the given SQLException into a String.
142:
143: @param sqlx The SQLException
144: @return A String
145: */
146:
147: public static String toString(SQLException sqlx) {
148: StringBuffer buffer = new StringBuffer();
149: String newLine = System.getProperty("line.separator");
150: do {
151: buffer.append("error code = ").append(sqlx.getErrorCode())
152: .append(newLine);
153: buffer.append("localized message = ").append(
154: sqlx.getLocalizedMessage()).append(newLine);
155: buffer.append("message = ").append(sqlx.getMessage())
156: .append(newLine);
157: buffer.append("sqlstate = ").append(sqlx.getSQLState())
158: .append(newLine);
159: sqlx = sqlx.getNextException();
160: } while (sqlx != null);
161:
162: return buffer.toString();
163: }
164:
165: }
|