KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > PreparedStatementInPool


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.jdbc;
5
6 import java.io.InputStream JavaDoc;
7 import java.io.Reader JavaDoc;
8 import java.math.BigDecimal JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.sql.Array JavaDoc;
11 import java.sql.Blob JavaDoc;
12 import java.sql.Clob JavaDoc;
13 import java.sql.Date JavaDoc;
14 import java.sql.ParameterMetaData JavaDoc;
15 import java.sql.PreparedStatement JavaDoc;
16 import java.sql.Ref JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.ResultSetMetaData JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Time JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Calendar JavaDoc;
23
24 /**
25  * Wrapper around a PreparedStatement that supports error-handling
26  * and caching.
27  *
28  * @author Aaron Mulder ammulder@alumni.princeton.edu
29  */

30 public class PreparedStatementInPool extends StatementInPool implements PreparedStatement JavaDoc {
31
32     private final static String JavaDoc CLOSED = "PreparedStatement has been closed!";
33     private PreparedStatement JavaDoc impl;
34     private ConnectionWrapper con;
35     private String JavaDoc sql;
36
37     /**
38      * Creates a new statement from a source statement and wrapper connection.
39      */

40     public PreparedStatementInPool(PreparedStatement JavaDoc source, ConnectionWrapper owner, String JavaDoc sql) {
41         super(source, owner);
42         if (source == null || owner == null) throw new NullPointerException JavaDoc();
43         impl = source;
44         con = owner;
45         this.sql = sql;
46     }
47
48     /**
49      * Gets a reference to the "real" Statement. This should only be used if
50      * you need to cast that to a specific type to call a proprietary method -
51      * you will defeat all the pooling if you use the underlying Statement
52      * directly.
53      */

54     public PreparedStatement JavaDoc getUnderlyingPreparedStatement() {
55         return impl;
56     }
57
58     /**
59      * Returns the SQL Statement string.
60      */

61     public String JavaDoc getSql() {
62         return sql;
63     }
64
65     // ---- Implementation of java.sql.Statement ----
66

67     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
68         if (impl == null) throw new SQLException JavaDoc(CLOSED);
69         try {
70             setLastUsed();
71             return new ResultSetInPool(impl.executeQuery(), this);
72         } catch (SQLException JavaDoc e) {
73             setError(e);
74             throw e;
75         }
76     }
77
78     public int executeUpdate() throws SQLException JavaDoc {
79         if (impl == null) throw new SQLException JavaDoc(CLOSED);
80         try {
81             setLastUsed();
82             return impl.executeUpdate();
83         } catch (SQLException JavaDoc e) {
84             setError(e);
85             throw e;
86         }
87     }
88
89     public void setNull(int parameterIndex, int sqlType) throws SQLException JavaDoc {
90         if (impl == null) throw new SQLException JavaDoc(CLOSED);
91         try {
92             impl.setNull(parameterIndex, sqlType);
93         } catch (SQLException JavaDoc e) {
94             setError(e);
95             throw e;
96         }
97     }
98
99     public void setBoolean(int parameterIndex, boolean x) throws SQLException JavaDoc {
100         if (impl == null) throw new SQLException JavaDoc(CLOSED);
101         try {
102             impl.setBoolean(parameterIndex, x);
103         } catch (SQLException JavaDoc e) {
104             setError(e);
105             throw e;
106         }
107     }
108
109     public void setByte(int parameterIndex, byte x) throws SQLException JavaDoc {
110         if (impl == null) throw new SQLException JavaDoc(CLOSED);
111         try {
112             impl.setByte(parameterIndex, x);
113         } catch (SQLException JavaDoc e) {
114             setError(e);
115             throw e;
116         }
117     }
118
119     public void setShort(int parameterIndex, short x) throws SQLException JavaDoc {
120         if (impl == null) throw new SQLException JavaDoc(CLOSED);
121         try {
122             impl.setShort(parameterIndex, x);
123         } catch (SQLException JavaDoc e) {
124             setError(e);
125             throw e;
126         }
127     }
128
129     public void setInt(int parameterIndex, int x) throws SQLException JavaDoc {
130         if (impl == null) throw new SQLException JavaDoc(CLOSED);
131         try {
132             impl.setInt(parameterIndex, x);
133         } catch (SQLException JavaDoc e) {
134             setError(e);
135             throw e;
136         }
137     }
138
139     public void setLong(int parameterIndex, long x) throws SQLException JavaDoc {
140         if (impl == null) throw new SQLException JavaDoc(CLOSED);
141         try {
142             impl.setLong(parameterIndex, x);
143         } catch (SQLException JavaDoc e) {
144             setError(e);
145             throw e;
146         }
147     }
148
149     public void setFloat(int parameterIndex, float x) throws SQLException JavaDoc {
150         if (impl == null) throw new SQLException JavaDoc(CLOSED);
151         try {
152             impl.setFloat(parameterIndex, x);
153         } catch (SQLException JavaDoc e) {
154             setError(e);
155             throw e;
156         }
157     }
158
159     public void setDouble(int parameterIndex, double x) throws SQLException JavaDoc {
160         if (impl == null) throw new SQLException JavaDoc(CLOSED);
161         try {
162             impl.setDouble(parameterIndex, x);
163         } catch (SQLException JavaDoc e) {
164             setError(e);
165             throw e;
166         }
167     }
168
169     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException JavaDoc {
170         if (impl == null) throw new SQLException JavaDoc(CLOSED);
171         try {
172             impl.setBigDecimal(parameterIndex, x);
173         } catch (SQLException JavaDoc e) {
174             setError(e);
175             throw e;
176         }
177     }
178
179     public void setString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc {
180         if (impl == null) throw new SQLException JavaDoc(CLOSED);
181         try {
182             impl.setString(parameterIndex, x);
183         } catch (SQLException JavaDoc e) {
184             setError(e);
185             throw e;
186         }
187     }
188
189     public void setBytes(int parameterIndex, byte[] x) throws SQLException JavaDoc {
190         if (impl == null) throw new SQLException JavaDoc(CLOSED);
191         try {
192             impl.setBytes(parameterIndex, x);
193         } catch (SQLException JavaDoc e) {
194             setError(e);
195             throw e;
196         }
197     }
198
199     public void setDate(int parameterIndex, Date JavaDoc x) throws SQLException JavaDoc {
200         if (impl == null) throw new SQLException JavaDoc(CLOSED);
201         try {
202             impl.setDate(parameterIndex, x);
203         } catch (SQLException JavaDoc e) {
204             setError(e);
205             throw e;
206         }
207     }
208
209     public void setTime(int parameterIndex, Time JavaDoc x) throws SQLException JavaDoc {
210         if (impl == null) throw new SQLException JavaDoc(CLOSED);
211         try {
212             impl.setTime(parameterIndex, x);
213         } catch (SQLException JavaDoc e) {
214             setError(e);
215             throw e;
216         }
217     }
218
219     public void setTimestamp(int parameterIndex, Timestamp JavaDoc x) throws SQLException JavaDoc {
220         if (impl == null) throw new SQLException JavaDoc(CLOSED);
221         try {
222             impl.setTimestamp(parameterIndex, x);
223         } catch (SQLException JavaDoc e) {
224             setError(e);
225             throw e;
226         }
227     }
228
229     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
230         if (impl == null) throw new SQLException JavaDoc(CLOSED);
231         try {
232             impl.setAsciiStream(parameterIndex, x, length);
233         } catch (SQLException JavaDoc e) {
234             setError(e);
235             throw e;
236         }
237     }
238
239     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
240         if (impl == null) throw new SQLException JavaDoc(CLOSED);
241         try {
242             impl.setUnicodeStream(parameterIndex, x, length);
243         } catch (SQLException JavaDoc e) {
244             setError(e);
245             throw e;
246         }
247     }
248
249     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
250         if (impl == null) throw new SQLException JavaDoc(CLOSED);
251         try {
252             impl.setBinaryStream(parameterIndex, x, length);
253         } catch (SQLException JavaDoc e) {
254             setError(e);
255             throw e;
256         }
257     }
258
259     public void clearParameters() throws SQLException JavaDoc {
260         if (impl == null) throw new SQLException JavaDoc(CLOSED);
261         try {
262             impl.clearParameters();
263         } catch (SQLException JavaDoc e) {
264             setError(e);
265             throw e;
266         }
267     }
268
269     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException JavaDoc {
270         if (impl == null) throw new SQLException JavaDoc(CLOSED);
271         try {
272             impl.setObject(parameterIndex, x, targetSqlType, scale);
273         } catch (SQLException JavaDoc e) {
274             setError(e);
275             throw e;
276         }
277     }
278
279     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException JavaDoc {
280         if (impl == null) throw new SQLException JavaDoc(CLOSED);
281         try {
282             impl.setObject(parameterIndex, x, targetSqlType);
283         } catch (SQLException JavaDoc e) {
284             setError(e);
285             throw e;
286         }
287     }
288
289     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc {
290         if (impl == null) throw new SQLException JavaDoc(CLOSED);
291         try {
292             impl.setObject(parameterIndex, x);
293         } catch (SQLException JavaDoc e) {
294             setError(e);
295             throw e;
296         }
297     }
298
299     public boolean execute() throws SQLException JavaDoc {
300         if (impl == null) throw new SQLException JavaDoc(CLOSED);
301         try {
302             setLastUsed();
303             return impl.execute();
304         } catch (SQLException JavaDoc e) {
305             setError(e);
306             throw e;
307         }
308     }
309
310     public void addBatch() throws SQLException JavaDoc {
311         if (impl == null) throw new SQLException JavaDoc(CLOSED);
312         try {
313             impl.addBatch();
314         } catch (SQLException JavaDoc e) {
315             setError(e);
316             throw e;
317         }
318     }
319
320     public void setCharacterStream(int parameterIndex, Reader JavaDoc reader, int length) throws SQLException JavaDoc {
321         if (impl == null) throw new SQLException JavaDoc(CLOSED);
322         try {
323             impl.setCharacterStream(parameterIndex, reader, length);
324         } catch (SQLException JavaDoc e) {
325             setError(e);
326             throw e;
327         }
328     }
329
330     public void setRef(int i, Ref JavaDoc x) throws SQLException JavaDoc {
331         if (impl == null) throw new SQLException JavaDoc(CLOSED);
332         try {
333             impl.setRef(i, x);
334         } catch (SQLException JavaDoc e) {
335             setError(e);
336             throw e;
337         }
338     }
339
340     public void setBlob(int i, Blob JavaDoc x) throws SQLException JavaDoc {
341         if (impl == null) throw new SQLException JavaDoc(CLOSED);
342         try {
343             impl.setBlob(i, x);
344         } catch (SQLException JavaDoc e) {
345             setError(e);
346             throw e;
347         }
348     }
349
350     public void setClob(int i, Clob JavaDoc x) throws SQLException JavaDoc {
351         if (impl == null) throw new SQLException JavaDoc(CLOSED);
352         try {
353             impl.setClob(i, x);
354         } catch (SQLException JavaDoc e) {
355             setError(e);
356             throw e;
357         }
358     }
359
360     public void setArray(int i, Array JavaDoc x) throws SQLException JavaDoc {
361         if (impl == null) throw new SQLException JavaDoc(CLOSED);
362         try {
363             impl.setArray(i, x);
364         } catch (SQLException JavaDoc e) {
365             setError(e);
366             throw e;
367         }
368     }
369
370     public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
371         if (impl == null) throw new SQLException JavaDoc(CLOSED);
372         try {
373             return impl.getMetaData();
374         } catch (SQLException JavaDoc e) {
375             setError(e);
376             throw e;
377         }
378     }
379
380     public void setDate(int parameterIndex, Date JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc {
381         if (impl == null) throw new SQLException JavaDoc(CLOSED);
382         try {
383             impl.setDate(parameterIndex, x, cal);
384         } catch (SQLException JavaDoc e) {
385             setError(e);
386             throw e;
387         }
388     }
389
390     public void setTime(int parameterIndex, Time JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc {
391         if (impl == null) throw new SQLException JavaDoc(CLOSED);
392         try {
393             impl.setTime(parameterIndex, x, cal);
394         } catch (SQLException JavaDoc e) {
395             setError(e);
396             throw e;
397         }
398     }
399
400     public void setTimestamp(int parameterIndex, Timestamp JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc {
401         if (impl == null) throw new SQLException JavaDoc(CLOSED);
402         try {
403             impl.setTimestamp(parameterIndex, x, cal);
404         } catch (SQLException JavaDoc e) {
405             setError(e);
406             throw e;
407         }
408     }
409
410     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName) throws SQLException JavaDoc {
411         if (impl == null) throw new SQLException JavaDoc(CLOSED);
412         try {
413             impl.setNull(paramIndex, sqlType, typeName);
414         } catch (SQLException JavaDoc e) {
415             setError(e);
416             throw e;
417         }
418     }
419
420     public void close() throws SQLException JavaDoc {
421         if (con != null) {
422             con.statementClosed(this);
423         }
424         super.clearFields();
425         con = null;
426         impl = null;
427         sql = null;
428     }
429
430     // JDK 1.4 methods
431

432     /* (non-Javadoc)
433      * @see java.sql.PreparedStatement#setURL(int, java.net.URL)
434      */

435     public void setURL(int arg0, URL JavaDoc arg1) throws SQLException JavaDoc {
436         // TODO Auto-generated method stub
437

438     }
439
440     /* (non-Javadoc)
441      * @see java.sql.PreparedStatement#getParameterMetaData()
442      */

443     public ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc {
444         // TODO Auto-generated method stub
445
return null;
446     }
447 }
448
Popular Tags