KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > JDBCUtil


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * 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 *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util;
19
20 import java.sql.*;
21 import java.util.Locale JavaDoc;
22
23 /**
24  * <p>Helper class for managing common JDBC tasks.</p>
25  *
26  * <p>This class is abstract to allow implementations to
27  * take advantage of different logging capabilities/interfaces in
28  * different parts of the code.</p>
29  *
30  * @version CVS $Revision: 1.2.4.4 $ $Date: 2004/03/15 03:54:22 $
31  */

32 abstract public class JDBCUtil
33 {
34     /**
35      * An abstract method which child classes override to handle logging of
36      * errors in their particular environments.
37      *
38      * @param errorString the error message generated
39      */

40     abstract protected void delegatedLog(String JavaDoc errorString);
41
42     /**
43      * Checks database metadata to see if a table exists.
44      * Try UPPER, lower, and MixedCase, to see if the table is there.
45      *
46      * @param dbMetaData the database metadata to be used to look up this table
47      * @param tableName the table name
48      *
49      * @throws SQLException if an exception is encountered while accessing the database
50      */

51     public boolean tableExists(DatabaseMetaData dbMetaData, String JavaDoc tableName)
52         throws SQLException {
53         return ( tableExistsCaseSensitive(dbMetaData, tableName) ||
54                  tableExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US)) ||
55                  tableExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US)) );
56     }
57
58     /**
59      * Checks database metadata to see if a table exists. This method
60      * is sensitive to the case of the provided table name.
61      *
62      * @param dbMetaData the database metadata to be used to look up this table
63      * @param tableName the case sensitive table name
64      *
65      * @throws SQLException if an exception is encountered while accessing the database
66      */

67     public boolean tableExistsCaseSensitive(DatabaseMetaData dbMetaData, String JavaDoc tableName)
68         throws SQLException {
69         ResultSet rsTables = dbMetaData.getTables(null, null, tableName, null);
70         try {
71             boolean found = rsTables.next();
72             return found;
73         } finally {
74             closeJDBCResultSet(rsTables);
75         }
76     }
77
78     /**
79      * Checks database metadata to see if a column exists in a table
80      * Try UPPER, lower, and MixedCase, both on the table name and the column name, to see if the column is there.
81      *
82      * @param dbMetaData the database metadata to be used to look up this column
83      * @param tableName the table name
84      * @param columnName the column name
85      *
86      * @throws SQLException if an exception is encountered while accessing the database
87      */

88     public boolean columnExists(DatabaseMetaData dbMetaData, String JavaDoc tableName, String JavaDoc columnName)
89         throws SQLException {
90         return ( columnExistsCaseSensitive(dbMetaData, tableName, columnName) ||
91                  columnExistsCaseSensitive(dbMetaData, tableName, columnName.toUpperCase(Locale.US)) ||
92                  columnExistsCaseSensitive(dbMetaData, tableName, columnName.toLowerCase(Locale.US)) ||
93                  columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName) ||
94                  columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
95                  columnExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US), columnName.toLowerCase(Locale.US)) ||
96                  columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName) ||
97                  columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName.toUpperCase(Locale.US)) ||
98                  columnExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US), columnName.toLowerCase(Locale.US)) );
99     }
100
101     /**
102      * Checks database metadata to see if a column exists in a table. This method
103      * is sensitive to the case of both the provided table name and column name.
104      *
105      * @param dbMetaData the database metadata to be used to look up this column
106      * @param tableName the case sensitive table name
107      * @param columnName the case sensitive column name
108      *
109      * @throws SQLException if an exception is encountered while accessing the database
110      */

111     public boolean columnExistsCaseSensitive(DatabaseMetaData dbMetaData, String JavaDoc tableName, String JavaDoc columnName)
112         throws SQLException {
113         ResultSet rsTables = dbMetaData.getColumns(null, null, tableName, columnName);
114         try {
115             boolean found = rsTables.next();
116             return found;
117         } finally {
118             closeJDBCResultSet(rsTables);
119         }
120     }
121
122     /**
123      * Closes database connection and logs if an error
124      * is encountered
125      *
126      * @param conn the connection to be closed
127      */

128     public void closeJDBCConnection(Connection conn) {
129         try {
130             if (conn != null) {
131                 conn.close();
132             }
133         } catch (SQLException sqle) {
134             // Log exception and continue
135
subclassLogWrapper("Unexpected exception while closing database connection.");
136         }
137     }
138
139     /**
140      * Closes database statement and logs if an error
141      * is encountered
142      *
143      * @param stmt the statement to be closed
144      */

145     public void closeJDBCStatement(Statement stmt) {
146         try {
147             if (stmt != null) {
148                 stmt.close();
149             }
150         } catch (SQLException sqle) {
151             // Log exception and continue
152
subclassLogWrapper("Unexpected exception while closing database statement.");
153         }
154     }
155
156     /**
157      * Closes database result set and logs if an error
158      * is encountered
159      *
160      * @param aResultSet the result set to be closed
161      */

162     public void closeJDBCResultSet(ResultSet aResultSet ) {
163         try {
164             if (aResultSet != null) {
165                 aResultSet.close();
166             }
167         } catch (SQLException sqle) {
168             // Log exception and continue
169
subclassLogWrapper("Unexpected exception while closing database result set.");
170         }
171     }
172
173     /**
174      * Wraps the delegated call to the subclass logging method with a Throwable
175      * wrapper. All throwables generated by the subclass logging method are
176      * caught and ignored.
177      *
178      * @param logString the raw string to be passed to the logging method implemented
179      * by the subclass
180      */

181     private void subclassLogWrapper(String JavaDoc logString)
182     {
183         try {
184             delegatedLog(logString);
185         }
186         catch(Throwable JavaDoc t) {
187             // Throwables generated by the logging system are ignored
188
}
189     }
190
191 }
192
Popular Tags