KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > query > block > BlockQueryJDBCTemp


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

16 package com.jdon.model.query.block;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.sql.DataSource JavaDoc;
26
27 import org.apache.log4j.Logger;
28
29 import com.jdon.model.query.JdbcUtil;
30 import com.jdon.model.query.cache.QueryConditonDatakey;
31 import com.jdon.util.DbUtil;
32
33 /**
34  * Batch query JDBC Template
35  *
36  * this class can be directly by application system.
37  *
38  * @author <a HREF="mailto:banqiao@jdon.com">banq </a>
39  *
40  */

41 public class BlockQueryJDBCTemp implements BlockQueryJDBC {
42     private final static Logger logger = Logger.getLogger(BlockQueryJDBCTemp.class);
43
44     private DataSource JavaDoc dataSource;
45
46     private JdbcUtil jdbcUtil;
47
48     public BlockQueryJDBCTemp(DataSource JavaDoc dataSource) {
49         this.dataSource = dataSource;
50         this.jdbcUtil = new JdbcUtil();
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see com.jdon.model.query.PageIteratorJDBC#fetchDataAllCount(java.lang.Object,
57      * java.lang.String)
58      */

59     public int fetchDataAllCount(QueryConditonDatakey qcd) {
60         logger.debug("[JdonFramework]--> execute fetch all count for sql sentence: " + qcd.getSqlquery());
61         Connection JavaDoc c = null;
62         PreparedStatement JavaDoc ps = null;
63         ResultSet JavaDoc rs = null;
64         int ret = 0;
65         try {
66             c = dataSource.getConnection();
67             ps = c.prepareStatement(qcd.getSqlquery(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
68
69             jdbcUtil.setQueryParams(qcd.getQueryParams(), ps);
70
71             rs = ps.executeQuery();
72             if (rs.first()) {
73                 ret = rs.getInt(1);
74             }
75         } catch (SQLException JavaDoc se) {
76             logger.error(se);
77         } catch (Exception JavaDoc ex) {
78             logger.error(ex);
79         } finally {
80             try {
81                 if (rs != null)
82                     rs.close();
83                 if (ps != null)
84                     ps.close();
85                 if (c != null)
86                     c.close();
87             } catch (Exception JavaDoc ex) {
88             }
89         }
90         logger.debug("[JdonFramework]--> fetchDataAllCount is" + ret);
91         return ret;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see com.jdon.model.query.PageIteratorJDBC#fetchDatas(java.util.Collection,
98      * java.lang.String, int, int)
99      */

100     public List JavaDoc fetchDatas(QueryConditonDatakey qcdk) {
101         logger.debug("[JdonFramework]--> fetch the primary key collection, sql sentence: " + qcdk.getSQlKey());
102         Connection JavaDoc c = null;
103         PreparedStatement JavaDoc ps = null;
104         ResultSet JavaDoc rs = null;
105
106         int blockStart = qcdk.getBlockStart();
107         int blockSize = qcdk.getBlockSize();
108         logger.debug("[JdonFramework]--> blockStart=" + blockStart + " blockSize=" + blockSize);
109         List JavaDoc items = new ArrayList JavaDoc(blockSize);
110         try {
111             c = dataSource.getConnection();
112
113             DbUtil.testConnection(c);
114             ps = c.prepareStatement(qcdk.getSqlquery(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
115
116             jdbcUtil.setQueryParams(qcdk.getQueryParams(), ps);
117
118             rs = ps.executeQuery();
119             if (DbUtil.supportsFetchSize)
120                 rs.setFetchSize(blockSize);
121             // Many JDBC drivers don't implement scrollable cursors the real
122
// way, but instead load all results into memory. Looping through
123
// the results ourselves is more efficient.
124
for (int i = 0; i < blockStart; i++) {
125                 if (!rs.next()) break;
126             }
127             blockSize ++;
128             while(rs.next() && (--blockSize > 0)) {
129                 Object JavaDoc result = rs.getObject(1);
130                 logger.debug("[JdonFramework]--> found a primary key = " + result + ", type:" + result.getClass().getName());
131                 items.add(result);
132             }
133
134             logger.debug("[JdonFramework]--> get a result succefully ..");
135         } catch (SQLException JavaDoc se) {
136             logger.error(se);
137
138         } catch (Exception JavaDoc ex) {
139             logger.error(ex);
140
141         } finally {
142             try {
143                 if (rs != null)
144                     rs.close();
145                 if (ps != null)
146                     ps.close();
147                 if (c != null)
148                     c.close();
149             } catch (Exception JavaDoc ex) {
150             }
151         }
152         return items;
153
154     }
155
156 }
157
Popular Tags