KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > DelegatingPreparedStatement


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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
17 package org.apache.commons.dbcp;
18
19 import java.math.BigDecimal JavaDoc;
20 import java.sql.Array JavaDoc;
21 import java.sql.Blob JavaDoc;
22 import java.sql.Clob JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.Ref JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Calendar JavaDoc;
29
30 /**
31  * A base delegating implementation of {@link PreparedStatement}.
32  * <p>
33  * All of the methods from the {@link PreparedStatement} interface
34  * simply check to see that the {@link PreparedStatement} is active,
35  * and call the corresponding method on the "delegate"
36  * provided in my constructor.
37  * <p>
38  * Extends AbandonedTrace to implement Statement tracking and
39  * logging of code which created the Statement. Tracking the
40  * Statement ensures that the Connection which created it can
41  * close any open Statement's on Connection close.
42  *
43  * @author Rodney Waldhoff
44  * @author Glenn L. Nielsen
45  * @author James House
46  * @author Dirk Verbeeck
47  * @version $Revision: 1.22 $ $Date: 2004/03/06 13:35:31 $
48  */

49 public class DelegatingPreparedStatement extends DelegatingStatement
50         implements PreparedStatement JavaDoc {
51
52     /** My delegate. */
53     protected PreparedStatement JavaDoc _stmt = null;
54
55     /**
56      * Create a wrapper for the Statement which traces this
57      * Statement to the Connection which created it and the
58      * code which created it.
59      *
60      * @param s the {@link PreparedStatement} to delegate all calls to.
61      * @param c the {@link DelegatingConnection} that created this statement.
62      */

63     public DelegatingPreparedStatement(DelegatingConnection c,
64                                        PreparedStatement JavaDoc s) {
65         super(c, s);
66         _stmt = s;
67     }
68
69     public boolean equals(Object JavaDoc obj) {
70         PreparedStatement JavaDoc delegate = (PreparedStatement JavaDoc) getInnermostDelegate();
71         if (delegate == null) {
72             return false;
73         }
74         if (obj instanceof DelegatingPreparedStatement) {
75             DelegatingPreparedStatement s = (DelegatingPreparedStatement) obj;
76             return delegate.equals(s.getInnermostDelegate());
77         }
78         else {
79             return delegate.equals(obj);
80         }
81     }
82
83     /** Sets my delegate. */
84     public void setDelegate(PreparedStatement JavaDoc s) {
85         super.setDelegate(s);
86         _stmt = s;
87     }
88
89     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
90         checkOpen();
91         try {
92             return DelegatingResultSet.wrapResultSet(this,_stmt.executeQuery());
93         }
94         catch (SQLException JavaDoc e) {
95             handleException(e);
96             return null;
97         }
98     }
99
100     public int executeUpdate() throws SQLException JavaDoc
101     { checkOpen(); try { return _stmt.executeUpdate(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
102
103     public void setNull(int parameterIndex, int sqlType) throws SQLException JavaDoc
104     { checkOpen(); try { _stmt.setNull(parameterIndex,sqlType); } catch (SQLException JavaDoc e) { handleException(e); } }
105
106     public void setBoolean(int parameterIndex, boolean x) throws SQLException JavaDoc
107     { checkOpen(); try { _stmt.setBoolean(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
108
109     public void setByte(int parameterIndex, byte x) throws SQLException JavaDoc
110     { checkOpen(); try { _stmt.setByte(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
111
112     public void setShort(int parameterIndex, short x) throws SQLException JavaDoc
113     { checkOpen(); try { _stmt.setShort(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
114
115     public void setInt(int parameterIndex, int x) throws SQLException JavaDoc
116     { checkOpen(); try { _stmt.setInt(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
117
118     public void setLong(int parameterIndex, long x) throws SQLException JavaDoc
119     { checkOpen(); try { _stmt.setLong(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
120
121     public void setFloat(int parameterIndex, float x) throws SQLException JavaDoc
122     { checkOpen(); try { _stmt.setFloat(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
123
124     public void setDouble(int parameterIndex, double x) throws SQLException JavaDoc
125     { checkOpen(); try { _stmt.setDouble(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
126
127     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException JavaDoc
128     { checkOpen(); try { _stmt.setBigDecimal(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
129
130     public void setString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc
131     { checkOpen(); try { _stmt.setString(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
132
133     public void setBytes(int parameterIndex, byte[] x) throws SQLException JavaDoc
134     { checkOpen(); try { _stmt.setBytes(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
135
136     public void setDate(int parameterIndex, java.sql.Date JavaDoc x) throws SQLException JavaDoc
137     { checkOpen(); try { _stmt.setDate(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
138
139     public void setTime(int parameterIndex, java.sql.Time JavaDoc x) throws SQLException JavaDoc
140     { checkOpen(); try { _stmt.setTime(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
141
142     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x) throws SQLException JavaDoc
143     { checkOpen(); try { _stmt.setTimestamp(parameterIndex,x); } catch (SQLException JavaDoc e) { handleException(e); } }
144
145     public void setAsciiStream(int parameterIndex, java.io.InputStream JavaDoc x, int length) throws SQLException JavaDoc
146     { checkOpen(); try { _stmt.setAsciiStream(parameterIndex,x,length); } catch (SQLException JavaDoc e) { handleException(e); } }
147
148     /** @deprecated */
149     public void setUnicodeStream(int parameterIndex, java.io.InputStream JavaDoc x, int length) throws SQLException JavaDoc
150     { checkOpen(); try { _stmt.setUnicodeStream(parameterIndex,x,length); } catch (SQLException JavaDoc e) { handleException(e); } }
151     
152     public void setBinaryStream(int parameterIndex, java.io.InputStream JavaDoc x, int length) throws SQLException JavaDoc
153     { checkOpen(); try { _stmt.setBinaryStream(parameterIndex,x,length); } catch (SQLException JavaDoc e) { handleException(e); } }
154
155     public void clearParameters() throws SQLException JavaDoc
156     { checkOpen(); try { _stmt.clearParameters(); } catch (SQLException JavaDoc e) { handleException(e); } }
157
158     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException JavaDoc
159     { checkOpen(); try { _stmt.setObject(parameterIndex, x, targetSqlType, scale); } catch (SQLException JavaDoc e) { handleException(e); } }
160
161     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException JavaDoc
162     { checkOpen(); try { _stmt.setObject(parameterIndex, x, targetSqlType); } catch (SQLException JavaDoc e) { handleException(e); } }
163
164     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc
165     { checkOpen(); try { _stmt.setObject(parameterIndex, x); } catch (SQLException JavaDoc e) { handleException(e); } }
166
167     public boolean execute() throws SQLException JavaDoc
168     { checkOpen(); try { return _stmt.execute(); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
169
170     public void addBatch() throws SQLException JavaDoc
171     { checkOpen(); try { _stmt.addBatch(); } catch (SQLException JavaDoc e) { handleException(e); } }
172
173     public void setCharacterStream(int parameterIndex, java.io.Reader JavaDoc reader, int length) throws SQLException JavaDoc
174     { checkOpen(); try { _stmt.setCharacterStream(parameterIndex,reader,length); } catch (SQLException JavaDoc e) { handleException(e); } }
175
176     public void setRef(int i, Ref JavaDoc x) throws SQLException JavaDoc
177     { checkOpen(); try { _stmt.setRef(i,x); } catch (SQLException JavaDoc e) { handleException(e); } }
178
179     public void setBlob(int i, Blob JavaDoc x) throws SQLException JavaDoc
180     { checkOpen(); try { _stmt.setBlob(i,x); } catch (SQLException JavaDoc e) { handleException(e); } }
181
182     public void setClob(int i, Clob JavaDoc x) throws SQLException JavaDoc
183     { checkOpen(); try { _stmt.setClob(i,x); } catch (SQLException JavaDoc e) { handleException(e); } }
184
185     public void setArray(int i, Array JavaDoc x) throws SQLException JavaDoc
186     { checkOpen(); try { _stmt.setArray(i,x); } catch (SQLException JavaDoc e) { handleException(e); } }
187
188     public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc
189     { checkOpen(); try { return _stmt.getMetaData(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
190
191     public void setDate(int parameterIndex, java.sql.Date JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
192     { checkOpen(); try { _stmt.setDate(parameterIndex,x,cal); } catch (SQLException JavaDoc e) { handleException(e); } }
193
194     public void setTime(int parameterIndex, java.sql.Time JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
195     { checkOpen(); try { _stmt.setTime(parameterIndex,x,cal); } catch (SQLException JavaDoc e) { handleException(e); } }
196
197     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
198     { checkOpen(); try { _stmt.setTimestamp(parameterIndex,x,cal); } catch (SQLException JavaDoc e) { handleException(e); } }
199
200     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName) throws SQLException JavaDoc
201     { checkOpen(); try { _stmt.setNull(paramIndex,sqlType,typeName); } catch (SQLException JavaDoc e) { handleException(e); } }
202
203     // ------------------- JDBC 3.0 -----------------------------------------
204
// Will be commented by the build process on a JDBC 2.0 system
205

206 /* JDBC_3_ANT_KEY_BEGIN */
207
208     public void setURL(int parameterIndex, java.net.URL JavaDoc x) throws SQLException JavaDoc
209     { checkOpen(); try { _stmt.setURL(parameterIndex, x); } catch (SQLException JavaDoc e) { handleException(e); } }
210
211     public java.sql.ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc
212     { checkOpen(); try { return _stmt.getParameterMetaData(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
213
214 /* JDBC_3_ANT_KEY_END */
215 }
216
Popular Tags