Java tutorial
package jdbc; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.sql.*; /* * Copyright 2002-2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public abstract class JdbcUtils { /** * Constant that indicates an unknown (or unspecified) SQL type. * * @see java.sql.Types */ public static final int TYPE_UNKNOWN = Integer.MIN_VALUE; private static final Log logger = LogFactory.getLog(JdbcUtils.class); /** * Close the given JDBC Connection and ignore any thrown exception. This is * useful for typical finally blocks in manual JDBC code. * * @param conn * the JDBC Connection to close (may be <code>null</code>) */ public static void closeConnection(Connection conn) { if (conn != null) { try { conn.close(); conn = null; } catch (SQLException ex) { logger.error("Could not close JDBC Connection", ex); } catch (Throwable ex) { // We don't trust the JDBC driver: It might throw // RuntimeException or Error. logger.error("Unexpected exception on closing JDBC Connection", ex); } } } /** * Close the given JDBC Statement and ignore any thrown exception. This is * useful for typical finally blocks in manual JDBC code. * * @param stmt * the JDBC Statement to close (may be <code>null</code>) */ public static void closeStatement(Statement stmt) { if (stmt != null) { try { stmt.close(); stmt = null; } catch (SQLException ex) { logger.warn("Could not close JDBC Statement", ex); } catch (Throwable ex) { // We don't trust the JDBC driver: It might throw // RuntimeException or Error. logger.error("Unexpected exception on closing JDBC Statement", ex); } } } /** * Close the given JDBC ResultSet and ignore any thrown exception. This is * useful for typical finally blocks in manual JDBC code. * * @param rs * the JDBC ResultSet to close (may be <code>null</code>) */ public static void closeResultSet(ResultSet rs) { if (rs != null) { try { rs.close(); rs = null; } catch (SQLException ex) { logger.warn("Could not close JDBC ResultSet", ex); } catch (Throwable ex) { // We don't trust the JDBC driver: It might throw // RuntimeException or Error. logger.error("Unexpected exception on closing JDBC ResultSet", ex); } } } /** * Retrieve a JDBC column value from a ResultSet, using the most appropriate * value type. The returned value should be a detached value object, not * having any ties to the active ResultSet: in particular, it should not be * a Blob or Clob object but rather a byte array respectively String * representation. * <p> * Uses the <code>getObject(index)</code> method, but includes additional * "hacks" to get around Oracle 10g returning a non-standard object for its * TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns * leaving out the time portion: These columns will explicitly be extracted * as standard <code>java.sql.Timestamp</code> object. * * @param rs * is the ResultSet holding the data * @param index * is the column index * @return the value object * @see java.sql.Blob * @see java.sql.Clob * @see java.sql.Timestamp * @see oracle.sql.TIMESTAMP */ public static Object getResultSetValue(ResultSet rs, int index) throws SQLException { Object obj = rs.getObject(index); if (obj instanceof Blob) { obj = rs.getBytes(index); } else if (obj instanceof Clob) { obj = rs.getString(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) { obj = rs.getTimestamp(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) { String metaDataClassName = rs.getMetaData().getColumnClassName(index); if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) { obj = rs.getTimestamp(index); } else { obj = rs.getDate(index); } } else if (obj != null && obj instanceof Date) { if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) { obj = rs.getTimestamp(index); } } return obj; } /** * Return whether the given JDBC driver supports JDBC 2.0 batch updates. * <p> * Typically invoked right before execution of a given set of statements: to * decide whether the set of SQL statements should be executed through the * JDBC 2.0 batch mechanism or simply in a traditional one-by-one fashion. * <p> * Logs a warning if the "supportsBatchUpdates" methods throws an exception * and simply returns false in that case. * * @param con * the Connection to check * @return whether JDBC 2.0 batch updates are supported * @see java.sql.DatabaseMetaData#supportsBatchUpdates */ public static boolean supportsBatchUpdates(Connection con) { try { DatabaseMetaData dbmd = con.getMetaData(); if (dbmd != null) { if (dbmd.supportsBatchUpdates()) { if (logger.isDebugEnabled()) { logger.debug("JDBC driver supports batch updates"); } return true; } else { if (logger.isDebugEnabled()) { logger.debug("JDBC driver does not support batch updates"); } } } } catch (SQLException ex) { logger.warn("JDBC driver 'supportsBatchUpdates' method threw exception", ex); } catch (AbstractMethodError err) { logger.warn("JDBC driver does not support JDBC 2.0 'supportsBatchUpdates' method", err); } return false; } }