1
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
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 }