KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > core > CorePreparedStatement


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.core;
23
24 import org.enhydra.jdbc.util.Logger;
25 import org.enhydra.jdbc.util.JdbcUtil;
26
27 import java.io.InputStream JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30 import java.sql.Array JavaDoc;
31 import java.sql.Blob JavaDoc;
32 import java.sql.Clob JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.Date JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.Ref JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.ResultSetMetaData JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.SQLWarning JavaDoc;
41 import java.sql.Time JavaDoc;
42 import java.sql.Timestamp JavaDoc;
43 import java.util.Calendar JavaDoc;
44
45 /**
46  * A very simple implementation of PreparedStatement. When created
47  * it is supplied with another PreparedStatement to which all
48  * of this class' methods delegate their work.
49  */

50 public abstract class CorePreparedStatement
51     extends JdbcUtil
52     implements PreparedStatement JavaDoc {
53
54     public PreparedStatement JavaDoc ps;
55     // the PreparedStatement which does most of the work.
56

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

401     public void close() throws SQLException JavaDoc {
402         if (ps != null) {
403             ps.close();
404         }
405     }
406
407     public int[] executeBatch() throws SQLException JavaDoc {
408         //preInvoke();
409
try {
410             return ps.executeBatch();
411         } catch (SQLException JavaDoc e) {
412             catchInvoke(e);
413         }
414         return null;
415     }
416
417     public int getMaxFieldSize() throws SQLException JavaDoc {
418         //preInvoke();
419
try {
420             return ps.getMaxFieldSize();
421         } catch (SQLException JavaDoc e) {
422             catchInvoke(e);
423         }
424         return 0;
425     }
426
427     public void setMaxFieldSize(int max) throws SQLException JavaDoc {
428         //preInvoke();
429
try {
430             ps.setMaxFieldSize(max);
431         } catch (SQLException JavaDoc e) {
432             catchInvoke(e);
433         }
434     }
435
436     public int getMaxRows() throws SQLException JavaDoc {
437         //preInvoke();
438
try {
439             return ps.getMaxRows();
440         } catch (SQLException JavaDoc e) {
441             catchInvoke(e);
442         }
443         return 0;
444     }
445
446     public void setMaxRows(int max) throws SQLException JavaDoc {
447         //preInvoke();
448
try {
449             ps.setMaxRows(max);
450         } catch (SQLException JavaDoc e) {
451             catchInvoke(e);
452         }
453     }
454
455     public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc {
456         //preInvoke();
457
try {
458             ps.setEscapeProcessing(enable);
459         } catch (SQLException JavaDoc e) {
460             catchInvoke(e);
461         }
462     }
463
464     public int getQueryTimeout() throws SQLException JavaDoc {
465         //preInvoke();
466
try {
467             return ps.getQueryTimeout();
468         } catch (SQLException JavaDoc e) {
469             catchInvoke(e);
470         }
471         return 0;
472     }
473
474     public void setQueryTimeout(int seconds) throws SQLException JavaDoc {
475         //preInvoke();
476
try {
477             ps.setQueryTimeout(seconds);
478         } catch (SQLException JavaDoc e) {
479             catchInvoke(e);
480         }
481     }
482
483     public void cancel() throws SQLException JavaDoc {
484         //preInvoke();
485
try {
486             ps.cancel();
487         } catch (SQLException JavaDoc e) {
488             catchInvoke(e);
489         }
490     }
491
492     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
493         //preInvoke();
494
try {
495             return ps.getWarnings();
496         } catch (SQLException JavaDoc e) {
497             catchInvoke(e);
498         }
499         return null;
500     }
501
502     public void clearWarnings() throws SQLException JavaDoc {
503         //preInvoke();
504
try {
505             ps.clearWarnings();
506         } catch (SQLException JavaDoc e) {
507             catchInvoke(e);
508         }
509     }
510
511     public void setCursorName(String JavaDoc name) throws SQLException JavaDoc {
512         //preInvoke();
513
try {
514             ps.setCursorName(name);
515         } catch (SQLException JavaDoc e) {
516             catchInvoke(e);
517         }
518     }
519
520     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
521         //preInvoke();
522
try {
523             return ps.getResultSet();
524         } catch (SQLException JavaDoc e) {
525             catchInvoke(e);
526         }
527         return null;
528     }
529
530     public int getUpdateCount() throws SQLException JavaDoc {
531         //preInvoke();
532
try {
533             return ps.getUpdateCount();
534         } catch (SQLException JavaDoc e) {
535             catchInvoke(e);
536         }
537         return 0;
538     }
539
540     public boolean getMoreResults() throws SQLException JavaDoc {
541         //preInvoke();
542
try {
543             return ps.getMoreResults();
544         } catch (SQLException JavaDoc e) {
545             catchInvoke(e);
546         }
547         return false;
548     }
549
550     public void setFetchDirection(int direction) throws SQLException JavaDoc {
551         //preInvoke();
552
try {
553             ps.setFetchDirection(direction);
554         } catch (SQLException JavaDoc e) {
555             catchInvoke(e);
556         }
557     }
558
559     public int getFetchDirection() throws SQLException JavaDoc {
560         //preInvoke();
561
try {
562             return ps.getFetchDirection();
563         } catch (SQLException JavaDoc e) {
564             catchInvoke(e);
565         }
566         return 0;
567     }
568
569     public void setFetchSize(int rows) throws SQLException JavaDoc {
570         //preInvoke();
571
try {
572             ps.setFetchSize(rows);
573         } catch (SQLException JavaDoc e) {
574             catchInvoke(e);
575         }
576     }
577
578     public int getFetchSize() throws SQLException JavaDoc {
579         //preInvoke();
580
try {
581             return ps.getFetchSize();
582         } catch (SQLException JavaDoc e) {
583             catchInvoke(e);
584         }
585         return 0;
586     }
587
588     public int getResultSetConcurrency() throws SQLException JavaDoc {
589         //preInvoke();
590
try {
591             return ps.getResultSetConcurrency();
592         } catch (SQLException JavaDoc e) {
593             catchInvoke(e);
594         }
595         return 0;
596     }
597
598     public int getResultSetType() throws SQLException JavaDoc {
599         //preInvoke();
600
try {
601             return ps.getResultSetType();
602         } catch (SQLException JavaDoc e) {
603             catchInvoke(e);
604         }
605         return 0;
606     }
607
608     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
609         //preInvoke();
610
try {
611             return ps.getConnection();
612         } catch (SQLException JavaDoc e) {
613             catchInvoke(e);
614         }
615         return null;
616     }
617
618     public void clearBatch() throws SQLException JavaDoc {
619         //preInvoke();
620
try {
621             ps.clearBatch();
622         } catch (SQLException JavaDoc e) {
623             catchInvoke(e);
624         }
625     }
626
627     public void addBatch(String JavaDoc s) throws SQLException JavaDoc {
628         //preInvoke();
629
try {
630             ps.addBatch(s);
631         } catch (SQLException JavaDoc e) {
632             catchInvoke(e);
633         }
634     }
635
636     public boolean execute(String JavaDoc s) throws SQLException JavaDoc {
637         //preInvoke();
638
try {
639             return ps.execute(s);
640         } catch (SQLException JavaDoc e) {
641             catchInvoke(e);
642         }
643         return false;
644     }
645
646     public ResultSet JavaDoc executeQuery(String JavaDoc s) throws SQLException JavaDoc {
647         //preInvoke();
648
try {
649             return ps.executeQuery(s);
650         } catch (SQLException JavaDoc e) {
651             catchInvoke(e);
652         }
653         return null;
654     }
655
656     public int executeUpdate(String JavaDoc s) throws SQLException JavaDoc {
657         //preInvoke();
658
try {
659             return ps.executeUpdate(s);
660         } catch (SQLException JavaDoc e) {
661             catchInvoke(e);
662         }
663         return 0;
664     }
665
666     /*
667     * Add those following methods to compile on JDK 1.4.
668     * Instead those methods are defined in the java.sql.PreparedStatement interface
669     * only since JDK 1.4.
670     */

