KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > util > DataAccess


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.exchange.util;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import javax.sql.DataSource JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import sync4j.exchange.DataAccessException;
32
33 /**
34  * This class provide methods
35  * to access database source
36  *
37  * @author Fabio Maggi @ Funambol
38  * @version $Id: DataAccess.java,v 1.10 2005/01/19 11:37:48 fabius Exp $
39  **/

40 public class DataAccess {
41
42     // ------------------------------------------------------------------------- Constants
43

44     private static final String JavaDoc JNDI_DATASOURCE_NAME_BUNDLE = "java:comp/env/jdbc/sync4j";
45     private static final String JavaDoc JNDI_DATASOURCE_NAME_JBOSS = "java:/jdbc/sync4j" ;
46
47
48     // ------------------------------------------------------------------------- Private data
49

50     private String JavaDoc jndiDataSourceName = null;
51
52     // ------------------------------------------------------------------------- Protected data
53

54     /**
55      * Data store to use for database connections
56      */

57     protected transient DataSource JavaDoc dataSource = null;
58
59     // ------------------------------------------------------------------------- Costructor
60

61     /** Creates a new instance of DataAccess */
62     public DataAccess()
63     throws DataAccessException {
64         setJndiDataSourceName();
65     }
66
67     // ------------------------------------------------------------------------- Public methods
68

69     public void setJndiDataSourceName()
70     throws DataAccessException {
71
72         try {
73
74             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
75
76             try {
77                 jndiDataSourceName = JNDI_DATASOURCE_NAME_JBOSS ;
78                 dataSource = (DataSource JavaDoc) ctx.lookup(jndiDataSourceName ) ;
79             } catch (NameNotFoundException JavaDoc e) {
80                 jndiDataSourceName = JNDI_DATASOURCE_NAME_BUNDLE ;
81                 dataSource = (DataSource JavaDoc) ctx.lookup(jndiDataSourceName ) ;
82             }
83
84         } catch (NamingException JavaDoc e) {
85             throw new DataAccessException ("Data source "
86             + jndiDataSourceName
87             + " not found "
88             + e.toString()
89             );
90         }
91     }
92
93     /**
94      * @return database connection
95      *
96      * @throws DataAccessException
97      **/

98     public Connection JavaDoc getConnection ()
99     throws DataAccessException {
100
101         Connection JavaDoc con = null;
102
103         try {
104
105             con = this.dataSource.getConnection();
106
107         } catch (SQLException JavaDoc e) {
108             throw new DataAccessException
109                 ("Error creating connection: " + e.getMessage());
110         }
111
112         return con;
113
114     }
115
116     /**
117      * cleanUp database connection
118      *
119      * @param con coonection
120      * @param ps preparement statement
121      * @param rs resultset
122      *
123      * @throws DataAccessException
124      **/

125     public void cleanUp (Connection JavaDoc con ,
126                          PreparedStatement JavaDoc ps ,
127                          ResultSet JavaDoc rs )
128     throws DataAccessException {
129
130         try {
131
132             if (con != null) {
133                 con.close();
134                 con = null;
135             }
136
137             if (ps != null) {
138                 ps.close();
139                 ps = null;
140             }
141
142             if (rs != null) {
143                 rs.close();
144                 rs = null;
145             }
146
147         } catch (SQLException JavaDoc e) {
148             throw new DataAccessException
149                 ("Error cleaning up connection: " + e.getMessage());
150         }
151
152     }
153
154 }
Popular Tags