KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > PreparedStatement


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.io.File JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.sql.Array JavaDoc;
29 import java.sql.Blob JavaDoc;
30 import java.sql.Clob JavaDoc;
31 import java.sql.Date JavaDoc;
32 import java.sql.ParameterMetaData JavaDoc;
33 import java.sql.Ref JavaDoc;
34 import java.sql.ResultSetMetaData JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Time JavaDoc;
37 import java.sql.Timestamp JavaDoc;
38 import java.util.Calendar JavaDoc;
39
40 import net.sf.hajdbc.Database;
41 import net.sf.hajdbc.Operation;
42
43 /**
44  * @author Paul Ferraro
45  * @version $Revision: 1042 $
46  * @param <T>
47  * @since 1.0
48  */

49 public class PreparedStatement<T extends java.sql.PreparedStatement JavaDoc> extends Statement<T> implements java.sql.PreparedStatement JavaDoc
50 {
51     private String JavaDoc sql;
52     
53     /**
54      * Constructs a new PreparedStatementProxy.
55      * @param connection a Connection proxy
56      * @param operation an operation that creates PreparedStatements
57      * @param sql an SQL statement
58      * @throws java.sql.SQLException if operation execution fails
59      */

60     public PreparedStatement(Connection<?> connection, Operation<java.sql.Connection JavaDoc, T> operation, String JavaDoc sql) throws java.sql.SQLException JavaDoc
61     {
62         super(connection, operation);
63         
64         this.sql = sql;
65     }
66     
67     /**
68      * @see java.sql.PreparedStatement#addBatch()
69      */

70     public void addBatch() throws SQLException JavaDoc
71     {
72         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
73         {
74             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
75             {
76                 statement.addBatch();
77                 
78                 return null;
79             }
80         };
81         
82         this.executeWriteToDriver(operation);
83     }
84
85     /**
86      * @see java.sql.PreparedStatement#clearParameters()
87      */

88     public void clearParameters() throws SQLException JavaDoc
89     {
90         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
91         {
92             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
93             {
94                 statement.clearParameters();
95                 
96                 return null;
97             }
98         };
99         
100         this.executeWriteToDriver(operation);
101     }
102
103     /**
104      * @see java.sql.PreparedStatement#execute()
105      */

106     public boolean execute() throws SQLException JavaDoc
107     {
108         Operation<T, Boolean JavaDoc> operation = new Operation<T, Boolean JavaDoc>()
109         {
110             public Boolean JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
111             {
112                 return statement.execute();
113             }
114         };
115         
116         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
117     }
118
119     /**
120      * @see java.sql.PreparedStatement#executeQuery()
121      */

122     public java.sql.ResultSet JavaDoc executeQuery() throws SQLException JavaDoc
123     {
124         Operation<T, java.sql.ResultSet JavaDoc> operation = new Operation<T, java.sql.ResultSet JavaDoc>()
125         {
126             public java.sql.ResultSet JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
127             {
128                 return statement.executeQuery();
129             }
130         };
131
132         return ((this.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_READ_ONLY) && !this.isSelectForUpdate(this.sql)) ? this.executeReadFromDatabase(operation) : new ResultSet<T>(this, operation);
133     }
134
135     /**
136      * @see java.sql.PreparedStatement#executeUpdate()
137      */

138     public int executeUpdate() throws SQLException JavaDoc
139     {
140         Operation<T, Integer JavaDoc> operation = new Operation<T, Integer JavaDoc>()
141         {
142             public Integer JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
143             {
144                 return statement.executeUpdate();
145             }
146         };
147         
148         return this.firstValue(this.executeTransactionalWriteToDatabase(operation));
149     }
150
151     /**
152      * @see java.sql.PreparedStatement#getMetaData()
153      */

154     public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc
155     {
156         Operation<T, ResultSetMetaData JavaDoc> operation = new Operation<T, ResultSetMetaData JavaDoc>()
157         {
158             public ResultSetMetaData JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
159             {
160                 return statement.getMetaData();
161             }
162         };
163         
164         return this.executeReadFromDatabase(operation);
165     }
166
167     /**
168      * @see java.sql.PreparedStatement#getParameterMetaData()
169      */

170     public ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc
171     {
172         Operation<T, ParameterMetaData JavaDoc> operation = new Operation<T, ParameterMetaData JavaDoc>()
173         {
174             public ParameterMetaData JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
175             {
176                 return statement.getParameterMetaData();
177             }
178         };
179         
180         return this.executeReadFromDatabase(operation);
181     }
182
183     /**
184      * @see java.sql.PreparedStatement#setArray(int, java.sql.Array)
185      */

186     public void setArray(final int index, final Array JavaDoc value) throws SQLException JavaDoc
187     {
188         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
189         {
190             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
191             {
192                 statement.setArray(index, value);
193                 
194                 return null;
195             }
196         };
197         
198         this.executeWriteToDriver(operation);
199     }
200
201     /**
202      * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream, int)
203      */

204     public void setAsciiStream(final int index, InputStream JavaDoc inputStream, final int length) throws SQLException JavaDoc
205     {
206         final FileSupport fileSupport = this.getFileSupport();
207         final File JavaDoc file = fileSupport.createFile(inputStream);
208         
209         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
210         {
211             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
212             {
213                 statement.setAsciiStream(index, fileSupport.getInputStream(file), length);
214                 
215                 return null;
216             }
217         };
218         
219         this.executeWriteToDriver(operation);
220     }
221
222     /**
223      * @see java.sql.PreparedStatement#setBigDecimal(int, java.math.BigDecimal)
224      */

225     public void setBigDecimal(final int index, final BigDecimal JavaDoc value) throws SQLException JavaDoc
226     {
227         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
228         {
229             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
230             {
231                 statement.setBigDecimal(index, value);
232                 
233                 return null;
234             }
235         };
236         
237         this.executeWriteToDriver(operation);
238     }
239
240     /**
241      * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)
242      */

243     public void setBinaryStream(final int index, InputStream JavaDoc inputStream, final int length) throws SQLException JavaDoc
244     {
245         final FileSupport fileSupport = this.getFileSupport();
246         final File JavaDoc file = fileSupport.createFile(inputStream);
247
248         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
249         {
250             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
251             {
252                 statement.setBinaryStream(index, fileSupport.getInputStream(file), length);
253                 
254                 return null;
255             }
256         };
257         
258         this.executeWriteToDriver(operation);
259     }
260
261     /**
262      * @see java.sql.PreparedStatement#setBlob(int, java.sql.Blob)
263      */

264     public void setBlob(final int index, final Blob JavaDoc value) throws SQLException JavaDoc
265     {
266         final FileSupport fileSupport = this.getFileSupport();
267         final File JavaDoc file = fileSupport.createFile(value);
268         
269         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
270         {
271             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
272             {
273                 statement.setBlob(index, fileSupport.getBlob(file));
274                 
275                 return null;
276             }
277         };
278         
279         this.executeWriteToDriver(operation);
280     }
281
282     /**
283      * @see java.sql.PreparedStatement#setBoolean(int, boolean)
284      */

285     public void setBoolean(final int index, final boolean value) throws SQLException JavaDoc
286     {
287         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
288         {
289             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
290             {
291                 statement.setBoolean(index, value);
292                 
293                 return null;
294             }
295         };
296         
297         this.executeWriteToDriver(operation);
298     }
299
300     /**
301      * @see java.sql.PreparedStatement#setByte(int, byte)
302      */

303     public void setByte(final int index, final byte value) throws SQLException JavaDoc
304     {
305         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
306         {
307             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
308             {
309                 statement.setByte(index, value);
310                 
311                 return null;
312             }
313         };
314         
315         this.executeWriteToDriver(operation);
316     }
317
318     /**
319      * @see java.sql.PreparedStatement#setBytes(int, byte[])
320      */

321     public void setBytes(final int index, final byte[] value) throws SQLException JavaDoc
322     {
323         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
324         {
325             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
326             {
327                 statement.setBytes(index, value);
328                 
329                 return null;
330             }
331         };
332         
333         this.executeWriteToDriver(operation);
334     }
335
336     /**
337      * @see java.sql.PreparedStatement#setCharacterStream(int, java.io.Reader, int)
338      */

339     public void setCharacterStream(final int index, Reader JavaDoc reader, final int length) throws SQLException JavaDoc
340     {
341         final FileSupport fileSupport = this.getFileSupport();
342         final File JavaDoc file = fileSupport.createFile(reader);
343         
344         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
345         {
346             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
347             {
348                 statement.setCharacterStream(index, fileSupport.getReader(file), length);
349                 
350                 return null;
351             }
352         };
353         
354         this.executeWriteToDriver(operation);
355     }
356     
357     /**
358      * @see java.sql.PreparedStatement#setClob(int, java.sql.Clob)
359      */

360     public void setClob(final int index, final Clob JavaDoc value) throws SQLException JavaDoc
361     {
362         final FileSupport fileSupport = this.getFileSupport();
363         final File JavaDoc file = fileSupport.createFile(value);
364         
365         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
366         {
367             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
368             {
369                 statement.setClob(index, fileSupport.getClob(file));
370                 
371                 return null;
372             }
373         };
374         
375         this.executeWriteToDriver(operation);
376     }
377
378     /**
379      * @see java.sql.PreparedStatement#setDate(int, java.sql.Date)
380      */

381     public void setDate(final int index, final Date JavaDoc value) throws SQLException JavaDoc
382     {
383         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
384         {
385             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
386             {
387                 statement.setDate(index, value);
388                 
389                 return null;
390             }
391         };
392         
393         this.executeWriteToDriver(operation);
394     }
395
396     /**
397      * @see java.sql.PreparedStatement#setDate(int, java.sql.Date, java.util.Calendar)
398      */

399     public void setDate(final int index, final Date JavaDoc value, final Calendar JavaDoc calendar) throws SQLException JavaDoc
400     {
401         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
402         {
403             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
404             {
405                 statement.setDate(index, value, calendar);
406                 
407                 return null;
408             }
409         };
410         
411         this.executeWriteToDriver(operation);
412     }
413
414     /**
415      * @see java.sql.PreparedStatement#setDouble(int, double)
416      */

417     public void setDouble(final int index, final double value) throws SQLException JavaDoc
418     {
419         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
420         {
421             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
422             {
423                 statement.setDouble(index, value);
424                 
425                 return null;
426             }
427         };
428         
429         this.executeWriteToDriver(operation);
430     }
431
432     /**
433      * @see java.sql.PreparedStatement#setFloat(int, float)
434      */

435     public void setFloat(final int index, final float value) throws SQLException JavaDoc
436     {
437         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
438         {
439             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
440             {
441                 statement.setFloat(index, value);
442                 
443                 return null;
444             }
445         };
446         
447         this.executeWriteToDriver(operation);
448     }
449
450     /**
451      * @see java.sql.PreparedStatement#setInt(int, int)
452      */

453     public void setInt(final int index, final int value) throws SQLException JavaDoc
454     {
455         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
456         {
457             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
458             {
459                 statement.setInt(index, value);
460                 
461                 return null;
462             }
463         };
464         
465         this.executeWriteToDriver(operation);
466     }
467
468     /**
469      * @see java.sql.PreparedStatement#setLong(int, long)
470      */

471     public void setLong(final int index, final long value) throws SQLException JavaDoc
472     {
473         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
474         {
475             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
476             {
477                 statement.setLong(index, value);
478                 
479                 return null;
480             }
481         };
482         
483         this.executeWriteToDriver(operation);
484     }
485
486     /**
487      * @see java.sql.PreparedStatement#setNull(int, int)
488      */

489     public void setNull(final int index, final int sqlType) throws SQLException JavaDoc
490     {
491         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
492         {
493             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
494             {
495                 statement.setNull(index, sqlType);
496                 
497                 return null;
498             }
499         };
500         
501         this.executeWriteToDriver(operation);
502     }
503
504     /**
505      * @see java.sql.PreparedStatement#setNull(int, int, java.lang.String)
506      */

507     public void setNull(final int index, final int sqlType, final String JavaDoc typeName) throws SQLException JavaDoc
508     {
509         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
510         {
511             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
512             {
513                 statement.setNull(index, sqlType, typeName);
514                 
515                 return null;
516             }
517         };
518         
519         this.executeWriteToDriver(operation);
520     }
521
522     /**
523      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object)
524      */

525     public void setObject(final int index, final Object JavaDoc value) throws SQLException JavaDoc
526     {
527         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
528         {
529             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
530             {
531                 statement.setObject(index, value);
532                 
533                 return null;
534             }
535         };
536         
537         this.executeWriteToDriver(operation);
538     }
539
540     /**
541      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object, int)
542      */

543     public void setObject(final int index, final Object JavaDoc value, final int sqlType) throws SQLException JavaDoc
544     {
545         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
546         {
547             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
548             {
549                 statement.setObject(index, value, sqlType);
550                 
551                 return null;
552             }
553         };
554         
555         this.executeWriteToDriver(operation);
556     }
557
558     /**
559      * @see java.sql.PreparedStatement#setObject(int, java.lang.Object, int, int)
560      */

561     public void setObject(final int index, final Object JavaDoc value, final int sqlType, final int scale) throws SQLException JavaDoc
562     {
563         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
564         {
565             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
566             {
567                 statement.setObject(index, value, sqlType, scale);
568                 
569                 return null;
570             }
571         };
572         
573         this.executeWriteToDriver(operation);
574     }
575
576     /**
577      * @see java.sql.PreparedStatement#setRef(int, java.sql.Ref)
578      */

579     public void setRef(final int index, final Ref JavaDoc value) throws SQLException JavaDoc
580     {
581         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
582         {
583             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
584             {
585                 statement.setRef(index, value);
586                 
587                 return null;
588             }
589         };
590         
591         this.executeWriteToDriver(operation);
592     }
593
594     /**
595      * @see java.sql.PreparedStatement#setShort(int, short)
596      */

597     public void setShort(final int index, final short value) throws SQLException JavaDoc
598     {
599         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
600         {
601             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
602             {
603                 statement.setShort(index, value);
604                 
605                 return null;
606             }
607         };
608         
609         this.executeWriteToDriver(operation);
610     }
611
612     /**
613      * @see java.sql.PreparedStatement#setString(int, java.lang.String)
614      */

615     public void setString(final int index, final String JavaDoc value) throws SQLException JavaDoc
616     {
617         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
618         {
619             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
620             {
621                 statement.setString(index, value);
622                 
623                 return null;
624             }
625         };
626         
627         this.executeWriteToDriver(operation);
628     }
629
630     /**
631      * @see java.sql.PreparedStatement#setTime(int, java.sql.Time)
632      */

633     public void setTime(final int index, final Time JavaDoc value) throws SQLException JavaDoc
634     {
635         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
636         {
637             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
638             {
639                 statement.setTime(index, value);
640                 
641                 return null;
642             }
643         };
644         
645         this.executeWriteToDriver(operation);
646     }
647
648     /**
649      * @see java.sql.PreparedStatement#setTime(int, java.sql.Time, java.util.Calendar)
650      */

651     public void setTime(final int index, final Time JavaDoc value, final Calendar JavaDoc calendar) throws SQLException JavaDoc
652     {
653         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
654         {
655             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
656             {
657                 statement.setTime(index, value, calendar);
658                 
659                 return null;
660             }
661         };
662         
663         this.executeWriteToDriver(operation);
664     }
665
666     /**
667      * @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp)
668      */

669     public void setTimestamp(final int index, final Timestamp JavaDoc value) throws SQLException JavaDoc
670     {
671         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
672         {
673             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
674             {
675                 statement.setTimestamp(index, value);
676                 
677                 return null;
678             }
679         };
680         
681         this.executeWriteToDriver(operation);
682     }
683
684     /**
685      * @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp, java.util.Calendar)
686      */

687     public void setTimestamp(final int index, final Timestamp JavaDoc value, final Calendar JavaDoc calendar) throws SQLException JavaDoc
688     {
689         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
690         {
691             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
692             {
693                 statement.setTimestamp(index, value, calendar);
694                 
695                 return null;
696             }
697         };
698         
699         this.executeWriteToDriver(operation);
700     }
701
702     /**
703      * @see java.sql.PreparedStatement#setUnicodeStream(int, java.io.InputStream, int)
704      * @deprecated
705      */

706     @Deprecated JavaDoc
707     public void setUnicodeStream(final int index, InputStream JavaDoc inputStream, final int length) throws SQLException JavaDoc
708     {
709         final FileSupport fileSupport = this.getFileSupport();
710         final File JavaDoc file = fileSupport.createFile(inputStream);
711
712         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
713         {
714             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
715             {
716                 statement.setUnicodeStream(index, fileSupport.getInputStream(file), length);
717                 
718                 return null;
719             }
720         };
721         
722         this.executeWriteToDriver(operation);
723     }
724     
725     /**
726      * @see java.sql.PreparedStatement#setURL(int, java.net.URL)
727      */

728     public void setURL(final int index, final URL JavaDoc value) throws SQLException JavaDoc
729     {
730         Operation<T, Void JavaDoc> operation = new Operation<T, Void JavaDoc>()
731         {
732             public Void JavaDoc execute(Database database, T statement) throws SQLException JavaDoc
733             {
734                 statement.setURL(index, value);
735                 
736                 return null;
737             }
738         };
739         
740         this.executeWriteToDriver(operation);
741     }
742     
743     protected FileSupport getFileSupport()
744     {
745         Connection connection = (Connection) this.getConnection();
746         
747         return connection.getFileSupport();
748     }
749 }
750
Popular Tags