View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
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  package org.jmonit.support.jdbc;
17  
18  import java.io.InputStream;
19  import java.io.Reader;
20  import java.math.BigDecimal;
21  import java.net.URL;
22  import java.sql.Array;
23  import java.sql.Blob;
24  import java.sql.Clob;
25  import java.sql.Connection;
26  import java.sql.Date;
27  import java.sql.NClob;
28  import java.sql.ParameterMetaData;
29  import java.sql.PreparedStatement;
30  import java.sql.Ref;
31  import java.sql.ResultSet;
32  import java.sql.ResultSetMetaData;
33  import java.sql.RowId;
34  import java.sql.SQLException;
35  import java.sql.SQLXML;
36  import java.sql.Time;
37  import java.sql.Timestamp;
38  import java.util.Calendar;
39  
40  import org.jmonit.Monitor;
41  import org.jmonit.Stopwatch;
42  
43  /**
44   * @author <a href="mailto:ndeloof@sourceforge.net">Nicolas De Loof</a>
45   */
46  public class MonitoredPreparedStatement
47      extends MonitoredStatement
48      implements PreparedStatement
49  {
50      /** delegate preparedStatement */
51      private PreparedStatement statement;
52  
53      /** The monitor for this statement execution */
54      private Monitor monitor;
55  
56      /**
57       * @param statement target statement
58       * @param query SQL Query
59       * @param connection monitored connection
60       * @param monitor the JDBC monitor on this connection
61       */
62      public MonitoredPreparedStatement( PreparedStatement statement, String query,
63                                         Connection connection, JdbcMonitor monitor )
64      {
65          super( statement, connection, monitor );
66          this.statement = statement;
67          this.monitor = monitor.getPreparedStatementMonitor( query );
68      }
69  
70      /**
71       * @return the monitor used for executions of this statement
72       */
73      protected Monitor getMonitor()
74      {
75          return monitor;
76      }
77  
78      private Stopwatch getStopwatch()
79      {
80          return Stopwatch.start( getMonitor() );
81      }
82  
83      /**
84       * {@inheritDoc}
85       * 
86       * @see java.sql.PreparedStatement#addBatch()
87       */
88      public final void addBatch()
89          throws SQLException
90      {
91          statement.addBatch();
92      }
93  
94      /**
95       * {@inheritDoc}
96       * 
97       * @see java.sql.PreparedStatement#clearParameters()
98       */
99      public final void clearParameters()
100         throws SQLException
101     {
102         statement.clearParameters();
103     }
104 
105     /**
106      * {@inheritDoc}
107      * 
108      * @see java.sql.PreparedStatement#execute()
109      */
110     public final boolean execute()
111         throws SQLException
112     {
113         Stopwatch stopwatch = getStopwatch();
114         try
115         {
116             return statement.execute();
117         }
118         catch ( SQLException sqle )
119         {
120             throw handleException( sqle );
121         }
122         finally
123         {
124             stopwatch.stop();
125         }
126     }
127 
128     /**
129      * {@inheritDoc}
130      * 
131      * @see java.sql.PreparedStatement#executeQuery()
132      */
133     public final ResultSet executeQuery()
134         throws SQLException
135     {
136         Stopwatch stopwatch = getStopwatch();
137         try
138         {
139             return statement.executeQuery();
140         }
141         catch ( SQLException sqle )
142         {
143             throw handleException( sqle );
144         }
145         finally
146         {
147             stopwatch.stop();
148         }
149     }
150 
151     /**
152      * {@inheritDoc}
153      * 
154      * @see java.sql.PreparedStatement#executeUpdate()
155      */
156     public final int executeUpdate()
157         throws SQLException
158     {
159         Stopwatch stopwatch = getStopwatch();
160         try
161         {
162             return statement.executeUpdate();
163         }
164         catch ( SQLException sqle )
165         {
166             throw handleException( sqle );
167         }
168         finally
169         {
170             stopwatch.stop();
171         }
172     }
173 
174     /**
175      * {@inheritDoc}
176      * 
177      * @see java.sql.PreparedStatement#getMetaData()
178      */
179     public final ResultSetMetaData getMetaData()
180         throws SQLException
181     {
182         return statement.getMetaData();
183     }
184 
185     /**
186      * {@inheritDoc}
187      * 
188      * @see java.sql.PreparedStatement#setArray(int, java.sql.Array)
189      */
190     public final void setArray( int i, Array x )
191         throws SQLException
192     {
193         statement.setArray( i, x );
194     }
195 
196     /**
197      * {@inheritDoc}
198      * 
199      * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream,
200      * int)
201      */
202     public final void setAsciiStream( int parameterIndex, InputStream x, int length )
203         throws SQLException
204     {
205         statement.setAsciiStream( parameterIndex, x, length );
206     }
207 
208     /**
209      * {@inheritDoc}
210      * 
211      * @see java.sql.PreparedStatement#setBigDecimal(int, java.math.BigDecimal)
212      */
213     public final void setBigDecimal( int parameterIndex, BigDecimal x )
214         throws SQLException
215     {
216         statement.setBigDecimal( parameterIndex, x );
217     }
218 
219     /**
220      * {@inheritDoc}
221      * 
222      * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream,
223      * int)
224      */
225     public final void setBinaryStream( int parameterIndex, InputStream x, int length )
226         throws SQLException
227     {
228         statement.setBinaryStream( parameterIndex, x, length );
229     }
230 
231     /**
232      * {@inheritDoc}
233      * 
234      * @see java.sql.PreparedStatement#setBlob(int, java.sql.Blob)
235      */
236     public final void setBlob( int i, Blob x )
237         throws SQLException
238     {
239         statement.setBlob( i, x );
240     }
241 
242     /**
243      * {@inheritDoc}
244      * 
245      * @see java.sql.PreparedStatement#setBoolean(int, boolean)
246      */
247     public final void setBoolean( int parameterIndex, boolean x )
248         throws SQLException
249     {
250         statement.setBoolean( parameterIndex, x );
251     }
252 
253     /**
254      * {@inheritDoc}
255      * 
256      * @see java.sql.PreparedStatement#setByte(int, byte)
257      */
258     public final void setByte( int parameterIndex, byte x )
259         throws SQLException
260     {
261         statement.setByte( parameterIndex, x );
262     }
263 
264     /**
265      * {@inheritDoc}
266      * 
267      * @see java.sql.PreparedStatement#setBytes(int, byte[])
268      */
269     public final void setBytes( int parameterIndex, byte[] x )
270         throws SQLException
271     {
272         statement.setBytes( parameterIndex, x );
273     }
274 
275     /**
276      * {@inheritDoc}
277      * 
278      * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader,
279      * int)
280      */
281     public final void setCharacterStream( int parameterIndex, Reader reader, int length )
282         throws SQLException
283     {
284         statement.setCharacterStream( parameterIndex, reader, length );
285     }
286 
287     /**
288      * {@inheritDoc}
289      * 
290      * @see java.sql.PreparedStatement#setClob(int, java.sql.Clob)
291      */
292     public final void setClob( int i, Clob x )
293         throws SQLException
294     {
295         statement.setClob( i, x );
296     }
297 
298     /**
299      * {@inheritDoc}
300      * 
301      * @see java.sql.PreparedStatement#setDate(int, java.sql.Date,
302      * java.util.Calendar)
303      */
304     public final void setDate( int parameterIndex, Date x, Calendar cal )
305         throws SQLException
306     {
307         statement.setDate( parameterIndex, x, cal );
308     }
309 
310     /**
311      * {@inheritDoc}
312      * 
313      * @see java.sql.PreparedStatement#setDate(int, java.sql.Date)
314      */
315     public final void setDate( int parameterIndex, Date x )
316         throws SQLException
317     {
318         statement.setDate( parameterIndex, x );
319     }
320 
321     /**
322      * {@inheritDoc}
323      * 
324      * @see java.sql.PreparedStatement#setDouble(int, double)
325      */
326     public final void setDouble( int parameterIndex, double x )
327         throws SQLException
328     {
329         statement.setDouble( parameterIndex, x );
330     }
331 
332     /**
333      * {@inheritDoc}
334      * 
335      * @see java.sql.PreparedStatement#setFloat(int, float)
336      */
337     public final void setFloat( int parameterIndex, float x )
338         throws SQLException
339     {
340         statement.setFloat( parameterIndex, x );
341     }
342 
343     /**
344      * {@inheritDoc}
345      * 
346      * @see java.sql.PreparedStatement#setInt(int, int)
347      */
348     public final void setInt( int parameterIndex, int x )
349         throws SQLException
350     {
351         statement.setInt( parameterIndex, x );
352     }
353 
354     /**
355      * {@inheritDoc}
356      * 
357      * @see java.sql.PreparedStatement#setLong(int, long)
358      */
359     public final void setLong( int parameterIndex, long x )
360         throws SQLException
361     {
362         statement.setLong( parameterIndex, x );
363     }
364 
365     /**
366      * {@inheritDoc}
367      * 
368      * @see java.sql.PreparedStatement#setNull(int, int, java.lang.String)
369      */
370     public final void setNull( int paramIndex, int sqlType, String typeName )
371         throws SQLException
372     {
373         statement.setNull( paramIndex, sqlType, typeName );
374     }
375 
376     /**
377      * {@inheritDoc}
378      * 
379      * @see java.sql.PreparedStatement#setNull(int, int)
380      */
381     public final void setNull( int parameterIndex, int sqlType )
382         throws SQLException
383     {
384         statement.setNull( parameterIndex, sqlType );
385     }
386 
387     /**
388      * {@inheritDoc}
389      * 
390      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object, int,
391      * int)
392      */
393     public final void setObject( int parameterIndex, Object x, int targetSqlType, int scale )
394         throws SQLException
395     {
396         statement.setObject( parameterIndex, x, targetSqlType, scale );
397     }
398 
399     /**
400      * {@inheritDoc}
401      * 
402      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object, int)
403      */
404     public final void setObject( int parameterIndex, Object x, int targetSqlType )
405         throws SQLException
406     {
407         statement.setObject( parameterIndex, x, targetSqlType );
408     }
409 
410     /**
411      * {@inheritDoc}
412      * 
413      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object)
414      */
415     public final void setObject( int parameterIndex, Object x )
416         throws SQLException
417     {
418         statement.setObject( parameterIndex, x );
419     }
420 
421     /**
422      * {@inheritDoc}
423      * 
424      * @see java.sql.PreparedStatement#setRef(int, java.sql.Ref)
425      */
426     public final void setRef( int i, Ref x )
427         throws SQLException
428     {
429         statement.setRef( i, x );
430     }
431 
432     /**
433      * {@inheritDoc}
434      * 
435      * @see java.sql.PreparedStatement#setShort(int, short)
436      */
437     public final void setShort( int parameterIndex, short x )
438         throws SQLException
439     {
440         statement.setShort( parameterIndex, x );
441     }
442 
443     /**
444      * {@inheritDoc}
445      * 
446      * @see java.sql.PreparedStatement#setString(int, java.lang.String)
447      */
448     public final void setString( int parameterIndex, String x )
449         throws SQLException
450     {
451         statement.setString( parameterIndex, x );
452     }
453 
454     /**
455      * {@inheritDoc}
456      * 
457      * @see java.sql.PreparedStatement#setTime(int, java.sql.Time,
458      * java.util.Calendar)
459      */
460     public final void setTime( int parameterIndex, Time x, Calendar cal )
461         throws SQLException
462     {
463         statement.setTime( parameterIndex, x, cal );
464     }
465 
466     /**
467      * {@inheritDoc}
468      * 
469      * @see java.sql.PreparedStatement#setTime(int, java.sql.Time)
470      */
471     public final void setTime( int parameterIndex, Time x )
472         throws SQLException
473     {
474         statement.setTime( parameterIndex, x );
475     }
476 
477     /**
478      * {@inheritDoc}
479      * 
480      * @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp,
481      * java.util.Calendar)
482      */
483     public final void setTimestamp( int parameterIndex, Timestamp x, Calendar cal )
484         throws SQLException
485     {
486         statement.setTimestamp( parameterIndex, x, cal );
487     }
488 
489     /**
490      * {@inheritDoc}
491      * 
492      * @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp)
493      */
494     public final void setTimestamp( int parameterIndex, Timestamp x )
495         throws SQLException
496     {
497         statement.setTimestamp( parameterIndex, x );
498     }
499 
500     /**
501      * {@inheritDoc}
502      * 
503      * @see java.sql.PreparedStatement#setUnicodeStream(int,
504      * java.io.InputStream, int)
505      */
506     public final void setUnicodeStream( int parameterIndex, InputStream x, int length )
507         throws SQLException
508     {
509         statement.setUnicodeStream( parameterIndex, x, length );
510     }
511 
512     /**
513      * @param parameterIndex
514      * @param x
515      * @param length
516      * @throws SQLException
517      * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream,
518      * long)
519      */
520     public final void setAsciiStream( int parameterIndex, InputStream x, long length )
521         throws SQLException
522     {
523         statement.setAsciiStream( parameterIndex, x, length );
524     }
525 
526     /**
527      * @param parameterIndex
528      * @param x
529      * @throws SQLException
530      * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream)
531      */
532     public final void setAsciiStream( int parameterIndex, InputStream x )
533         throws SQLException
534     {
535         statement.setAsciiStream( parameterIndex, x );
536     }
537 
538     /**
539      * @param parameterIndex
540      * @param x
541      * @param length
542      * @throws SQLException
543      * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream,
544      * long)
545      */
546     public final void setBinaryStream( int parameterIndex, InputStream x, long length )
547         throws SQLException
548     {
549         statement.setBinaryStream( parameterIndex, x, length );
550     }
551 
552     /**
553      * @param parameterIndex
554      * @param x
555      * @throws SQLException
556      * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream)
557      */
558     public final void setBinaryStream( int parameterIndex, InputStream x )
559         throws SQLException
560     {
561         statement.setBinaryStream( parameterIndex, x );
562     }
563 
564     /**
565      * @param parameterIndex
566      * @param inputStream
567      * @param length
568      * @throws SQLException
569      * @see java.sql.PreparedStatement#setBlob(int, java.io.InputStream, long)
570      */
571     public final void setBlob( int parameterIndex, InputStream inputStream, long length )
572         throws SQLException
573     {
574         statement.setBlob( parameterIndex, inputStream, length );
575     }
576 
577     /**
578      * @param parameterIndex
579      * @param inputStream
580      * @throws SQLException
581      * @see java.sql.PreparedStatement#setBlob(int, java.io.InputStream)
582      */
583     public final void setBlob( int parameterIndex, InputStream inputStream )
584         throws SQLException
585     {
586         statement.setBlob( parameterIndex, inputStream );
587     }
588 
589     /**
590      * @param parameterIndex
591      * @param reader
592      * @param length
593      * @throws SQLException
594      * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader,
595      * long)
596      */
597     public final void setCharacterStream( int parameterIndex, Reader reader, long length )
598         throws SQLException
599     {
600         statement.setCharacterStream( parameterIndex, reader, length );
601     }
602 
603     /**
604      * @param parameterIndex
605      * @param reader
606      * @throws SQLException
607      * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader)
608      */
609     public final void setCharacterStream( int parameterIndex, Reader reader )
610         throws SQLException
611     {
612         statement.setCharacterStream( parameterIndex, reader );
613     }
614 
615     /**
616      * @param parameterIndex
617      * @param reader
618      * @param length
619      * @throws SQLException
620      * @see java.sql.PreparedStatement#setClob(int, java.io.Reader, long)
621      */
622     public final void setClob( int parameterIndex, Reader reader, long length )
623         throws SQLException
624     {
625         statement.setClob( parameterIndex, reader, length );
626     }
627 
628     /**
629      * @param parameterIndex
630      * @param reader
631      * @throws SQLException
632      * @see java.sql.PreparedStatement#setClob(int, java.io.Reader)
633      */
634     public final void setClob( int parameterIndex, Reader reader )
635         throws SQLException
636     {
637         statement.setClob( parameterIndex, reader );
638     }
639 
640     /**
641      * @param parameterIndex
642      * @param value
643      * @param length
644      * @throws SQLException
645      * @see java.sql.PreparedStatement#setNCharacterStream(int, java.io.Reader,
646      * long)
647      */
648     public final void setNCharacterStream( int parameterIndex, Reader value, long length )
649         throws SQLException
650     {
651         statement.setNCharacterStream( parameterIndex, value, length );
652     }
653 
654     /**
655      * @param parameterIndex
656      * @param value
657      * @throws SQLException
658      * @see java.sql.PreparedStatement#setNCharacterStream(int, java.io.Reader)
659      */
660     public final void setNCharacterStream( int parameterIndex, Reader value )
661         throws SQLException
662     {
663         statement.setNCharacterStream( parameterIndex, value );
664     }
665 
666     /**
667      * @param parameterIndex
668      * @param value
669      * @throws SQLException
670      * @see java.sql.PreparedStatement#setNClob(int, java.sql.NClob)
671      */
672     public final void setNClob( int parameterIndex, NClob value )
673         throws SQLException
674     {
675         statement.setNClob( parameterIndex, value );
676     }
677 
678     /**
679      * @param parameterIndex
680      * @param reader
681      * @param length
682      * @throws SQLException
683      * @see java.sql.PreparedStatement#setNClob(int, java.io.Reader, long)
684      */
685     public final void setNClob( int parameterIndex, Reader reader, long length )
686         throws SQLException
687     {
688         statement.setNClob( parameterIndex, reader, length );
689     }
690 
691     /**
692      * @param parameterIndex
693      * @param reader
694      * @throws SQLException
695      * @see java.sql.PreparedStatement#setNClob(int, java.io.Reader)
696      */
697     public final void setNClob( int parameterIndex, Reader reader )
698         throws SQLException
699     {
700         statement.setNClob( parameterIndex, reader );
701     }
702 
703     /**
704      * @param parameterIndex
705      * @param value
706      * @throws SQLException
707      * @see java.sql.PreparedStatement#setNString(int, java.lang.String)
708      */
709     public final void setNString( int parameterIndex, String value )
710         throws SQLException
711     {
712         statement.setNString( parameterIndex, value );
713     }
714 
715     /**
716      * @param parameterIndex
717      * @param x
718      * @throws SQLException
719      * @see java.sql.PreparedStatement#setRowId(int, java.sql.RowId)
720      */
721     public final void setRowId( int parameterIndex, RowId x )
722         throws SQLException
723     {
724         statement.setRowId( parameterIndex, x );
725     }
726 
727     /**
728      * @param parameterIndex
729      * @param xmlObject
730      * @throws SQLException
731      * @see java.sql.PreparedStatement#setSQLXML(int, java.sql.SQLXML)
732      */
733     public final void setSQLXML( int parameterIndex, SQLXML xmlObject )
734         throws SQLException
735     {
736         statement.setSQLXML( parameterIndex, xmlObject );
737     }
738 
739     /**
740      * @param parameterIndex
741      * @param x
742      * @throws SQLException
743      * @see java.sql.PreparedStatement#setURL(int, java.net.URL)
744      */
745     public final void setURL( int parameterIndex, URL x )
746         throws SQLException
747     {
748         statement.setURL( parameterIndex, x );
749     }
750 
751     /**
752      * @return
753      * @throws SQLException
754      * @see java.sql.PreparedStatement#getParameterMetaData()
755      */
756     public final ParameterMetaData getParameterMetaData()
757         throws SQLException
758     {
759         return statement.getParameterMetaData();
760     }
761 
762 }