View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp;
19  
20  import java.math.BigDecimal;
21  import java.sql.Array;
22  import java.sql.Blob;
23  import java.sql.Clob;
24  import java.sql.PreparedStatement;
25  import java.sql.Ref;
26  import java.sql.ResultSet;
27  import java.sql.ResultSetMetaData;
28  import java.sql.SQLException;
29  import java.util.Calendar;
30  /* JDBC_4_ANT_KEY_BEGIN */
31  import java.io.InputStream;
32  import java.io.Reader;
33  import java.sql.NClob;
34  import java.sql.RowId;
35  import java.sql.SQLXML;
36  /* JDBC_4_ANT_KEY_END */
37  
38  /**
39   * A base delegating implementation of {@link PreparedStatement}.
40   * <p>
41   * All of the methods from the {@link PreparedStatement} interface
42   * simply check to see that the {@link PreparedStatement} is active,
43   * and call the corresponding method on the "delegate"
44   * provided in my constructor.
45   * <p>
46   * Extends AbandonedTrace to implement Statement tracking and
47   * logging of code which created the Statement. Tracking the 
48   * Statement ensures that the Connection which created it can
49   * close any open Statement's on Connection close.
50   *
51   * @author Rodney Waldhoff
52   * @author Glenn L. Nielsen
53   * @author James House
54   * @author Dirk Verbeeck
55   * @version $Revision: 745698 $ $Date: 2009-02-18 19:28:26 -0500 (Wed, 18 Feb 2009) $
56   */
57  public class DelegatingPreparedStatement extends DelegatingStatement
58          implements PreparedStatement {
59  
60      /**
61       * Create a wrapper for the Statement which traces this
62       * Statement to the Connection which created it and the
63       * code which created it.
64       *
65       * @param s the {@link PreparedStatement} to delegate all calls to.
66       * @param c the {@link DelegatingConnection} that created this statement.
67       */
68      public DelegatingPreparedStatement(DelegatingConnection c,
69                                         PreparedStatement s) {
70          super(c, s);
71      }
72  
73      public boolean equals(Object obj) {
74          PreparedStatement delegate = (PreparedStatement) getInnermostDelegate();
75          if (delegate == null) {
76              return false;
77          }
78          if (obj instanceof DelegatingPreparedStatement) {
79              DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
80              return delegate.equals(s.getInnermostDelegate());
81          }
82          else {
83              return delegate.equals(obj);
84          }
85      }
86  
87      /** Sets my delegate. */
88      public void setDelegate(PreparedStatement s) {
89          super.setDelegate(s);
90          _stmt = s;
91      }
92  
93      public ResultSet executeQuery() throws SQLException {
94          checkOpen();
95          try {
96              return DelegatingResultSet.wrapResultSet(this,((PreparedStatement)_stmt).executeQuery());
97          }
98          catch (SQLException e) {
99              handleException(e);
100             return null;
101         }
102     }
103 
104     public int executeUpdate() throws SQLException
105     { checkOpen(); try { return ((PreparedStatement)_stmt).executeUpdate(); } catch (SQLException e) { handleException(e); return 0; } }
106 
107     public void setNull(int parameterIndex, int sqlType) throws SQLException
108     { checkOpen(); try { ((PreparedStatement)_stmt).setNull(parameterIndex,sqlType); } catch (SQLException e) { handleException(e); } }
109 
110     public void setBoolean(int parameterIndex, boolean x) throws SQLException
111     { checkOpen(); try { ((PreparedStatement)_stmt).setBoolean(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
112 
113     public void setByte(int parameterIndex, byte x) throws SQLException
114     { checkOpen(); try { ((PreparedStatement)_stmt).setByte(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
115 
116     public void setShort(int parameterIndex, short x) throws SQLException
117     { checkOpen(); try { ((PreparedStatement)_stmt).setShort(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
118 
119     public void setInt(int parameterIndex, int x) throws SQLException
120     { checkOpen(); try { ((PreparedStatement)_stmt).setInt(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
121 
122     public void setLong(int parameterIndex, long x) throws SQLException
123     { checkOpen(); try { ((PreparedStatement)_stmt).setLong(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
124 
125     public void setFloat(int parameterIndex, float x) throws SQLException
126     { checkOpen(); try { ((PreparedStatement)_stmt).setFloat(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
127 
128     public void setDouble(int parameterIndex, double x) throws SQLException
129     { checkOpen(); try { ((PreparedStatement)_stmt).setDouble(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
130 
131     public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
132     { checkOpen(); try { ((PreparedStatement)_stmt).setBigDecimal(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
133 
134     public void setString(int parameterIndex, String x) throws SQLException
135     { checkOpen(); try { ((PreparedStatement)_stmt).setString(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
136 
137     public void setBytes(int parameterIndex, byte[] x) throws SQLException
138     { checkOpen(); try { ((PreparedStatement)_stmt).setBytes(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
139 
140     public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
141     { checkOpen(); try { ((PreparedStatement)_stmt).setDate(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
142 
143     public void setTime(int parameterIndex, java.sql.Time x) throws SQLException
144     { checkOpen(); try { ((PreparedStatement)_stmt).setTime(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
145 
146     public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException
147     { checkOpen(); try { ((PreparedStatement)_stmt).setTimestamp(parameterIndex,x); } catch (SQLException e) { handleException(e); } }
148 
149     public void setAsciiStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
150     { checkOpen(); try { ((PreparedStatement)_stmt).setAsciiStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
151 
152     /** @deprecated */
153     public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
154     { checkOpen(); try { ((PreparedStatement)_stmt).setUnicodeStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
155     
156     public void setBinaryStream(int parameterIndex, java.io.InputStream x, int length) throws SQLException
157     { checkOpen(); try { ((PreparedStatement)_stmt).setBinaryStream(parameterIndex,x,length); } catch (SQLException e) { handleException(e); } }
158 
159     public void clearParameters() throws SQLException
160     { checkOpen(); try { ((PreparedStatement)_stmt).clearParameters(); } catch (SQLException e) { handleException(e); } }
161 
162     public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
163     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x, targetSqlType, scale); } catch (SQLException e) { handleException(e); } }
164 
165     public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
166     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x, targetSqlType); } catch (SQLException e) { handleException(e); } }
167 
168     public void setObject(int parameterIndex, Object x) throws SQLException
169     { checkOpen(); try { ((PreparedStatement)_stmt).setObject(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
170 
171     public boolean execute() throws SQLException
172     { checkOpen(); try { return ((PreparedStatement)_stmt).execute(); } catch (SQLException e) { handleException(e); return false; } }
173 
174     public void addBatch() throws SQLException
175     { checkOpen(); try { ((PreparedStatement)_stmt).addBatch(); } catch (SQLException e) { handleException(e); } }
176 
177     public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException
178     { checkOpen(); try { ((PreparedStatement)_stmt).setCharacterStream(parameterIndex,reader,length); } catch (SQLException e) { handleException(e); } }
179 
180     public void setRef(int i, Ref x) throws SQLException
181     { checkOpen(); try { ((PreparedStatement)_stmt).setRef(i,x); } catch (SQLException e) { handleException(e); } }
182 
183     public void setBlob(int i, Blob x) throws SQLException
184     { checkOpen(); try { ((PreparedStatement)_stmt).setBlob(i,x); } catch (SQLException e) { handleException(e); } }
185 
186     public void setClob(int i, Clob x) throws SQLException
187     { checkOpen(); try { ((PreparedStatement)_stmt).setClob(i,x); } catch (SQLException e) { handleException(e); } }
188 
189     public void setArray(int i, Array x) throws SQLException
190     { checkOpen(); try { ((PreparedStatement)_stmt).setArray(i,x); } catch (SQLException e) { handleException(e); } }
191 
192     public ResultSetMetaData getMetaData() throws SQLException
193     { checkOpen(); try { return ((PreparedStatement)_stmt).getMetaData(); } catch (SQLException e) { handleException(e); return null; } }
194 
195     public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException
196     { checkOpen(); try { ((PreparedStatement)_stmt).setDate(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
197 
198     public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException
199     { checkOpen(); try { ((PreparedStatement)_stmt).setTime(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
200 
201     public void setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException
202     { checkOpen(); try { ((PreparedStatement)_stmt).setTimestamp(parameterIndex,x,cal); } catch (SQLException e) { handleException(e); } }
203 
204     public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException
205     { checkOpen(); try { ((PreparedStatement)_stmt).setNull(paramIndex,sqlType,typeName); } catch (SQLException e) { handleException(e); } }
206 
207     /**
208      * Returns a String representation of this object.
209      *
210      * @return String 
211      * @since 1.2.2
212      */
213     public String toString() {
214     return _stmt.toString();
215     }
216 
217     // ------------------- JDBC 3.0 -----------------------------------------
218     // Will be commented by the build process on a JDBC 2.0 system
219 
220 /* JDBC_3_ANT_KEY_BEGIN */
221 
222     public void setURL(int parameterIndex, java.net.URL x) throws SQLException
223     { checkOpen(); try { ((PreparedStatement)_stmt).setURL(parameterIndex, x); } catch (SQLException e) { handleException(e); } }
224 
225     public java.sql.ParameterMetaData getParameterMetaData() throws SQLException
226     { checkOpen(); try { return ((PreparedStatement)_stmt).getParameterMetaData(); } catch (SQLException e) { handleException(e); return null; } }
227 /* JDBC_3_ANT_KEY_END */
228 /* JDBC_4_ANT_KEY_BEGIN */
229 
230     public void setRowId(int parameterIndex, RowId value) throws SQLException {
231         checkOpen();
232         try {
233             ((PreparedStatement)_stmt).setRowId(parameterIndex, value);
234         }
235         catch (SQLException e) {
236             handleException(e);
237         }
238     }
239 
240     public void setNString(int parameterIndex, String value) throws SQLException {
241         checkOpen();
242         try {
243             ((PreparedStatement)_stmt).setNString(parameterIndex, value);
244         }
245         catch (SQLException e) {
246             handleException(e);
247         }
248     }
249 
250     public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
251         checkOpen();
252         try {
253             ((PreparedStatement)_stmt).setNCharacterStream(parameterIndex, value, length);
254         }
255         catch (SQLException e) {
256             handleException(e);
257         }
258     }
259 
260     public void setNClob(int parameterIndex, NClob value) throws SQLException {
261         checkOpen();
262         try {
263             ((PreparedStatement)_stmt).setNClob(parameterIndex, value);
264         }
265         catch (SQLException e) {
266             handleException(e);
267         }
268     }
269 
270     public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
271         checkOpen();
272         try {
273             ((PreparedStatement)_stmt).setClob(parameterIndex, reader, length);
274         }
275         catch (SQLException e) {
276             handleException(e);
277         }
278     }
279 
280     public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
281         checkOpen();
282         try {
283             ((PreparedStatement)_stmt).setBlob(parameterIndex, inputStream, length);
284         }
285         catch (SQLException e) {
286             handleException(e);
287         }
288     }
289 
290     public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
291         checkOpen();
292         try {
293             ((PreparedStatement)_stmt).setNClob(parameterIndex, reader, length);
294         }
295         catch (SQLException e) {
296             handleException(e);
297         }
298     }
299 
300     public void setSQLXML(int parameterIndex, SQLXML value) throws SQLException {
301         checkOpen();
302         try {
303             ((PreparedStatement)_stmt).setSQLXML(parameterIndex, value);
304         }
305         catch (SQLException e) {
306             handleException(e);
307         }
308     }
309 
310     public void setAsciiStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
311         checkOpen();
312         try {
313             ((PreparedStatement)_stmt).setAsciiStream(parameterIndex, inputStream, length);
314         }
315         catch (SQLException e) {
316             handleException(e);
317         }
318     }
319 
320     public void setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
321         checkOpen();
322         try {
323             ((PreparedStatement)_stmt).setBinaryStream(parameterIndex, inputStream, length);
324         }
325         catch (SQLException e) {
326             handleException(e);
327         }
328     }
329 
330     public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
331         checkOpen();
332         try {
333             ((PreparedStatement)_stmt).setCharacterStream(parameterIndex, reader, length);
334         }
335         catch (SQLException e) {
336             handleException(e);
337         }
338     }
339 
340     public void setAsciiStream(int parameterIndex, InputStream inputStream) throws SQLException {
341         checkOpen();
342         try {
343             ((PreparedStatement)_stmt).setAsciiStream(parameterIndex, inputStream);
344         }
345         catch (SQLException e) {
346             handleException(e);
347         }
348     }
349 
350     public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
351         checkOpen();
352         try {
353             ((PreparedStatement)_stmt).setBinaryStream(parameterIndex, inputStream);
354         }
355         catch (SQLException e) {
356             handleException(e);
357         }
358     }
359 
360     public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
361         checkOpen();
362         try {
363             ((PreparedStatement)_stmt).setCharacterStream(parameterIndex, reader);
364         }
365         catch (SQLException e) {
366             handleException(e);
367         }
368     }
369 
370     public void setNCharacterStream(int parameterIndex, Reader reader) throws SQLException {
371         checkOpen();
372         try {
373             ((PreparedStatement)_stmt).setNCharacterStream(parameterIndex, reader);
374         }
375         catch (SQLException e) {
376             handleException(e);
377         }
378     }
379 
380     public void setClob(int parameterIndex, Reader reader) throws SQLException {
381         checkOpen();
382         try {
383             ((PreparedStatement)_stmt).setClob(parameterIndex, reader);
384         }
385         catch (SQLException e) {
386             handleException(e);
387         }
388     }
389 
390     public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
391         checkOpen();
392         try {
393             ((PreparedStatement)_stmt).setBlob(parameterIndex, inputStream);
394         }
395         catch (SQLException e) {
396             handleException(e);
397         }
398     }
399 
400     public void setNClob(int parameterIndex, Reader reader) throws SQLException {
401         checkOpen();
402         try {
403             ((PreparedStatement)_stmt).setNClob(parameterIndex, reader);
404         }
405         catch (SQLException e) {
406             handleException(e);
407         }
408     }
409 /* JDBC_4_ANT_KEY_END */
410 }