001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.dao;
022:
023: import com.liferay.util.JNDIUtil;
024:
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029:
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032:
033: import javax.sql.DataSource;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * <a href="DataAccess.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class DataAccess {
045:
046: public static Connection getConnection(String location)
047: throws NamingException, SQLException {
048:
049: InitialContext ctx = new InitialContext();
050:
051: DataSource ds = (DataSource) JNDIUtil.lookup(ctx, location);
052:
053: Connection con = ds.getConnection();
054:
055: return con;
056: }
057:
058: public static void cleanUp(Connection con) {
059: cleanUp(con, null, null);
060: }
061:
062: public static void cleanUp(Connection con, Statement s) {
063: cleanUp(con, s, null);
064: }
065:
066: public static void cleanUp(Connection con, Statement s, ResultSet rs) {
067: try {
068: if (rs != null) {
069: rs.close();
070: }
071: } catch (SQLException sqle) {
072: if (_log.isWarnEnabled()) {
073: _log.warn(sqle.getMessage());
074: }
075: }
076:
077: try {
078: if (s != null) {
079: s.close();
080: }
081: } catch (SQLException sqle) {
082: if (_log.isWarnEnabled()) {
083: _log.warn(sqle.getMessage());
084: }
085: }
086:
087: try {
088: if (con != null) {
089: con.close();
090: }
091: } catch (SQLException sqle) {
092: if (_log.isWarnEnabled()) {
093: _log.warn(sqle.getMessage());
094: }
095: }
096: }
097:
098: private static Log _log = LogFactory.getLog(DataAccess.class);
099:
100: }
|