KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > QueryHelper


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.spi.MockParametersCallbacks;
19 import scriptella.spi.QueryCallback;
20 import scriptella.util.IOUtils;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25
26 /**
27  * Query abstraction to for tests.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 public class QueryHelper {
33     private String JavaDoc sql;
34
35     public QueryHelper(String JavaDoc sql) {
36         this.sql = sql;
37     }
38
39     /**
40      * Executes a query for connection.
41      * @param con connection to use.
42      * @param callback callback to call for rows being iterated.
43      */

44     public void execute(Connection JavaDoc con, QueryCallback callback) {
45         Statement JavaDoc st = null;
46         ResultSetAdapter ra = null;
47         try {
48             st=con.createStatement();
49             ra = new ResultSetAdapter(st.executeQuery(sql),
50                     MockParametersCallbacks.UNSUPPORTED, new JdbcTypesConverter());
51             while (ra.next()) {
52                 callback.processRow(ra);
53             }
54         } catch (SQLException JavaDoc e) {
55             throw new IllegalStateException JavaDoc(e.getMessage(), e);
56         } finally {
57             IOUtils.closeSilently(ra);
58             JdbcUtils.closeSilent(st);
59         }
60
61     }
62
63     protected void onSQLException(SQLException JavaDoc e) {
64         throw new IllegalStateException JavaDoc(e.getMessage(), e);
65     }
66 }
67
Popular Tags