1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.dao;
24  
25  import com.liferay.util.JNDIUtil;
26  
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Statement;
31  
32  import javax.naming.InitialContext;
33  import javax.naming.NamingException;
34  
35  import javax.sql.DataSource;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="DataAccess.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class DataAccess {
47  
48      public static Connection getConnection(String location)
49          throws NamingException, SQLException {
50  
51          InitialContext ctx = new InitialContext();
52  
53          DataSource ds = (DataSource)JNDIUtil.lookup(ctx, location);
54  
55          Connection con = ds.getConnection();
56  
57          return con;
58      }
59  
60      public static void cleanUp(Connection con) {
61          cleanUp(con, null, null);
62      }
63  
64      public static void cleanUp(Connection con, Statement s) {
65          cleanUp(con, s, null);
66      }
67  
68      public static void cleanUp(Connection con, Statement s, ResultSet rs) {
69          try {
70              if (rs != null) {
71                  rs.close();
72              }
73          }
74          catch (SQLException sqle) {
75              if (_log.isWarnEnabled()) {
76                  _log.warn(sqle.getMessage());
77              }
78          }
79  
80          try {
81              if (s != null) {
82                  s.close();
83              }
84          }
85          catch (SQLException sqle) {
86              if (_log.isWarnEnabled()) {
87                  _log.warn(sqle.getMessage());
88              }
89          }
90  
91          try {
92              if (con != null) {
93                  con.close();
94              }
95          }
96          catch (SQLException sqle) {
97              if (_log.isWarnEnabled()) {
98                  _log.warn(sqle.getMessage());
99              }
100         }
101     }
102 
103     private static Log _log = LogFactory.getLog(DataAccess.class);
104 
105 }