1 16 17 package org.springframework.jdbc.support; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.sql.Blob ; 22 import java.sql.Clob ; 23 import java.sql.Connection ; 24 import java.sql.DatabaseMetaData ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.sql.Types ; 29 30 import javax.sql.DataSource ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.springframework.jdbc.CannotGetJdbcConnectionException; 36 import org.springframework.jdbc.datasource.DataSourceUtils; 37 38 45 public abstract class JdbcUtils { 46 47 51 public static final int TYPE_UNKNOWN = Integer.MIN_VALUE; 52 53 54 private static final Log logger = LogFactory.getLog(JdbcUtils.class); 55 56 57 62 public static void closeConnection(Connection con) { 63 if (con != null) { 64 try { 65 con.close(); 66 } 67 catch (SQLException ex) { 68 logger.debug("Could not close JDBC Connection", ex); 69 } 70 catch (Throwable ex) { 71 logger.debug("Unexpected exception on closing JDBC Connection", ex); 73 } 74 } 75 } 76 77 82 public static void closeStatement(Statement stmt) { 83 if (stmt != null) { 84 try { 85 stmt.close(); 86 } 87 catch (SQLException ex) { 88 logger.debug("Could not close JDBC Statement", ex); 89 } 90 catch (Throwable ex) { 91 logger.debug("Unexpected exception on closing JDBC Statement", ex); 93 } 94 } 95 } 96 97 102 public static void closeResultSet(ResultSet rs) { 103 if (rs != null) { 104 try { 105 rs.close(); 106 } 107 catch (SQLException ex) { 108 logger.debug("Could not close JDBC ResultSet", ex); 109 } 110 catch (Throwable ex) { 111 logger.debug("Unexpected exception on closing JDBC ResultSet", ex); 113 } 114 } 115 } 116 117 135 public static Object getResultSetValue(ResultSet rs, int index) throws SQLException { 136 Object obj = rs.getObject(index); 137 if (obj instanceof Blob ) { 138 obj = rs.getBytes(index); 139 } 140 else if (obj instanceof Clob ) { 141 obj = rs.getString(index); 142 } 143 else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) { 144 obj = rs.getTimestamp(index); 145 } 146 else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) { 147 String metaDataClassName = rs.getMetaData().getColumnClassName(index); 148 if ("java.sql.Timestamp".equals(metaDataClassName) || 149 "oracle.sql.TIMESTAMP".equals(metaDataClassName)) { 150 obj = rs.getTimestamp(index); 151 } 152 else { 153 obj = rs.getDate(index); 154 } 155 } 156 else if (obj != null && obj instanceof java.sql.Date ) { 157 if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) { 158 obj = rs.getTimestamp(index); 159 } 160 } 161 return obj; 162 } 163 164 179 public static Object extractDatabaseMetaData(DataSource dataSource, DatabaseMetaDataCallback action) 180 throws MetaDataAccessException { 181 182 Connection con = null; 183 try { 184 con = DataSourceUtils.getConnection(dataSource); 185 if (con == null) { 186 throw new MetaDataAccessException("Connection returned by DataSource [" + dataSource + "] was null"); 188 } 189 DatabaseMetaData metaData = con.getMetaData(); 190 if (metaData == null) { 191 throw new MetaDataAccessException("DatabaseMetaData returned by Connection [" + con + "] was null"); 193 } 194 return action.processMetaData(metaData); 195 } 196 catch (CannotGetJdbcConnectionException ex) { 197 throw new MetaDataAccessException("Could not get Connection for extracting meta data", ex); 198 } 199 catch (SQLException ex) { 200 throw new MetaDataAccessException("Error while extracting DatabaseMetaData", ex); 201 } 202 catch (AbstractMethodError err) { 203 throw new MetaDataAccessException( 204 "JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver", err); 205 } 206 finally { 207 DataSourceUtils.releaseConnection(con, dataSource); 208 } 209 } 210 211 221 public static Object extractDatabaseMetaData(DataSource dataSource, final String metaDataMethodName) 222 throws MetaDataAccessException { 223 224 return extractDatabaseMetaData(dataSource, 225 new DatabaseMetaDataCallback() { 226 public Object processMetaData(DatabaseMetaData dbmd) throws SQLException , MetaDataAccessException { 227 try { 228 Method method = dbmd.getClass().getMethod(metaDataMethodName, (Class []) null); 229 return method.invoke(dbmd, (Object []) null); 230 } 231 catch (NoSuchMethodException ex) { 232 throw new MetaDataAccessException("No method named '" + metaDataMethodName + 233 "' found on DatabaseMetaData instance [" + dbmd + "]", ex); 234 } 235 catch (IllegalAccessException ex) { 236 throw new MetaDataAccessException( 237 "Could not access DatabaseMetaData method '" + metaDataMethodName + "'", ex); 238 } 239 catch (InvocationTargetException ex) { 240 if (ex.getTargetException() instanceof SQLException ) { 241 throw (SQLException ) ex.getTargetException(); 242 } 243 throw new MetaDataAccessException( 244 "Invocation of DatabaseMetaData method '" + metaDataMethodName + "' failed", ex); 245 } 246 } 247 }); 248 } 249 250 261 public static boolean supportsBatchUpdates(Connection con) { 262 try { 263 DatabaseMetaData dbmd = con.getMetaData(); 264 if (dbmd != null) { 265 if (dbmd.supportsBatchUpdates()) { 266 logger.debug("JDBC driver supports batch updates"); 267 return true; 268 } 269 else { 270 logger.debug("JDBC driver does not support batch updates"); 271 } 272 } 273 } 274 catch (SQLException ex) { 275 logger.debug("JDBC driver 'supportsBatchUpdates' method threw exception", ex); 276 } 277 catch (AbstractMethodError err) { 278 logger.debug("JDBC driver does not support JDBC 2.0 'supportsBatchUpdates' method", err); 279 } 280 return false; 281 } 282 283 288 public static boolean isNumeric(int sqlType) { 289 return Types.BIT == sqlType || Types.BIGINT == sqlType || Types.DECIMAL == sqlType || 290 Types.DOUBLE == sqlType || Types.FLOAT == sqlType || Types.INTEGER == sqlType || 291 Types.NUMERIC == sqlType || Types.REAL == sqlType || Types.SMALLINT == sqlType || 292 Types.TINYINT == sqlType; 293 } 294 295 } 296 | Popular Tags |