1 /***
2 * @(#)JdbcServiceImpl.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.persistence;
26
27 import java.sql.Connection;
28 import java.sql.SQLException;
29
30 import javax.sql.DataSource;
31
32 import org.apache.commons.dbutils.QueryRunner;
33 import org.apache.commons.dbutils.ResultSetHandler;
34 import org.huihoo.jfox.soaf.exception.DAOException;
35
36 /***
37 * <p>
38 * JDBC persistence service implementation.
39 * </p>
40 *
41 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
42 * @version $Revision: 1.10 $ $Date: 2005/08/19 11:29:21 $
43 * @version Revision: 1.0
44 */
45
46 public class JdbcServiceImpl implements JdbcService {
47
48 private QueryRunner queryRunner;
49
50 /***
51 * @param dataSource
52 */
53 public JdbcServiceImpl() {
54 queryRunner = new QueryRunner();
55 }
56
57 /***
58 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#setDataSource(javax.sql.DataSource)
59 */
60 public void setDataSource(DataSource dataSource) {
61 queryRunner.setDataSource(dataSource);
62 }
63
64 /***
65 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.sql.Connection,
66 * java.lang.String, java.lang.Object,
67 * org.apache.commons.dbutils.ResultSetHandler)
68 */
69 public Object query(Connection conn, String sql, Object param,
70 ResultSetHandler rsh) throws DAOException {
71 try {
72 return queryRunner.query(conn, sql, param, rsh);
73 } catch (SQLException e) {
74 throw new DAOException("Failed to query - sql [" + sql
75 + "], parameterObject [" + param + "]. Cause: " + e, e);
76 }
77 }
78
79 /***
80 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.sql.Connection,
81 * java.lang.String, java.lang.Object[],
82 * org.apache.commons.dbutils.ResultSetHandler)
83 */
84 public Object query(Connection conn, String sql, Object[] params,
85 ResultSetHandler rsh) throws DAOException {
86 try {
87 return queryRunner.query(conn, sql, params, rsh);
88 } catch (SQLException e) {
89 throw new DAOException("Failed to query - sql [" + sql
90 + "]. Cause: " + e, e);
91 }
92 }
93
94 /***
95 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.sql.Connection,
96 * java.lang.String, org.apache.commons.dbutils.ResultSetHandler)
97 */
98 public Object query(Connection conn, String sql, ResultSetHandler rsh)
99 throws DAOException {
100 try {
101 return queryRunner.query(conn, sql, rsh);
102 } catch (SQLException e) {
103 throw new DAOException("Failed to query - sql [" + sql
104 + "]. Cause: " + e, e);
105 }
106 }
107
108 /***
109 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.lang.String,
110 * java.lang.Object, org.apache.commons.dbutils.ResultSetHandler)
111 */
112 public Object query(String sql, Object param, ResultSetHandler rsh)
113 throws DAOException {
114 try {
115 return queryRunner.query(sql, param, rsh);
116 } catch (SQLException e) {
117 throw new DAOException("Failed to query - sql [" + sql
118 + "]. Cause: " + e, e);
119 }
120 }
121
122 /***
123 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.lang.String,
124 * java.lang.Object[], org.apache.commons.dbutils.ResultSetHandler)
125 */
126 public Object query(String sql, Object[] params, ResultSetHandler rsh)
127 throws DAOException {
128 try {
129 return queryRunner.query(sql, params, rsh);
130 } catch (SQLException e) {
131 throw new DAOException("Failed to query - sql [" + sql
132 + "]. Cause: " + e, e);
133 }
134 }
135
136 /***
137 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#query(java.lang.String,
138 * org.apache.commons.dbutils.ResultSetHandler)
139 */
140 public Object query(String sql, ResultSetHandler rsh) throws DAOException {
141 try {
142 return queryRunner.query(sql, rsh);
143 } catch (SQLException e) {
144 throw new DAOException("Failed to query - sql [" + sql
145 + "]. Cause: " + e, e);
146 }
147 }
148
149 /***
150 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.sql.Connection,
151 * java.lang.String, java.lang.Object)
152 */
153 public int update(Connection conn, String sql, Object param)
154 throws DAOException {
155 try {
156 return queryRunner.update(conn, sql);
157 } catch (SQLException e) {
158 throw new DAOException("Failed to query - sql [" + sql
159 + "]. Cause: " + e, e);
160 }
161 }
162
163 /***
164 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.sql.Connection,
165 * java.lang.String, java.lang.Object[])
166 */
167 public int update(Connection conn, String sql, Object[] params)
168 throws DAOException {
169 try {
170 return queryRunner.update(conn, sql, conn);
171 } catch (SQLException e) {
172 throw new DAOException("Failed to query - sql [" + sql
173 + "]. Cause: " + e, e);
174 }
175 }
176
177 /***
178 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.sql.Connection,
179 * java.lang.String)
180 */
181 public int update(Connection conn, String sql) throws DAOException {
182 try {
183 return queryRunner.update(sql);
184 } catch (SQLException e) {
185 throw new DAOException("Failed to query - sql [" + sql
186 + "]. Cause: " + e, e);
187 }
188 }
189
190 /***
191 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.lang.String,
192 * java.lang.Object)
193 */
194 public int update(String sql, Object param) throws DAOException {
195 try {
196 return queryRunner.update(sql, param);
197 } catch (SQLException e) {
198 throw new DAOException("Failed to query - sql [" + sql
199 + "]. Cause: " + e, e);
200 }
201 }
202
203 /***
204 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.lang.String,
205 * java.lang.Object[])
206 */
207 public int update(String sql, Object[] params) throws DAOException {
208 try {
209 return queryRunner.update(sql, params);
210 } catch (SQLException e) {
211 throw new DAOException("Failed to query - sql [" + sql
212 + "]. Cause: " + e, e);
213 }
214 }
215
216 /***
217 * @see org.huihoo.jfox.soaf.services.persistence.JdbcService#update(java.lang.String)
218 */
219 public int update(String sql) throws DAOException {
220 try {
221 return queryRunner.update(sql);
222 } catch (SQLException e) {
223 throw new DAOException("Failed to query - sql [" + sql
224 + "]. Cause: " + e, e);
225 }
226 }
227 }