View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package org.apache.james.util; 9 10 import java.sql.*; 11 import java.util.Locale; 12 13 /*** 14 * <p>Helper class for managing common JDBC tasks.</p> 15 * 16 * <p>This class is abstract to allow implementations to 17 * take advantage of different logging capabilities/interfaces in 18 * different parts of the code.</p> 19 * 20 * @author Noel Bergman <noel@devtech.com> 21 * @author Peter M. Goldstein <farsight@alum.mit.edu> 22 * 23 */ 24 abstract public class JDBCUtil 25 { 26 /*** 27 * An abstract method which child classes override to handle logging of 28 * errors in their particular environments. 29 * 30 * @param errorString the error message generated 31 */ 32 abstract protected void delegatedLog(String errorString); 33 34 /*** 35 * Checks database metadata to see if a table exists. 36 * Try UPPER, lower, and MixedCase, to see if the table is there. 37 * 38 * @param dbMetaData the database metadata to be used to look up this table 39 * @param tableName the table name 40 * 41 * @throws SQLException if an exception is encountered while accessing the database 42 */ 43 public boolean tableExists(DatabaseMetaData dbMetaData, String tableName) 44 throws SQLException { 45 return ( tableExistsCaseSensitive(dbMetaData, tableName) || 46 tableExistsCaseSensitive(dbMetaData, tableName.toUpperCase(Locale.US)) || 47 tableExistsCaseSensitive(dbMetaData, tableName.toLowerCase(Locale.US)) ); 48 } 49 50 /*** 51 * Checks database metadata to see if a table exists. This method 52 * is sensitive to the case of the provided table name. 53 * 54 * @param dbMetaData the database metadata to be used to look up this table 55 * @param tableName the case sensitive table name 56 * 57 * @throws SQLException if an exception is encountered while accessing the database 58 */ 59 public boolean tableExistsCaseSensitive(DatabaseMetaData dbMetaData, String tableName) 60 throws SQLException { 61 ResultSet rsTables = dbMetaData.getTables(null, null, tableName, null); 62 try { 63 boolean found = rsTables.next(); 64 return found; 65 } finally { 66 closeJDBCResultSet(rsTables); 67 } 68 } 69 70 /*** 71 * Closes database connection and logs if an error 72 * is encountered 73 * 74 * @param conn the connection to be closed 75 */ 76 public void closeJDBCConnection(Connection conn) { 77 try { 78 if (conn != null) { 79 conn.close(); 80 } 81 } catch (SQLException sqle) { 82 // Log exception and continue 83 subclassLogWrapper("Unexpected exception while closing database connection."); 84 } 85 } 86 87 /*** 88 * Closes database statement and logs if an error 89 * is encountered 90 * 91 * @param stmt the statement to be closed 92 */ 93 public void closeJDBCStatement(Statement stmt) { 94 try { 95 if (stmt != null) { 96 stmt.close(); 97 } 98 } catch (SQLException sqle) { 99 // Log exception and continue 100 subclassLogWrapper("Unexpected exception while closing database statement."); 101 } 102 } 103 104 /*** 105 * Closes database result set and logs if an error 106 * is encountered 107 * 108 * @param aResultSet the result set to be closed 109 */ 110 public void closeJDBCResultSet(ResultSet aResultSet ) { 111 try { 112 if (aResultSet != null) { 113 aResultSet.close(); 114 } 115 } catch (SQLException sqle) { 116 // Log exception and continue 117 subclassLogWrapper("Unexpected exception while closing database result set."); 118 } 119 } 120 121 /*** 122 * Wraps the delegated call to the subclass logging method with a Throwable 123 * wrapper. All throwables generated by the subclass logging method are 124 * caught and ignored. 125 * 126 * @param logString the raw string to be passed to the logging method implemented 127 * by the subclass 128 */ 129 private void subclassLogWrapper(String logString) 130 { 131 try { 132 delegatedLog(logString); 133 } 134 catch(Throwable t) { 135 // Throwables generated by the logging system are ignored 136 } 137 } 138 139 }

This page was automatically generated by Maven