KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > query > JRJdbcQueryExecuter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.query;
29
30 import java.math.BigDecimal JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Types JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import net.sf.jasperreports.engine.JRDataSource;
40 import net.sf.jasperreports.engine.JRDataset;
41 import net.sf.jasperreports.engine.JRException;
42 import net.sf.jasperreports.engine.JRParameter;
43 import net.sf.jasperreports.engine.JRResultSetDataSource;
44 import net.sf.jasperreports.engine.JRValueParameter;
45 import net.sf.jasperreports.engine.util.JRProperties;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50
51 /**
52  * JDBC query executer for SQL queries.
53  * <p/>
54  * This query executer implementation offers built-in support for SQL queries.
55  *
56  * @author Teodor Danciu (teodord@users.sourceforge.net)
57  * @version $Id: JRJdbcQueryExecuter.java 1458 2006-11-06 12:27:37 +0200 (Mon, 06 Nov 2006) lucianc $
58  */

59 public class JRJdbcQueryExecuter extends JRAbstractQueryExecuter
60 {
61     private static final Log log = LogFactory.getLog(JRJdbcQueryExecuter.class);
62
63
64     private Connection JavaDoc connection;
65     
66     /**
67      * The statement used to fire the query.
68      */

69     private PreparedStatement JavaDoc statement;
70
71     private ResultSet JavaDoc resultSet;
72
73     
74     public JRJdbcQueryExecuter(JRDataset dataset, Map JavaDoc parameters)
75     {
76         super(dataset, parameters);
77         
78         connection = (Connection JavaDoc) getParameterValue(JRParameter.REPORT_CONNECTION);
79
80         if (connection == null)
81         {
82             if (log.isWarnEnabled())
83                 log.warn("The supplied java.sql.Connection object is null.");
84         }
85         
86         parseQuery();
87     }
88
89     
90     protected String JavaDoc getParameterReplacement(String JavaDoc parameterName)
91     {
92         return "?";
93     }
94
95     
96     /* (non-Javadoc)
97      * @see net.sf.jasperreports.engine.util.JRQueryExecuter#createDatasource()
98      */

99     public JRDataSource createDatasource() throws JRException
100     {
101         JRDataSource dataSource = null;
102         
103         createStatement();
104         
105         if (statement != null)
106         {
107             try
108             {
109                 Integer JavaDoc reportMaxCount = (Integer JavaDoc) getParameterValue(JRParameter.REPORT_MAX_COUNT);
110                 if (reportMaxCount != null)
111                 {
112                     statement.setMaxRows(reportMaxCount.intValue());
113                 }
114
115                 resultSet = statement.executeQuery();
116                 
117                 dataSource = new JRResultSetDataSource(resultSet);
118             }
119             catch (SQLException JavaDoc e)
120             {
121                 throw new JRException("Error executing SQL statement for : " + dataset.getName(), e);
122             }
123         }
124         
125         return dataSource;
126     }
127     
128     
129     private void createStatement() throws JRException
130     {
131         String JavaDoc queryString = getQueryString();
132         
133         if (log.isDebugEnabled())
134         {
135             log.debug("SQL query string: " + queryString);
136         }
137         
138         if (connection != null && queryString != null && queryString.trim().length() > 0)
139         {
140             try
141             {
142                 statement = connection.prepareStatement(queryString);
143                 
144                 int fetchSize = JRProperties.getIntegerProperty(dataset.getPropertiesMap(),
145                         JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE,
146                         0);
147                 if (fetchSize > 0)
148                 {
149                     statement.setFetchSize(fetchSize);
150                 }
151                 
152                 List JavaDoc parameterNames = getCollectedParameterNames();
153                 if (!parameterNames.isEmpty())
154                 {
155                     for(int i = 0; i < parameterNames.size(); i++)
156                     {
157                         String JavaDoc parameterName = (String JavaDoc)parameterNames.get(i);
158                         setStatementParameter(i + 1, parameterName);
159                     }
160                 }
161             }
162             catch (SQLException JavaDoc e)
163             {
164                 throw new JRException("Error preparing statement for executing the report query : " + "\n\n" + queryString + "\n\n", e);
165             }
166         }
167     }
168
169
170     protected void setStatementParameter(int parameterIndex, String JavaDoc parameterName) throws SQLException JavaDoc, JRException
171     {
172         JRValueParameter parameter = getValueParameter(parameterName);
173         Class JavaDoc clazz = parameter.getValueClass();
174         Object JavaDoc parameterValue = parameter.getValue();
175         
176         if (log.isDebugEnabled())
177         {
178             log.debug("Parameter #" + parameterIndex + " (" + parameterName + " of type " + clazz.getName() + "): " + parameterValue);
179         }
180
181         if ( clazz.equals(java.lang.Object JavaDoc.class) )
182         {
183             if (parameterValue == null)
184             {
185                 statement.setNull(parameterIndex, Types.JAVA_OBJECT);
186             }
187             else
188             {
189                 statement.setObject(parameterIndex, parameterValue);
190             }
191         }
192         else if ( clazz.equals(java.lang.Boolean JavaDoc.class) )
193         {
194             if (parameterValue == null)
195             {
196                 statement.setNull(parameterIndex, Types.BIT);
197             }
198             else
199             {
200                 statement.setBoolean(parameterIndex, ((Boolean JavaDoc)parameterValue).booleanValue());
201             }
202         }
203         else if ( clazz.equals(java.lang.Byte JavaDoc.class) )
204         {
205             if (parameterValue == null)
206             {
207                 statement.setNull(parameterIndex, Types.TINYINT);
208             }
209             else
210             {
211                 statement.setByte(parameterIndex, ((Byte JavaDoc)parameterValue).byteValue());
212             }
213         }
214         else if ( clazz.equals(java.lang.Double JavaDoc.class) )
215         {
216             if (parameterValue == null)
217             {
218                 statement.setNull(parameterIndex, Types.DOUBLE);
219             }
220             else
221             {
222                 statement.setDouble(parameterIndex, ((Double JavaDoc)parameterValue).doubleValue());
223             }
224         }
225         else if ( clazz.equals(java.lang.Float JavaDoc.class) )
226         {
227             if (parameterValue == null)
228             {
229                 statement.setNull(parameterIndex, Types.FLOAT);
230             }
231             else
232             {
233                 statement.setFloat(parameterIndex, ((Float JavaDoc)parameterValue).floatValue());
234             }
235         }
236         else if ( clazz.equals(java.lang.Integer JavaDoc.class) )
237         {
238             if (parameterValue == null)
239             {
240                 statement.setNull(parameterIndex, Types.INTEGER);
241             }
242             else
243             {
244                 statement.setInt(parameterIndex, ((Integer JavaDoc)parameterValue).intValue());
245             }
246         }
247         else if ( clazz.equals(java.lang.Long JavaDoc.class) )
248         {
249             if (parameterValue == null)
250             {
251                 statement.setNull(parameterIndex, Types.BIGINT);
252             }
253             else
254             {
255                 statement.setLong(parameterIndex, ((Long JavaDoc)parameterValue).longValue());
256             }
257         }
258         else if ( clazz.equals(java.lang.Short JavaDoc.class) )
259         {
260             if (parameterValue == null)
261             {
262                 statement.setNull(parameterIndex, Types.SMALLINT);
263             }
264             else
265             {
266                 statement.setShort(parameterIndex, ((Short JavaDoc)parameterValue).shortValue());
267             }
268         }
269         else if ( clazz.equals(java.math.BigDecimal JavaDoc.class) )
270         {
271             if (parameterValue == null)
272             {
273                 statement.setNull(parameterIndex, Types.DECIMAL);
274             }
275             else
276             {
277                 statement.setBigDecimal(parameterIndex, (BigDecimal JavaDoc)parameterValue);
278             }
279         }
280         else if ( clazz.equals(java.lang.String JavaDoc.class) )
281         {
282             if (parameterValue == null)
283             {
284                 statement.setNull(parameterIndex, Types.VARCHAR);
285             }
286             else
287             {
288                 statement.setString(parameterIndex, parameterValue.toString());
289             }
290         }
291         else if ( clazz.equals(java.util.Date JavaDoc.class) )
292         {
293             if (parameterValue == null)
294             {
295                 statement.setNull(parameterIndex, Types.DATE);
296             }
297             else
298             {
299                 statement.setDate( parameterIndex, new java.sql.Date JavaDoc( ((java.util.Date JavaDoc)parameterValue).getTime() ) );
300             }
301         }
302         else if ( clazz.equals(java.sql.Timestamp JavaDoc.class) )
303         {
304             if (parameterValue == null)
305             {
306                 statement.setNull(parameterIndex, Types.TIMESTAMP);
307             }
308             else
309             {
310                 statement.setTimestamp( parameterIndex, (java.sql.Timestamp JavaDoc)parameterValue );
311             }
312         }
313         else if ( clazz.equals(java.sql.Time JavaDoc.class) )
314         {
315             if (parameterValue == null)
316             {
317                 statement.setNull(parameterIndex, Types.TIME);
318             }
319             else
320             {
321                 statement.setTime( parameterIndex, (java.sql.Time JavaDoc)parameterValue );
322             }
323         }
324         else
325         {
326             throw new JRException("Parameter type not supported in query : " + parameterName + " class " + clazz.getName());
327         }
328     }
329
330     
331     /* (non-Javadoc)
332      * @see net.sf.jasperreports.engine.util.JRQueryExecuter#close()
333      */

334     public synchronized void close()
335     {
336         if (resultSet != null)
337         {
338             try
339             {
340                 resultSet.close();
341             }
342             catch (SQLException JavaDoc e)
343             {
344                 log.error("Error while closing result set.", e);
345             }
346             finally
347             {
348                 resultSet = null;
349             }
350         }
351         
352         if (statement != null)
353         {
354             try
355             {
356                 statement.close();
357             }
358             catch (SQLException JavaDoc e)
359             {
360                 log.error("Error while closing statement.", e);
361             }
362             finally
363             {
364                 statement = null;
365             }
366         }
367     }
368     
369     /* (non-Javadoc)
370      * @see net.sf.jasperreports.engine.util.JRQueryExecuter#cancelQuery()
371      */

372     public synchronized boolean cancelQuery() throws JRException
373     {
374         if (statement != null)
375         {
376             try
377             {
378                 statement.cancel();
379                 return true;
380             }
381             catch (Throwable JavaDoc t)
382             {
383                 throw new JRException("Error cancelling SQL statement", t);
384             }
385         }
386         
387         return false;
388     }
389 }
390
Popular Tags