001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc;
018:
019: import java.sql.*;
020: import javax.sql.DataSource;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * A set of Jdbc utilities.
026: *
027: * @author kimchy
028: *
029: */
030: public abstract class JdbcUtils {
031:
032: private static Log log = LogFactory.getLog(JdbcUtils.class);
033:
034: /**
035: * Returns a jdbc connection, and in case of failure, wraps the sql
036: * exception with a Jdbc device exception.
037: *
038: * @param dataSource
039: * @return A connection from the datasource.
040: * @throws JdbcGpsDeviceException
041: */
042: public static Connection getConnection(DataSource dataSource)
043: throws JdbcGpsDeviceException {
044: try {
045: return dataSource.getConnection();
046: } catch (SQLException e) {
047: throw new JdbcGpsDeviceException(
048: "Failed to open jdbc connection", e);
049: }
050: }
051:
052: /**
053: * Close the given JDBC connection and ignore any thrown exception. This is
054: * useful for typical finally blocks in manual JDBC code.
055: *
056: * @param con The JDBC Connection to close
057: */
058: public static void closeConnection(Connection con) {
059: if (con != null) {
060: try {
061: con.close();
062: } catch (SQLException ex) {
063: log.warn("Could not close JDBC Connection", ex);
064: } catch (RuntimeException ex) {
065: log
066: .error(
067: "Unexpected exception on closing JDBC Connection",
068: ex);
069: }
070: }
071: }
072:
073: /**
074: * Close the given JDBC Statement and ignore any thrown exception. This is
075: * useful for typical finally blocks in manual JDBC code.
076: *
077: * @param stmt The JDBC Statement to close
078: */
079: public static void closeStatement(Statement stmt) {
080: if (stmt != null) {
081: try {
082: stmt.close();
083: } catch (SQLException ex) {
084: log.warn("Could not close JDBC Statement", ex);
085: } catch (RuntimeException ex) {
086: log
087: .error(
088: "Unexpected exception on closing JDBC Statement",
089: ex);
090: }
091: }
092: }
093:
094: /**
095: * Close the given JDBC ResultSet and ignore any thrown exception. This is
096: * useful for typical finally blocks in manual JDBC code.
097: *
098: * @param rs the JDBC ResultSet to close
099: */
100: public static void closeResultSet(ResultSet rs) {
101: if (rs != null) {
102: try {
103: rs.close();
104: } catch (SQLException ex) {
105: log.warn("Could not close JDBC ResultSet", ex);
106: } catch (RuntimeException ex) {
107: log
108: .error(
109: "Unexpected exception on closing JDBC ResultSet",
110: ex);
111: }
112: }
113: }
114:
115: /**
116: * Returns the column index for the guven column name. Note that if there
117: * are two columns with the same name, the first onde index will be
118: * returned.
119: * <p>
120: * <code>-1</code> is returned if none is found.
121: */
122: public static int getColumnIndexFromColumnName(
123: ResultSetMetaData metaData, String columnName)
124: throws SQLException {
125: for (int i = 1; i <= metaData.getColumnCount(); i++) {
126: String tmpName = metaData.getColumnLabel(i);
127: if (tmpName.equalsIgnoreCase(columnName)) {
128: return i;
129: }
130: }
131: return -1;
132: }
133: }
|