671     // java.sql.Statements methods
672
public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
673             try {
674             return ps.execute(sql, autoGeneratedKeys);
675         } catch (SQLException JavaDoc e) {
676             catchInvoke(e);
677         }
678         return false;
679     }
680     public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
681         try {
682             return ps.execute(sql, columnIndexes);
683         } catch (SQLException JavaDoc e) {
684             catchInvoke(e);
685         }
686         return false;
687     }
688     public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
689         try {
690             return ps.execute(sql, columnNames);
691         } catch (SQLException JavaDoc e) {
692             catchInvoke(e);
693         }
694         return false;
695     }
696     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
697         try {
698             return ps.executeUpdate(sql, autoGeneratedKeys);
699         } catch (SQLException JavaDoc e) {
700             catchInvoke(e);
701         }
702         return 0;
703     }
704     public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
705         try {
706             return ps.executeUpdate(sql, columnIndexes);
707         } catch (SQLException JavaDoc e) {
708             catchInvoke(e);
709         }
710         return 0;
711     }
712     public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
713         try {
714             return ps.executeUpdate(sql, columnNames);
715         } catch (SQLException JavaDoc e) {
716             catchInvoke(e);
717         }
718         return 0;
719     }
720     public java.sql.ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
721         try {
722             return ps.getGeneratedKeys();
723         } catch (SQLException JavaDoc e) {
724             catchInvoke(e);
725         }
726         return null;
727     }
728     public boolean getMoreResults(int current) throws SQLException JavaDoc {
729         try {
730             return ps.getMoreResults(current);
731         } catch (SQLException JavaDoc e) {
732             catchInvoke(e);
733         }
734         return false;
735     }
736     public int getResultSetHoldability() throws SQLException JavaDoc {
737         try {
738             return ps.getResultSetHoldability();
739         } catch (SQLException JavaDoc e) {
740             catchInvoke(e);
741         }
742         return 0;
743     }
744     // java.sql.PreparedStatement methods
745
public java.sql.ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc {
746         try {
747             return ps.getParameterMetaData();
748         } catch (SQLException JavaDoc e) {
749             catchInvoke(e);
750         }
751         return null;
752     }
753     public void setURL(int parameterIndex, java.net.URL JavaDoc x) throws SQLException JavaDoc {
754         try {
755             ps.setURL(parameterIndex, x);
756         } catch (SQLException JavaDoc e) {
757             catchInvoke(e);
758         }
759     }
760
761     /**
762      * Methods used to do some works before and during the catch
763      * clause, to prevent the pool that a connection is broken.
764      */

765     abstract public void preInvoke() throws SQLException JavaDoc;
766     abstract public void catchInvoke(SQLException JavaDoc e) throws SQLException JavaDoc;
767
768 }
Popular Tags