1 17 18 package org.apache.james.util; 19 20 import java.sql.*; 21 import java.util.Locale ; 22 23 32 abstract public class JDBCUtil 33 { 34 40 abstract protected void delegatedLog(String errorString); 41 42 51 public boolean tableExists(DatabaseMetaData dbMetaData, String 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 67 public boolean tableExistsCaseSensitive(DatabaseMetaData dbMetaData, String 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 88 public boolean columnExists(DatabaseMetaData dbMetaData, String tableName, String 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 111 public boolean columnExistsCaseSensitive(DatabaseMetaData dbMetaData, String tableName, String 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 128 public void closeJDBCConnection(Connection conn) { 129 try { 130 if (conn != null) { 131 conn.close(); 132 } 133 } catch (SQLException sqle) { 134 subclassLogWrapper("Unexpected exception while closing database connection."); 136 } 137 } 138 139 145 public void closeJDBCStatement(Statement stmt) { 146 try { 147 if (stmt != null) { 148 stmt.close(); 149 } 150 } catch (SQLException sqle) { 151 subclassLogWrapper("Unexpected exception while closing database statement."); 153 } 154 } 155 156 162 public void closeJDBCResultSet(ResultSet aResultSet ) { 163 try { 164 if (aResultSet != null) { 165 aResultSet.close(); 166 } 167 } catch (SQLException sqle) { 168 subclassLogWrapper("Unexpected exception while closing database result set."); 170 } 171 } 172 173 181 private void subclassLogWrapper(String logString) 182 { 183 try { 184 delegatedLog(logString); 185 } 186 catch(Throwable t) { 187 } 189 } 190 191 } 192 | Popular Tags |