KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > spy > SpyPreparedStatement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source 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, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql.spy;
30
31 import com.caucho.log.Log;
32 import com.caucho.sql.SQLExceptionWrapper;
33 import com.caucho.util.L10N;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.math.BigDecimal JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.sql.*;
40 import java.util.Calendar JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Spying on a statement;
45  */

46 public class SpyPreparedStatement extends SpyStatement
47   implements java.sql.PreparedStatement JavaDoc {
48   protected final static Logger JavaDoc log = Log.open(SpyPreparedStatement.class);
49   protected static L10N L = new L10N(SpyPreparedStatement.class);
50
51   private String JavaDoc _sql;
52   protected PreparedStatement _pstmt;
53
54   SpyPreparedStatement(String JavaDoc id, Connection conn,
55                        PreparedStatement stmt, String JavaDoc sql)
56   {
57     super(id, conn, stmt);
58
59     _pstmt = stmt;
60     _sql = sql;
61   }
62
63   public java.sql.ResultSet JavaDoc executeQuery()
64     throws SQLException
65   {
66     try {
67       log.info(_id + ":executeQuery(" + _sql + ")");
68
69       ResultSet rs = _pstmt.executeQuery();
70
71       return rs;
72     } catch (Throwable JavaDoc e) {
73       log.info(_id + ":exn-executeQuery(" + e + ")");
74
75       throw SQLExceptionWrapper.create(e);
76     }
77   }
78
79   public int executeUpdate()
80     throws SQLException
81   {
82     try {
83       int result = _pstmt.executeUpdate();
84
85       log.info(_id + ":executeUpdate(" + _sql + ") -> " + result);
86
87       return result;
88     } catch (Throwable JavaDoc e) {
89       log.info(_id + ":exn-executeUpdate(" + e + ")");
90
91       throw SQLExceptionWrapper.create(e);
92     }
93   }
94
95   public boolean execute()
96     throws SQLException
97   {
98     try {
99       boolean result = _pstmt.execute();
100
101       log.info(_id + ":execute(" + _sql + ") -> " + result);
102
103       return result;
104     } catch (Throwable JavaDoc e) {
105       log.info(_id + ":exn-execute(" + e + ")");
106
107       throw SQLExceptionWrapper.create(e);
108     }
109   }
110
111   public void addBatch()
112     throws SQLException
113   {
114     try {
115       _pstmt.addBatch();
116
117       log.info(_id + ":addBatch()");
118     } catch (Throwable JavaDoc e) {
119       log.info(_id + ":exn-addBatch(" + e + ")");
120
121       throw SQLExceptionWrapper.create(e);
122     }
123   }
124
125   public void clearParameters()
126     throws SQLException
127   {
128     try {
129       log.info(_id + ":clearParameters()");
130
131       _pstmt.clearParameters();
132     } catch (Throwable JavaDoc e) {
133       log.info(_id + ":exn-clearParameters(" + e + ")");
134
135       throw SQLExceptionWrapper.create(e);
136     }
137   }
138
139   public ResultSetMetaData getMetaData()
140     throws SQLException
141   {
142     try {
143       ResultSetMetaData result = _pstmt.getMetaData();
144
145       log.info(_id + ":getMetaData() -> " + result);
146
147       return result;
148     } catch (Throwable JavaDoc e) {
149       log.info(_id + ":exn-getMetaData(" + e + ")");
150
151       throw SQLExceptionWrapper.create(e);
152     }
153   }
154
155   public ParameterMetaData getParameterMetaData()
156     throws SQLException
157   {
158     return _pstmt.getParameterMetaData();
159   }
160
161   public void setNull(int parameterIndex, int sqlType)
162     throws SQLException
163   {
164     try {
165       log.info(_id + ":setNull(" + parameterIndex + ",type=" + sqlType + ")");
166
167       _pstmt.setNull(parameterIndex, sqlType);
168     } catch (Throwable JavaDoc e) {
169       log.info(_id + ":exn-setNull(" + e + ")");
170
171       throw SQLExceptionWrapper.create(e);
172     }
173   }
174
175   public void setNull(int parameterIndex, int sqlType, String JavaDoc typeName)
176     throws SQLException
177   {
178     try {
179       log.info(_id + ":setNull(" + parameterIndex + ",type=" + sqlType +
180               ",typeName=" + typeName + ")");
181
182       _pstmt.setNull(parameterIndex, sqlType, typeName);
183     } catch (Throwable JavaDoc e) {
184       log.info(_id + ":exn-setNull(" + e + ")");
185
186       throw SQLExceptionWrapper.create(e);
187     }
188   }
189
190   public void setBoolean(int index, boolean value)
191     throws SQLException
192   {
193     try {
194       log.info(_id + ":setBoolean(" + index + "," + value + ")");
195
196       _pstmt.setBoolean(index, value);
197     } catch (Throwable JavaDoc e) {
198       log.info(_id + ":exn-setBoolean(" + e + ")");
199
200       throw SQLExceptionWrapper.create(e);
201     }
202   }
203
204   public void setByte(int index, byte value)
205     throws SQLException
206   {
207     try {
208       log.info(_id + ":setByte(" + index + "," + value + ")");
209
210       _pstmt.setByte(index, value);
211     } catch (Throwable JavaDoc e) {
212       log.info(_id + ":exn-setByte(" + e + ")");
213
214       throw SQLExceptionWrapper.create(e);
215     }
216   }
217
218   public void setShort(int index, short value)
219     throws SQLException
220   {
221     try {
222       log.info(_id + ":setShort(" + index + "," + value + ")");
223
224       _pstmt.setShort(index, value);
225     } catch (Throwable JavaDoc e) {
226       log.info(_id + ":exn-setShort(" + e + ")");
227
228       throw SQLExceptionWrapper.create(e);
229     }
230   }
231
232   public void setInt(int index, int value)
233     throws SQLException
234   {
235     try {
236       log.info(_id + ":setInt(" + index + "," + value + ")");
237
238       _pstmt.setInt(index, value);
239     } catch (Throwable JavaDoc e) {
240       log.info(_id + ":exn-setInt(" + e + ")");
241
242       throw SQLExceptionWrapper.create(e);
243     }
244   }
245
246   public void setLong(int index, long value)
247     throws SQLException
248   {
249     try {
250       log.info(_id + ":setLong(" + index + "," + value + ")");
251
252       _pstmt.setLong(index, value);
253     } catch (Throwable JavaDoc e) {
254       log.info(_id + ":exn-setLong(" + e + ")");
255
256       throw SQLExceptionWrapper.create(e);
257     }
258   }
259
260   public void setFloat(int index, float value)
261     throws SQLException
262   {
263     try {
264       log.info(_id + ":setFloat(" + index + "," + value + ")");
265
266       _pstmt.setFloat(index, value);
267     } catch (Throwable JavaDoc e) {
268       log.info(_id + ":exn-setFloat(" + e + ")");
269
270       throw SQLExceptionWrapper.create(e);
271     }
272   }
273
274   public void setDouble(int index, double value)
275     throws SQLException
276   {
277     try {
278       log.info(_id + ":setDouble(" + index + "," + value + ")");
279
280       _pstmt.setDouble(index, value);
281     } catch (Throwable JavaDoc e) {
282       log.info(_id + ":exn-setDouble(" + e + ")");
283
284       throw SQLExceptionWrapper.create(e);
285     }
286   }
287
288   public void setBigDecimal(int index, BigDecimal JavaDoc value)
289     throws SQLException
290   {
291     try {
292       log.info(_id + ":setBigDecimal(" + index + "," + value + ")");
293
294       _pstmt.setBigDecimal(index, value);
295     } catch (Throwable JavaDoc e) {
296       log.info(_id + ":exn-setBigDecimal(" + e + ")");
297
298       throw SQLExceptionWrapper.create(e);
299     }
300   }
301
302   public void setString(int index, String JavaDoc value)
303     throws SQLException
304   {
305     try {
306       log.info(_id + ":setString(" + index + "," + value + ")");
307
308       _pstmt.setString(index, value);
309     } catch (Throwable JavaDoc e) {
310       log.info(_id + ":exn-setString(" + e + ")");
311
312       throw SQLExceptionWrapper.create(e);
313     }
314   }
315
316   public void setBytes(int index, byte []value)
317     throws SQLException
318   {
319     try {
320       if (value != null)
321         log.info(_id + ":setBytes(" + index + ",len=" + value.length + ")");
322       else
323         log.info(_id + ":setBytes(" + index + ",null");
324
325       _pstmt.setBytes(index, value);
326     } catch (Throwable JavaDoc e) {
327       log.info(_id + ":exn-setBytes(" + e + ")");
328
329       throw SQLExceptionWrapper.create(e);
330     }
331   }
332
333   public void setDate(int index, Date value)
334     throws SQLException
335   {
336     try {
337       log.info(_id + ":setDate(" + index + "," + value + ")");
338
339       _pstmt.setDate(index, value);
340     } catch (Throwable JavaDoc e) {
341       log.info(_id + ":exn-setDate(" + e + ")");
342
343       throw SQLExceptionWrapper.create(e);
344     }
345   }
346
347   public void setDate(int index, Date value, Calendar JavaDoc cal)
348     throws SQLException
349   {
350     try {
351       log.info(_id + ":setDate(" + index + "," + value + ",cal=" + cal + ")");
352
353       _pstmt.setDate(index, value, cal);
354     } catch (Throwable JavaDoc e) {
355       log.info(_id + ":exn-setDate(" + e + ")");
356
357       throw SQLExceptionWrapper.create(e);
358     }
359   }
360
361   public void setTime(int index, Time value)
362     throws SQLException
363   {
364     try {
365       log.info(_id + ":setTime(" + index + "," + value + ")");
366
367       _pstmt.setTime(index, value);
368     } catch (Throwable JavaDoc e) {
369       log.info(_id + ":exn-setTime(" + e + ")");
370
371       throw SQLExceptionWrapper.create(e);
372     }
373   }
374
375   public void setTime(int index, Time value, Calendar JavaDoc cal)
376     throws SQLException
377   {
378     try {
379       log.info(_id + ":setTime(" + index + "," + value + ",cal=" + cal + ")");
380
381       _pstmt.setTime(index, value, cal);
382     } catch (Throwable JavaDoc e) {
383       log.info(_id + ":exn-setTime(" + e + ")");
384
385       throw SQLExceptionWrapper.create(e);
386     }
387   }
388
389   public void setTimestamp(int index, Timestamp value)
390     throws SQLException
391   {
392     try {
393       log.info(_id + ":setTimestamp(" + index + "," + value + ")");
394
395       _pstmt.setTimestamp(index, value);
396     } catch (Throwable JavaDoc e) {
397       log.info(_id + ":exn-setTimestamp(" + e + ")");
398
399       throw SQLExceptionWrapper.create(e);
400     }
401   }
402
403   public void setTimestamp(int index, Timestamp value, Calendar JavaDoc cal)
404     throws SQLException
405   {
406     try {
407       log.info(_id + ":setTimestamp(" + index + "," + value + ",cal=" + cal + ")");
408
409       _pstmt.setTimestamp(index, value, cal);
410     } catch (Throwable JavaDoc e) {
411       log.info(_id + ":exn-setTimestamp(" + e + ")");
412
413       throw SQLExceptionWrapper.create(e);
414     }
415   }
416
417   public void setAsciiStream(int index, InputStream JavaDoc value, int length)
418     throws SQLException
419   {
420     try {
421       log.info(_id + ":setAsciiStream(" + index + "," + value + ",len=" + length + ")");
422
423       _pstmt.setAsciiStream(index, value, length);
424     } catch (Throwable JavaDoc e) {
425       log.info(_id + ":exn-setAsciiStream(" + e + ")");
426
427       throw SQLExceptionWrapper.create(e);
428     }
429   }
430
431   public void setUnicodeStream(int index, InputStream JavaDoc value, int length)
432     throws SQLException
433   {
434     try {
435       log.info(_id + ":setUnicodeStream(" + index + "," + value + ",len=" + length + ")");
436
437       _pstmt.setUnicodeStream(index, value, length);
438     } catch (Throwable JavaDoc e) {
439       log.info(_id + ":exn-setUnicodeStream(" + e + ")");
440
441       throw SQLExceptionWrapper.create(e);
442     }
443   }
444
445   public void setBinaryStream(int index, InputStream JavaDoc value, int length)
446     throws SQLException
447   {
448     try {
449       log.info(_id + ":setBinaryStream(" + index + "," + value + ",len=" + length + ")");
450
451       _pstmt.setBinaryStream(index, value, length);
452     } catch (Throwable JavaDoc e) {
453       log.info(_id + ":exn-setBinaryStream(" + e + ")");
454
455       throw SQLExceptionWrapper.create(e);
456     }
457   }
458
459   public void setCharacterStream(int index, Reader JavaDoc value, int length)
460     throws SQLException
461   {
462     try {
463       log.info(_id + ":setCharacterStream(" + index + "," + value + ",len=" + length + ")");
464
465       _pstmt.setCharacterStream(index, value, length);
466     } catch (Throwable JavaDoc e) {
467       log.info(_id + ":exn-setCharacterStream(" + e + ")");
468
469       throw SQLExceptionWrapper.create(e);
470     }
471   }
472
473   public void setObject(int index, Object JavaDoc value, int type, int scale)
474     throws SQLException
475   {
476     try {
477       log.info(_id + ":setObject(" + index + "," + value +
478               ",type=" + type + ",scale=" + scale + ")");
479
480       _pstmt.setObject(index, value, type, scale);
481     } catch (Throwable JavaDoc e) {
482       log.info(_id + ":exn-setObject(" + e + ")");
483
484       throw SQLExceptionWrapper.create(e);
485     }
486   }
487
488   public void setObject(int index, Object JavaDoc value, int type)
489     throws SQLException
490   {
491     try {
492       log.info(_id + ":setObject(" + index + "," + value +
493               ",type=" + type + ")");
494
495       _pstmt.setObject(index, value, type);
496     } catch (Throwable JavaDoc e) {
497       log.info(_id + ":exn-setObject(" + e + ")");
498
499       throw SQLExceptionWrapper.create(e);
500     }
501   }
502
503   public void setObject(int index, Object JavaDoc value)
504     throws SQLException
505   {
506     try {
507       log.info(_id + ":setObject(" + index + "," + value + ")");
508
509       _pstmt.setObject(index, value);
510     } catch (Throwable JavaDoc e) {
511       e.printStackTrace();
512
513       log.info(_id + ":exn-setObject(" + e + ")");
514
515       throw SQLExceptionWrapper.create(e);
516     }
517   }
518
519   public void setRef(int index, Ref value)
520     throws SQLException
521   {
522     try {
523       log.info(_id + ":setRef(" + index + "," + value + ")");
524
525       _pstmt.setRef(index, value);
526     } catch (Throwable JavaDoc e) {
527       log.info(_id + ":exn-setRef(" + e + ")");
528
529       throw SQLExceptionWrapper.create(e);
530     }
531   }
532
533   public void setBlob(int index, Blob value)
534     throws SQLException
535   {
536     try {
537       log.info(_id + ":setBlob(" + index + "," + value + ")");
538
539       _pstmt.setBlob(index, value);
540     } catch (Throwable JavaDoc e) {
541       log.info(_id + ":exn-setBlob(" + e + ")");
542
543       throw SQLExceptionWrapper.create(e);
544     }
545   }
546
547   public void setClob(int index, Clob value)
548     throws SQLException
549   {
550     try {
551       log.info(_id + ":setClob(" + index + "," + value + ")");
552
553       _pstmt.setClob(index, value);
554     } catch (Throwable JavaDoc e) {
555       log.info(_id + ":exn-setClob(" + e + ")");
556
557       throw SQLExceptionWrapper.create(e);
558     }
559   }
560
561   public void setArray(int index, Array value)
562     throws SQLException
563   {
564     try {
565       log.info(_id + ":setArray(" + index + "," + value + ")");
566
567       _pstmt.setArray(index, value);
568     } catch (Throwable JavaDoc e) {
569       log.info(_id + ":exn-setArray(" + e + ")");
570
571       throw SQLExceptionWrapper.create(e);
572     }
573   }
574
575   public void setURL(int index, URL JavaDoc value)
576     throws SQLException
577   {
578     throw new UnsupportedOperationException JavaDoc();
579   }
580 }
581
Popular Tags