KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > jdbc > JdbcPreparedStatement


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.jdbc;
6
7 import java.io.InputStream JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.math.BigDecimal JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.sql.Array JavaDoc;
12 import java.sql.Blob JavaDoc;
13 import java.sql.Clob JavaDoc;
14 //#ifdef JDK14
15
import java.sql.ParameterMetaData JavaDoc;
16 import java.sql.Statement JavaDoc;
17 //#endif
18
import java.sql.PreparedStatement JavaDoc;
19 import java.sql.Ref JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.ResultSetMetaData JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.Calendar JavaDoc;
24
25 import org.h2.command.CommandInterface;
26 import org.h2.engine.SessionInterface;
27 import org.h2.expression.ParameterInterface;
28 import org.h2.message.Message;
29 import org.h2.message.TraceObject;
30 import org.h2.result.ResultInterface;
31 import org.h2.util.DateTimeUtils;
32 import org.h2.util.IOUtils;
33 import org.h2.util.ObjectArray;
34 import org.h2.value.DataType;
35 import org.h2.value.Value;
36 import org.h2.value.ValueBoolean;
37 import org.h2.value.ValueByte;
38 import org.h2.value.ValueBytes;
39 import org.h2.value.ValueDate;
40 import org.h2.value.ValueDecimal;
41 import org.h2.value.ValueDouble;
42 import org.h2.value.ValueFloat;
43 import org.h2.value.ValueInt;
44 import org.h2.value.ValueLong;
45 import org.h2.value.ValueNull;
46 import org.h2.value.ValueShort;
47 import org.h2.value.ValueString;
48 import org.h2.value.ValueTime;
49 import org.h2.value.ValueTimestamp;
50
51 //#ifdef JDK16
52
/*
53 import java.sql.RowId;
54 import java.sql.NClob;
55 import java.sql.SQLXML;
56 */

57 //#endif
58

59 /**
60  * Represents a prepared statement.
61  *
62  */

63 public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement JavaDoc {
64
65     private CommandInterface command;
66     private ObjectArray batchParameters;
67
68     /**
69      * Executes a query (select statement) and returns the result set. If another result set exists for this statement,
70      * this will be closed (even if this statement fails).
71      *
72      * @return the result set
73      * @throws SQLException if this object is closed or invalid
74      */

75     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
76         try {
77             int id = getNextId(TraceObject.RESULT_SET);
78             if(debug()) {
79                 debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id);
80                 debugCodeCall("executeQuery");
81             }
82             checkClosed();
83             closeOld();
84             ResultInterface result;
85             synchronized(session) {
86                 try {
87                     setExecutingStatement(command);
88                     boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
89                     result = command.executeQuery(maxRows, scrollable);
90                 } finally {
91                     setExecutingStatement(null);
92                 }
93             }
94             resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet);
95             return resultSet;
96         } catch(Throwable JavaDoc e) {
97             throw logAndConvert(e);
98         }
99     }
100
101     /**
102      * Executes a statement (insert, update, delete, create, drop, commit, rollback) and returns the update count. If
103      * another result set exists for this statement, this will be closed (even if this statement fails).
104      *
105      * If the statement is a create or drop and does not throw an exception, the current transaction (if any) is
106      * committed after executing the statement. If autocommit is on, this statement will be committed.
107      *
108      * @return the update count (number of row affected by an insert, update or delete, or 0 if no rows or the statement
109      * was a create, drop, commit or rollback)
110      * @throws SQLException if this object is closed or invalid
111      */

112     public int executeUpdate() throws SQLException JavaDoc {
113         try {
114             debugCodeCall("executeUpdate");
115             checkClosed();
116             return executeUpdateInternal();
117         } catch(Throwable JavaDoc e) {
118             throw logAndConvert(e);
119         }
120     }
121
122     private int executeUpdateInternal() throws SQLException JavaDoc {
123         closeOld();
124         synchronized(session) {
125             try {
126                 setExecutingStatement(command);
127                 updateCount = command.executeUpdate();
128             } finally {
129                 setExecutingStatement(null);
130             }
131         }
132         return updateCount;
133     }
134
135     /**
136      * Executes an arbitrary statement. If another result set exists for this statement, this will be closed (even if
137      * this statement fails). If autocommit is on, and the statement is not a select, this statement will be committed.
138      *
139      * @return true if a result set is available, false if not
140      * @throws SQLException if this object is closed or invalid
141      */

142     public boolean execute() throws SQLException JavaDoc {
143         try {
144             int id = getNextId(TraceObject.RESULT_SET);
145             if(debug()) {
146                 debugCodeCall("execute");
147             }
148             checkClosed();
149             closeOld();
150             boolean returnsResultSet;
151             synchronized(session) {
152                 try {
153                     setExecutingStatement(command);
154                     if (command.isQuery()) {
155                         returnsResultSet = true;
156                         boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY;
157                         ResultInterface result = command.executeQuery(maxRows, scrollable);
158                         resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet);
159                     } else {
160                         returnsResultSet = false;
161                         updateCount = command.executeUpdate();
162                     }
163                 } finally {
164                     setExecutingStatement(null);
165                 }
166             }
167             return returnsResultSet;
168         } catch(Throwable JavaDoc e) {
169             throw logAndConvert(e);
170         }
171     }
172
173     /**
174      * Clears all parameters.
175      *
176      * @throws SQLException if this object is closed or invalid
177      */

178     public void clearParameters() throws SQLException JavaDoc {
179         try {
180             debugCodeCall("clearParameters");
181             checkClosed();
182             ObjectArray parameters = command.getParameters();
183             for (int i = 0; i < parameters.size(); i++) {
184                 ParameterInterface param = (ParameterInterface) parameters.get(i);
185                 param.setValue(null);
186             }
187         } catch(Throwable JavaDoc e) {
188             throw logAndConvert(e);
189         }
190     }
191
192     /**
193      * Calling this method is not legal on a PreparedStatement.
194      *
195      * @throws SQLException Unsupported Feature (SQL State 0A000)
196      */

197     public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
198         try {
199             debugCodeCall("executeQuery", sql);
200             throw Message.getUnsupportedException();
201         } catch(Throwable JavaDoc e) {
202             throw logAndConvert(e);
203         }
204     }
205
206     /**
207      * Calling this method is not legal on a PreparedStatement.
208      *
209      * @throws SQLException Unsupported Feature (SQL State 0A000)
210      */

211     public void addBatch(String JavaDoc sql) throws SQLException JavaDoc {
212         try {
213             debugCodeCall("addBatch", sql);
214             throw Message.getUnsupportedException();
215         } catch(Throwable JavaDoc e) {
216             throw logAndConvert(e);
217         }
218     }
219
220     /**
221      * Calling this method is not legal on a PreparedStatement.
222      *
223      * @throws SQLException Unsupported Feature (SQL State 0A000)
224      */

225     public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
226         try {
227             debugCodeCall("executeUpdate", sql);
228             throw Message.getUnsupportedException();
229         } catch(Throwable JavaDoc e) {
230             throw logAndConvert(e);
231         }
232     }
233
234     /**
235      * Calling this method is not legal on a PreparedStatement.
236      *
237      * @throws SQLException Unsupported Feature (SQL State 0A000)
238      */

239     public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
240         try {
241             debugCodeCall("execute", sql);
242             throw Message.getUnsupportedException();
243         } catch(Throwable JavaDoc e) {
244             throw logAndConvert(e);
245         }
246     }
247
248     // =============================================================
249

250     /**
251      * Sets a parameter to null.
252      *
253      * @param parameterIndex the parameter index (1, 2, ...)
254      * @param sqlType the data type (Types.xxx)
255      * @throws SQLException if this object is closed
256      */

257     public void setNull(int parameterIndex, int sqlType) throws SQLException JavaDoc {
258         try {
259             if(debug()) {
260                 debugCode("setNull("+parameterIndex+", "+sqlType+");");
261             }
262             setParameter(parameterIndex, ValueNull.INSTANCE);
263         } catch(Throwable JavaDoc e) {
264             throw logAndConvert(e);
265         }
266     }
267
268     /**
269      * Sets the value of a parameter.
270      *
271      * @param parameterIndex the parameter index (1, 2, ...)
272      * @param x the value
273      * @throws SQLException if this object is closed
274      */

275     public void setInt(int parameterIndex, int x) throws SQLException JavaDoc {
276         try {
277             if(debug()) {
278                 debugCode("setInt("+parameterIndex+", "+x+");");
279             }
280             setParameter(parameterIndex, ValueInt.get(x));
281         } catch(Throwable JavaDoc e) {
282             throw logAndConvert(e);
283         }
284     }
285
286     /**
287      * Sets the value of a parameter.
288      *
289      * @param parameterIndex the parameter index (1, 2, ...)
290      * @param x the value
291      * @throws SQLException if this object is closed
292      */

293     public void setString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc {
294         try {
295             if(debug()) {
296                 debugCode("setString("+parameterIndex+", "+quote(x)+");");
297             }
298             Value v = x == null ? (Value) ValueNull.INSTANCE : ValueString.get(x);
299             setParameter(parameterIndex, v);
300         } catch(Throwable JavaDoc e) {
301             throw logAndConvert(e);
302         }
303     }
304
305     /**
306      * Sets the value of a parameter.
307      *
308      * @param parameterIndex the parameter index (1, 2, ...)
309      * @param x the value
310      * @throws SQLException if this object is closed
311      */

312     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException JavaDoc {
313         try {
314             if(debug()) {
315                 debugCode("setBigDecimal("+parameterIndex+", " + quoteBigDecimal(x) + ");");
316             }
317             Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDecimal.get(x);
318             setParameter(parameterIndex, v);
319         } catch(Throwable JavaDoc e) {
320             throw logAndConvert(e);
321         }
322     }
323
324     /**
325      * Sets the value of a parameter.
326      *
327      * @param parameterIndex the parameter index (1, 2, ...)
328      * @param x the value
329      * @throws SQLException if this object is closed
330      */

331     public void setDate(int parameterIndex, java.sql.Date JavaDoc x) throws SQLException JavaDoc {
332         try {
333             if(debug()) {
334                 debugCode("setDate("+parameterIndex+", " + quoteDate(x) + ");");
335             }
336             Value v = x == null ? (Value) ValueNull.INSTANCE : ValueDate.get(x);
337             setParameter(parameterIndex, v);
338         } catch(Throwable JavaDoc e) {
339             throw logAndConvert(e);
340         }
341     }
342
343     /**
344      * Sets the value of a parameter.
345      *
346      * @param parameterIndex the parameter index (1, 2, ...)
347      * @param x the value
348      * @throws SQLException if this object is closed
349      */

350     public void setTime(int parameterIndex, java.sql.Time JavaDoc x) throws SQLException JavaDoc {
351         try {
352             if(debug()) {
353                 debugCode("setTime("+parameterIndex+", " + quoteTime(x) + ");");
354             }
355             Value v = x == null ? (Value) ValueNull.INSTANCE : ValueTime.get(x);
356             setParameter(parameterIndex, v);
357         } catch(Throwable JavaDoc e) {
358             throw logAndConvert(e);
359         }
360     }
361
362     /**
363      * Sets the value of a parameter.
364      *
365      * @param parameterIndex the parameter index (1, 2, ...)
366      * @param x the value
367      * @throws SQLException if this object is closed
368      */

369     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x) throws SQLException JavaDoc {
370         try {
371             if(debug()) {
372                 debugCode("setTimestamp("+parameterIndex+", " + quoteTimestamp(x) + ");");
373             }
374             Value v = x == null ? (Value) ValueNull.INSTANCE : ValueTimestamp.get(x);
375             setParameter(parameterIndex, v);
376         } catch(Throwable JavaDoc e) {
377             throw logAndConvert(e);
378         }
379     }
380
381     /**
382      * Sets the value of a parameter.
383      *
384      * @param parameterIndex the parameter index (1, 2, ...)
385      * @param x the value
386      * @throws SQLException if this object is closed
387      */

388     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc {
389         try {
390             if(debug()) {
391                 debugCode("setObject("+parameterIndex+", x);");
392             }
393             if (x == null) {
394                 // throw Errors.getInvalidValueException("null", "x");
395
setParameter(parameterIndex, ValueNull.INSTANCE);
396             } else {
397                 setParameter(parameterIndex, DataType.convertToValue(session, x, Value.UNKNOWN));
398             }
399         } catch(Throwable JavaDoc e) {
400             throw logAndConvert(e);
401         }
402     }
403
404     /**
405      * Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to
406      * the database.
407      *
408      * @param parameterIndex the parameter index (1, 2, ...)
409      * @param x the value, null is allowed
410      * @param targetSqlType the type as defined in java.sql.Types
411      * @throws SQLException if this object is closed
412      */

413     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException JavaDoc {
414         try {
415             if(debug()) {
416                 debugCode("setObject("+parameterIndex+", x, "+targetSqlType+");");
417             }
418             int type = DataType.convertSQLTypeToValueType(targetSqlType);
419             if (x == null) {
420                 setParameter(parameterIndex, ValueNull.INSTANCE);
421             } else {
422                 Value v = DataType.convertToValue(session, x, type);
423                 setParameter(parameterIndex, v.convertTo(type));
424             }
425         } catch(Throwable JavaDoc e) {
426             throw logAndConvert(e);
427         }
428     }
429
430     /**
431      * Sets the value of a parameter. The object is converted, if required, to the specified data type before sending to
432      * the database.
433      *
434      * @param parameterIndex the parameter index (1, 2, ...)
435      * @param x the value, null is allowed
436      * @param targetSqlType the type as defined in java.sql.Types
437      * @param scale is ignored
438      * @throws SQLException if this object is closed
439      */

440     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException JavaDoc {
441         try {
442             if(debug()) {
443                 debugCode("setObject("+parameterIndex+", x, "+targetSqlType+", "+scale+");");
444             }
445             setObject(parameterIndex, x, targetSqlType);
446         } catch(Throwable JavaDoc e) {
447             throw logAndConvert(e);
448         }
449     }
450
451     /**
452      * Sets the value of a parameter.
453      *
454      * @param parameterIndex the parameter index (1, 2, ...)
455      * @param x the value
456      * @throws SQLException if this object is closed
457      */

458     public void setBoolean(int parameterIndex, boolean x) throws SQLException JavaDoc {
459         try {
460             if(debug()) {
461                 debugCode("setBoolean("+parameterIndex+", "+x+");");
462             }
463             setParameter(parameterIndex, ValueBoolean.get(x));
464         } catch(Throwable JavaDoc e) {
465             throw logAndConvert(e);
466         }
467     }
468
469     /**
470      * Sets the value of a parameter.
471      *
472      * @param parameterIndex the parameter index (1, 2, ...)
473      * @param x the value
474      * @throws SQLException if this object is closed
475      */

476     public void setByte(int parameterIndex, byte x) throws SQLException JavaDoc {
477         try {
478             if(debug()) {
479                 debugCode("setByte("+parameterIndex+", "+x+");");
480             }
481             setParameter(parameterIndex, ValueByte.get(x));
482         } catch(Throwable JavaDoc e) {
483             throw logAndConvert(e);
484         }
485     }
486
487     /**
488      * Sets the value of a parameter.
489      *
490      * @param parameterIndex the parameter index (1, 2, ...)
491      * @param x the value
492      * @throws SQLException if this object is closed
493      */

494     public void setShort(int parameterIndex, short x) throws SQLException JavaDoc {
495         try {
496             if(debug()) {
497                 debugCode("setShort("+parameterIndex+", "+x+");");
498             }
499             setParameter(parameterIndex, ValueShort.get(x));
500         } catch(Throwable JavaDoc e) {
501             throw logAndConvert(e);
502         }
503     }
504
505     /**
506      * Sets the value of a parameter.
507      *
508      * @param parameterIndex the parameter index (1, 2, ...)
509      * @param x the value
510      * @throws SQLException if this object is closed
511      */

512     public void setLong(int parameterIndex, long x) throws SQLException JavaDoc {
513         try {
514             if(debug()) {
515                 debugCode("setLong("+parameterIndex+", "+x+");");
516             }
517             setParameter(parameterIndex, ValueLong.get(x));
518         } catch(Throwable JavaDoc e) {
519             throw logAndConvert(e);
520         }
521     }
522
523     /**
524      * Sets the value of a parameter.
525      *
526      * @param parameterIndex the parameter index (1, 2, ...)
527      * @param x the value
528      * @throws SQLException if this object is closed
529      */

530     public void setFloat(int parameterIndex, float x) throws SQLException JavaDoc {
531         try {
532             if(debug()) {
533                 debugCode("setFloat("+parameterIndex+", "+x+"f);");
534             }
535             setParameter(parameterIndex, ValueFloat.get(x));
536         } catch(Throwable JavaDoc e) {
537             throw logAndConvert(e);
538         }
539     }
540
541     /**
542      * Sets the value of a parameter.
543      *
544      * @param parameterIndex the parameter index (1, 2, ...)
545      * @param x the value
546      * @throws SQLException if this object is closed
547      */

548     public void setDouble(int parameterIndex, double x) throws SQLException JavaDoc {
549         try {
550             if(debug()) {
551                 debugCode("setDouble("+parameterIndex+", "+x+");");
552             }
553             setParameter(parameterIndex, ValueDouble.get(x));
554         } catch(Throwable JavaDoc e) {
555             throw logAndConvert(e);
556         }
557     }
558
559     /**
560      * Sets the value of a column as a reference.
561      *
562      * @throws SQLException Unsupported Feature (SQL State 0A000)
563      */

564     public void setRef(int parameterIndex, Ref JavaDoc x) throws SQLException JavaDoc {
565         try {
566             if(debug()) {
567                 debugCode("setRef("+parameterIndex+", x);");
568             }
569             throw Message.getUnsupportedException();
570         } catch(Throwable JavaDoc e) {
571             throw logAndConvert(e);
572         }
573     }
574
575     /**
576      * Sets the date using a specified timezone. The value will be converted to the local timezone.
577      *
578      * @param parameterIndex the parameter index (1, 2, ...)
579      * @param x the value
580      * @param calendar the calendar
581      * @throws SQLException if this object is closed
582      */

583     public void setDate(int parameterIndex, java.sql.Date JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
584         try {
585             if(debug()) {
586                 debugCode("setDate("+parameterIndex+", " + quoteDate(x) + ", calendar);");
587             }
588             if (x == null) {
589                 setParameter(parameterIndex, ValueNull.INSTANCE);
590             } else {
591                 setParameter(parameterIndex, DateTimeUtils.convertDateToUniversal(x, calendar));
592             }
593         } catch(Throwable JavaDoc e) {
594             throw logAndConvert(e);
595         }
596     }
597
598     /**
599      * Sets the time using a specified timezone. The value will be converted to the local timezone.
600      *
601      * @param parameterIndex the parameter index (1, 2, ...)
602      * @param x the value
603      * @param calendar the calendar
604      * @throws SQLException if this object is closed
605      */

606     public void setTime(int parameterIndex, java.sql.Time JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
607         try {
608             if(debug()) {
609                 debugCode("setTime("+parameterIndex+", " + quoteTime(x) + ", calendar);");
610             }
611             if (x == null) {
612                 setParameter(parameterIndex, ValueNull.INSTANCE);
613             } else {
614                 setParameter(parameterIndex, DateTimeUtils.convertTimeToUniversal(x, calendar));
615             }
616         } catch(Throwable JavaDoc e) {
617             throw logAndConvert(e);
618         }
619     }
620
621     /**
622      * Sets the timestamp using a specified timezone. The value will be converted to the local timezone.
623      *
624      * @param parameterIndex the parameter index (1, 2, ...)
625      * @param x the value
626      * @param calendar the calendar
627      * @throws SQLException if this object is closed
628      */

629     public void setTimestamp(int parameterIndex, java.sql.Timestamp JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
630         try {
631             if(debug()) {
632                 debugCode("setTimestamp("+parameterIndex+", " + quoteTimestamp(x) + ", calendar);");
633             }
634             if (x == null) {
635                 setParameter(parameterIndex, ValueNull.INSTANCE);
636             } else {
637                 setParameter(parameterIndex, DateTimeUtils.convertTimestampToUniversal(x, calendar));
638             }
639         } catch(Throwable JavaDoc e) {
640             throw logAndConvert(e);
641         }
642     }
643
644     /**
645      * This feature is deprecated and not supported.
646      *
647      * @deprecated
648      *
649      * @throws SQLException Unsupported Feature (SQL State 0A000)
650      */

651     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
652         try {
653             if(debug()) {
654                 debugCode("setUnicodeStream("+parameterIndex+", x, "+length+");");
655             }
656             throw Message.getUnsupportedException();
657         } catch(Throwable JavaDoc e) {
658             throw logAndConvert(e);
659         }
660     }
661
662     /**
663      * Sets a parameter to null.
664      *
665      * @param parameterIndex the parameter index (1, 2, ...)
666      * @param sqlType the data type (Types.xxx)
667      * @param typeName this parameter is ignored
668      * @throws SQLException if this object is closed
669      */

670     public void setNull(int parameterIndex, int sqlType, String JavaDoc typeName) throws SQLException JavaDoc {
671         try {
672             if(debug()) {
673                 debugCode("setNull("+parameterIndex+", "+sqlType+", "+quote(typeName)+");");
674             }
675             setNull(parameterIndex, sqlType);
676         } catch(Throwable JavaDoc e) {
677             throw logAndConvert(e);
678         }
679     }
680
681     /**
682      * Sets the value of a parameter as a Blob.
683      *
684      * @param parameterIndex the parameter index (1, 2, ...)
685      * @param x the value
686      * @throws SQLException if this object is closed
687      */

688     public void setBlob(int parameterIndex, Blob JavaDoc x) throws SQLException JavaDoc {
689         try {
690             if(debug()) {
691                 debugCode("setBlob("+parameterIndex+", x);");
692             }
693             checkClosed();
694             Value v;
695             if(x == null) {
696                 v = ValueNull.INSTANCE;
697             } else {
698                 v = conn.createBlob(x.getBinaryStream(), -1);
699             }
700             setParameter(parameterIndex, v);
701         } catch(Throwable JavaDoc e) {
702             throw logAndConvert(e);
703         }
704     }
705
706     /**
707      * Sets the value of a parameter as a Blob.
708      *
709      * @param parameterIndex the parameter index (1, 2, ...)
710      * @param x the value
711      * @throws SQLException if this object is closed
712      */

713     public void setBlob(int parameterIndex, InputStream JavaDoc x) throws SQLException JavaDoc {
714         try {
715             if(debug()) {
716                 debugCode("setBlob("+parameterIndex+", x);");
717             }
718             checkClosed();
719             Value v = conn.createBlob(x, -1);
720             setParameter(parameterIndex, v);
721         } catch(Throwable JavaDoc e) {
722             throw logAndConvert(e);
723         }
724     }
725
726     /**
727      * Sets the value of a parameter as a Clob.
728      *
729      * @param parameterIndex the parameter index (1, 2, ...)
730      * @param x the value
731      * @throws SQLException if this object is closed
732      */

733     public void setClob(int parameterIndex, Clob JavaDoc x) throws SQLException JavaDoc {
734         try {
735             if(debug()) {
736                 debugCode("setClob("+parameterIndex+", x);");
737             }
738             checkClosed();
739             Value v;
740             if(x == null) {
741                 v = ValueNull.INSTANCE;
742             } else {
743                 v = conn.createClob(x.getCharacterStream(), -1);
744             }
745             setParameter(parameterIndex, v);
746         } catch(Throwable JavaDoc e) {
747             throw logAndConvert(e);
748         }
749     }
750     
751     /**
752      * Sets the value of a parameter as a Clob.
753      *
754      * @param parameterIndex the parameter index (1, 2, ...)
755      * @param x the value
756      * @throws SQLException if this object is closed
757      */

758     public void setClob(int parameterIndex, Reader JavaDoc x) throws SQLException JavaDoc {
759         try {
760             if(debug()) {
761                 debugCode("setClob("+parameterIndex+", x);");
762             }
763             checkClosed();
764             Value v;
765             if(x == null) {
766                 v = ValueNull.INSTANCE;
767             } else {
768                 v = conn.createClob(x, -1);
769             }
770             setParameter(parameterIndex, v);
771         } catch(Throwable JavaDoc e) {
772             throw logAndConvert(e);
773         }
774     }
775
776     /**
777      * Sets the value of a parameter as a Array.
778      *
779      * @throws SQLException Unsupported Feature (SQL State 0A000)
780      */

781     public void setArray(int parameterIndex, Array JavaDoc x) throws SQLException JavaDoc {
782         try {
783             if(debug()) {
784                 debugCode("setArray("+parameterIndex+", x);");
785             }
786             throw Message.getUnsupportedException();
787         } catch(Throwable JavaDoc e) {
788             throw logAndConvert(e);
789         }
790     }
791
792     /**
793      * Sets the value of a parameter as a byte array.
794      *
795      * @param parameterIndex the parameter index (1, 2, ...)
796      * @param x the value
797      * @throws SQLException if this object is closed
798      */

799     public void setBytes(int parameterIndex, byte[] x) throws SQLException JavaDoc {
800         try {
801             if(debug()) {
802                 debugCode("setBytes("+parameterIndex+", "+quoteBytes(x)+");");
803             }
804             // TODO clone the byte array (each array! maybe other objects) by default (maybe use a setting?)
805
Value v = x == null ? (Value) ValueNull.INSTANCE : ValueBytes.get(x);
806             setParameter(parameterIndex, v);
807         } catch(Throwable JavaDoc e) {
808             throw logAndConvert(e);
809         }
810     }
811     
812     /**
813      * Sets the value of a parameter as an input stream.
814      *
815      * @param parameterIndex the parameter index (1, 2, ...)
816      * @param x the value
817      * @param length the number of bytes
818      * @throws SQLException if this object is closed
819      */

820     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, long length) throws SQLException JavaDoc {
821         try {
822             if(debug()) {
823                 debugCode("setBinaryStream("+parameterIndex+", x, "+length+");");
824             }
825             checkClosed();
826             Value v = conn.createBlob(x, length);
827             setParameter(parameterIndex, v);
828         } catch(Throwable JavaDoc e) {
829             throw logAndConvert(e);
830         }
831     }
832
833     /**
834      * Sets the value of a parameter as an input stream.
835      *
836      * @param parameterIndex the parameter index (1, 2, ...)
837      * @param x the value
838      * @param length the number of bytes
839      * @throws SQLException if this object is closed
840      */

841     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
842         setBinaryStream(parameterIndex, x, (long) length);
843     }
844     
845     /**
846      * Sets the value of a parameter as an input stream.
847      *
848      * @param parameterIndex the parameter index (1, 2, ...)
849      * @param x the value
850      * @param length the number of bytes
851      * @throws SQLException if this object is closed
852      */

853     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x) throws SQLException JavaDoc {
854         setBinaryStream(parameterIndex, x, -1);
855     }
856
857     /**
858      * Sets the value of a parameter as an ASCII stream.
859      *
860      * @param parameterIndex the parameter index (1, 2, ...)
861      * @param x the value
862      * @param length the number of bytes
863      * @throws SQLException if this object is closed
864      */

865     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException JavaDoc {
866         setAsciiStream(parameterIndex, x, (long) length);
867     }
868     
869     /**
870      * Sets the value of a parameter as an ASCII stream.
871      *
872      * @param parameterIndex the parameter index (1, 2, ...)
873      * @param x the value
874      * @param length the number of bytes
875      * @throws SQLException if this object is closed
876      */

877     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, long length) throws SQLException JavaDoc {
878         try {
879             if(debug()) {
880                 debugCode("setAsciiStream("+parameterIndex+", x, "+length+");");
881             }
882             checkClosed();
883             Value v = conn.createClob(IOUtils.getAsciiReader(x), length);
884             setParameter(parameterIndex, v);
885         } catch(Throwable JavaDoc e) {
886             throw logAndConvert(e);
887         }
888     }
889     
890     /**
891      * Sets the value of a parameter as an ASCII stream.
892      *
893      * @param parameterIndex the parameter index (1, 2, ...)
894      * @param x the value
895      * @throws SQLException if this object is closed
896      */

897     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x) throws SQLException JavaDoc {
898         setAsciiStream(parameterIndex, x, -1);
899     }
900
901     /**
902      * Sets the value of a parameter as a character stream.
903      *
904      * @param parameterIndex the parameter index (1, 2, ...)
905      * @param x the value
906      * @param length the number of bytes
907      * @throws SQLException if this object is closed
908      */

909     public void setCharacterStream(int parameterIndex, Reader JavaDoc x, int length) throws SQLException JavaDoc {
910         setCharacterStream(parameterIndex, x, (long) length);
911     }
912     
913     /**
914      * Sets the value of a parameter as a character stream.
915      *
916      * @param parameterIndex the parameter index (1, 2, ...)
917      * @param x the value
918      * @throws SQLException if this object is closed
919      */

920     public void setCharacterStream(int parameterIndex, Reader JavaDoc x) throws SQLException JavaDoc {
921         setCharacterStream(parameterIndex, x, -1);
922     }
923     
924     /**
925      * Sets the value of a parameter as a character stream.
926      *
927      * @param parameterIndex the parameter index (1, 2, ...)
928      * @param x the value
929      * @param length the number of bytes
930      * @throws SQLException if this object is closed
931      */

932     public void setCharacterStream(int parameterIndex, Reader JavaDoc x, long length) throws SQLException JavaDoc {
933         try {
934             if(debug()) {
935                 debugCode("setCharacterStream("+parameterIndex+", x, "+length+");");
936             }
937             checkClosed();
938             Value v = conn.createClob(x, length);
939             setParameter(parameterIndex, v);
940         } catch(Throwable JavaDoc e) {
941             throw logAndConvert(e);
942         }
943     }
944
945     /**
946      * THIS FEATURE IS NOT SUPPORTED.
947      *
948      * @throws SQLException Unsupported Feature (SQL State 0A000)
949      */

950     public void setURL(int parameterIndex, URL JavaDoc x) throws SQLException JavaDoc {
951         try {
952             if(debug()) {
953                 debugCode("setURL("+parameterIndex+", x);");
954             }
955             throw Message.getUnsupportedException();
956         } catch(Throwable JavaDoc e) {
957             throw logAndConvert(e);
958         }
959     }
960
961     /**
962      * Gets the result set metadata of the query returned when the statement is executed.
963      *
964      * @return null as the method is not supported
965      * @throws SQLException if this object is closed
966      */

967     public ResultSetMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
968         try {
969             debugCodeCall("getMetaData");
970             checkClosed();
971             return null;
972         } catch(Throwable JavaDoc e) {
973             throw logAndConvert(e);
974         }
975     }
976
977
978     /**
979      * Clears the batch.
980      */

981     public void clearBatch() throws SQLException JavaDoc {
982         try {
983             debugCodeCall("clearBatch");
984             checkClosed();
985             batchParameters = null;
986         } catch(Throwable JavaDoc e) {
987             throw logAndConvert(e);
988         }
989     }
990
991     /**
992      * Closes this statement.
993      * All result sets that where created by this statement
994      * become invalid after calling this method.
995      */

996     public void close() throws SQLException JavaDoc {
997         try {
998             super.close();
999             if(command != null) {
1000                command.close();
1001                command = null;
1002            }
1003        } catch(Throwable JavaDoc e) {
1004            throw logAndConvert(e);
1005        }
1006    }
1007
1008    /**
1009     * Executes the batch.
1010     *
1011     * @return the array of updatecounts
1012     */

1013    public int[] executeBatch() throws SQLException JavaDoc {
1014        try {
1015            debugCodeCall("executeBatch");
1016            checkClosed();
1017            if (batchParameters == null) {
1018                // TODO batch: check what other database do if no parameters are set
1019
batchParameters = new ObjectArray();
1020            }
1021            int[] result = new int[batchParameters.size()];
1022            boolean error = false;
1023            SQLException JavaDoc next = null;
1024            for (int i = 0; i < batchParameters.size(); i++) {
1025                ObjectArray parameters = command.getParameters();
1026                Value[] set = (Value[]) batchParameters.get(i);
1027                for (int j = 0; j < set.length; j++) {
1028                    Value value = set[j];
1029                    ParameterInterface param = (ParameterInterface) parameters.get(j);
1030                    param.setValue(value);
1031                }
1032                try {
1033                    result[i] = executeUpdateInternal();
1034                } catch (SQLException JavaDoc e) {
1035                    if(next == null) {
1036                        next = e;
1037                    } else {
1038                        e.setNextException(next);
1039                        next = e;
1040                    }
1041                    logAndConvert(e);
1042//#ifdef JDK14
1043
result[i] = Statement.EXECUTE_FAILED;
1044//#endif
1045
error = true;
1046                }
1047            }
1048            batchParameters = null;
1049            if (error) {
1050                JdbcBatchUpdateException bue = new JdbcBatchUpdateException(next, result);
1051                bue.setNextException(next);
1052                throw bue;
1053            }
1054            return result;
1055        } catch(Throwable JavaDoc e) {
1056            throw logAndConvert(e);
1057        }
1058    }
1059
1060    /**
1061     * Adds the current settings to the batch.
1062     */

1063    public void addBatch() throws SQLException JavaDoc {
1064        try {
1065            debugCodeCall("addBatch");
1066            checkClosed();
1067            ObjectArray parameters = command.getParameters();
1068            Value[] set = new Value[parameters.size()];
1069            for (int i = 0; i < parameters.size(); i++) {
1070                ParameterInterface param = (ParameterInterface) parameters.get(i);
1071                Value value = param.getParamValue();
1072                set[i] = value;
1073            }
1074            if (batchParameters == null) {
1075                batchParameters = new ObjectArray();
1076            }
1077            batchParameters.add(set);
1078        } catch(Throwable JavaDoc e) {
1079            throw logAndConvert(e);
1080        }
1081    }
1082
1083    /**
1084     * Calling this method is not legal on a PreparedStatement.
1085     *
1086     * @throws SQLException Unsupported Feature (SQL State 0A000)
1087     */

1088    public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
1089        try {
1090            if(debug()) {
1091                debugCode("executeUpdate("+quote(sql)+", "+autoGeneratedKeys+");");
1092            }
1093            throw Message.getUnsupportedException();
1094        } catch(Throwable JavaDoc e) {
1095            throw logAndConvert(e);
1096        }
1097    }
1098
1099
1100    /**
1101     * Calling this method is not legal on a PreparedStatement.
1102     *
1103     * @throws SQLException Unsupported Feature (SQL State 0A000)
1104     */

1105    public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
1106        try {
1107            if (debug()) {
1108                debugCode("executeUpdate(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ");");
1109            }
1110            throw Message.getUnsupportedException();
1111        } catch (Exception JavaDoc e) {
1112            throw logAndConvert(e);
1113        }
1114    }
1115
1116    /**
1117     * Calling this method is not legal on a PreparedStatement.
1118     *
1119     * @throws SQLException Unsupported Feature (SQL State 0A000)
1120     */

1121    public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
1122        try {
1123            if (debug()) {
1124                debugCode("executeUpdate(" + quote(sql) + ", " + quoteArray(columnNames) + ");");
1125            }
1126            throw Message.getUnsupportedException();
1127        } catch (Exception JavaDoc e) {
1128            throw logAndConvert(e);
1129        }
1130    }
1131
1132    /**
1133     * Calling this method is not legal on a PreparedStatement.
1134     *
1135     * @throws SQLException Unsupported Feature (SQL State 0A000)
1136     */

1137    public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
1138        try {
1139            if (debug()) {
1140                debugCode("execute(" + quote(sql) + ", " + autoGeneratedKeys + ");");
1141            }
1142            throw Message.getUnsupportedException();
1143        } catch (Exception JavaDoc e) {
1144            throw logAndConvert(e);
1145        }
1146    }
1147
1148    /**
1149     * Calling this method is not legal on a PreparedStatement.
1150     *
1151     * @throws SQLException Unsupported Feature (SQL State 0A000)
1152     */

1153    public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
1154        try {
1155            if (debug()) {
1156                debugCode("execute(" + quote(sql) + ", " + quoteIntArray(columnIndexes) + ");");
1157            }
1158            throw Message.getUnsupportedException();
1159        } catch (Exception JavaDoc e) {
1160            throw logAndConvert(e);
1161        }
1162    }
1163
1164    /**
1165     * Calling this method is not legal on a PreparedStatement.
1166     *
1167     * @throws SQLException Unsupported Feature (SQL State 0A000)
1168     */

1169    public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
1170        try {
1171            if (debug()) {
1172                debugCode("execute(" + quote(sql) + ", " + quoteArray(columnNames) + ");");
1173            }
1174            throw Message.getUnsupportedException();
1175        } catch (Exception JavaDoc e) {
1176            throw logAndConvert(e);
1177        }
1178    }
1179
1180    /**
1181     * Get the parameter meta data of this prepared statement.
1182     *
1183     * @return the meta data
1184     */

1185//#ifdef JDK14
1186
public ParameterMetaData JavaDoc getParameterMetaData() throws SQLException JavaDoc {
1187        try {
1188            int id = getNextId(TraceObject.PARAMETER_META_DATA);
1189            if(debug()) {
1190                debugCodeAssign("ParameterMetaData", TraceObject.PARAMETER_META_DATA, id);
1191                debugCodeCall("getParameterMetaData");
1192            }
1193            checkClosed();
1194            JdbcParameterMetaData meta = new JdbcParameterMetaData(session, this, command, id);
1195            return meta;
1196        } catch(Throwable JavaDoc e) {
1197            throw logAndConvert(e);
1198        }
1199    }
1200//#endif
1201

1202    // =============================================================
1203

1204    JdbcPreparedStatement(SessionInterface session, JdbcConnection conn, String JavaDoc sql, int resultSetType, int id, boolean closeWithResultSet) throws SQLException JavaDoc {
1205        super(session, conn, resultSetType, id, closeWithResultSet);
1206        setTrace(session.getTrace(), TraceObject.PREPARED_STATEMENT, id);
1207        command = conn.prepareCommand(sql);
1208    }
1209
1210    private void setParameter(int parameterIndex, Value value) throws SQLException JavaDoc {
1211        checkClosed();
1212        parameterIndex--;
1213        ObjectArray parameters = command.getParameters();
1214        if (parameterIndex < 0 || parameterIndex >= parameters.size()) {
1215            throw Message.getInvalidValueException("" + (parameterIndex + 1), "parameterIndex");
1216        }
1217        ParameterInterface param = (ParameterInterface) parameters.get(parameterIndex);
1218        param.setValue(value);
1219    }
1220
1221    /**
1222     * Sets the value of a parameter as a row id.
1223     * @throws SQLException Unsupported Feature (SQL State 0A000)
1224     */

1225    //#ifdef JDK16
1226
/*
1227    public void setRowId(int parameterIndex, RowId x) throws SQLException {
1228        throw Message.getUnsupportedException();
1229    }
1230*/

1231    //#endif
1232

1233    /**
1234     * Sets the value of a parameter.
1235     *
1236     * @param parameterIndex the parameter index (1, 2, ...)
1237     * @param x the value
1238     * @throws SQLException if this object is closed
1239     */

1240    public void setNString(int parameterIndex, String JavaDoc x) throws SQLException JavaDoc {
1241        try {
1242            if(debug()) {
1243                debugCode("setNString("+parameterIndex+", "+quote(x)+");");
1244            }
1245            Value v = x == null ? (Value) ValueNull.INSTANCE : ValueString.get(x);
1246            setParameter(parameterIndex, v);
1247        } catch(Throwable JavaDoc e) {
1248            throw logAndConvert(e);
1249        }
1250    }
1251
1252    /**
1253     * Sets the value of a parameter as a character stream.
1254     *
1255     * @param parameterIndex the parameter index (1, 2, ...)
1256     * @param x the value
1257     * @param length the number of bytes
1258     * @throws SQLException if this object is closed
1259     */

1260    public void setNCharacterStream(int parameterIndex, Reader JavaDoc x, long length) throws SQLException JavaDoc {
1261        try {
1262            if(debug()) {
1263                debugCode("setNCharacterStream("+parameterIndex+", x, "+length+");");
1264            }
1265            checkClosed();
1266            Value v = conn.createClob(x, length);
1267            setParameter(parameterIndex, v);
1268        } catch(Throwable JavaDoc e) {
1269            throw logAndConvert(e);
1270        }
1271    }
1272
1273    /**
1274     * Sets the value of a parameter as a character stream.
1275     *
1276     * @param parameterIndex the parameter index (1, 2, ...)
1277     * @param x the value
1278     * @throws SQLException if this object is closed
1279     */

1280    public void setNCharacterStream(int parameterIndex, Reader JavaDoc x) throws SQLException JavaDoc {
1281        setNCharacterStream(parameterIndex, x, -1);
1282    }
1283
1284    /**
1285     * Sets the value of a parameter as a Clob.
1286     *
1287     * @param parameterIndex the parameter index (1, 2, ...)
1288     * @param x the value
1289     * @throws SQLException if this object is closed
1290     */

1291    //#ifdef JDK16
1292
/*
1293    public void setNClob(int parameterIndex, NClob x) throws SQLException {
1294        try {
1295            if(debug()) {
1296                debugCode("setNClob("+parameterIndex+", x);");
1297            }
1298            checkClosed();
1299            Value v;
1300            if(x == null) {
1301                v = ValueNull.INSTANCE;
1302            } else {
1303                v = conn.createClob(x.getCharacterStream(), -1);
1304            }
1305            setParameter(parameterIndex, v);
1306        } catch(Throwable e) {
1307            throw logAndConvert(e);
1308        }
1309    }
1310*/

1311    //#endif
1312

1313    /**
1314     * Sets the value of a parameter as a Clob.
1315     *
1316     * @param parameterIndex the parameter index (1, 2, ...)
1317     * @param x the value
1318     * @throws SQLException if this object is closed
1319     */

1320    public void setNClob(int parameterIndex, Reader JavaDoc x) throws SQLException JavaDoc {
1321        try {
1322            if(debug()) {
1323                debugCode("setNClob("+parameterIndex+", x);");
1324            }
1325            checkClosed();
1326            Value v = conn.createClob(x, -1);
1327            setParameter(parameterIndex, v);
1328        } catch(Throwable JavaDoc e) {
1329            throw logAndConvert(e);
1330        }
1331    }
1332
1333    /**
1334     * Sets the value of a parameter as a Clob.
1335     *
1336     * @param parameterIndex the parameter index (1, 2, ...)
1337     * @param x the value
1338     * @throws SQLException if this object is closed
1339     */

1340    public void setClob(int parameterIndex, Reader JavaDoc x, long length) throws SQLException JavaDoc {
1341        try {
1342            if(debug()) {
1343                debugCode("setClob("+parameterIndex+", x, "+length+");");
1344            }
1345            checkClosed();
1346            Value v = conn.createClob(x, length);
1347            setParameter(parameterIndex, v);
1348        } catch(Throwable JavaDoc e) {
1349            throw logAndConvert(e);
1350        }
1351    }
1352
1353    /**
1354     * Sets the value of a parameter as a Blob.
1355     *
1356     * @param parameterIndex the parameter index (1, 2, ...)
1357     * @param x the value
1358     * @throws SQLException if this object is closed
1359     */

1360    public void setBlob(int parameterIndex, InputStream JavaDoc x, long length) throws SQLException JavaDoc {
1361        try {
1362            if(debug()) {
1363                debugCode("setBlob("+parameterIndex+", x, "+length+");");
1364            }
1365            checkClosed();
1366            Value v = conn.createBlob(x, length);
1367            setParameter(parameterIndex, v);
1368        } catch(Throwable JavaDoc e) {
1369            throw logAndConvert(e);
1370        }
1371    }
1372
1373    /**
1374     * Sets the value of a parameter as a Clob.
1375     *
1376     * @param parameterIndex the parameter index (1, 2, ...)
1377     * @param x the value
1378     * @throws SQLException if this object is closed
1379     */

1380    public void setNClob(int parameterIndex, Reader JavaDoc x, long length) throws SQLException JavaDoc {
1381        try {
1382            if(debug()) {
1383                debugCode("setNClob("+parameterIndex+", x, "+length+");");
1384            }
1385            checkClosed();
1386            Value v = conn.createClob(x, length);
1387            setParameter(parameterIndex, v);
1388        } catch(Throwable JavaDoc e) {
1389            throw logAndConvert(e);
1390        }
1391    }
1392
1393    /**
1394     * Sets the value of a parameter as a SQLXML object.
1395     * @throws SQLException Unsupported Feature (SQL State 0A000)
1396     */

1397    //#ifdef JDK16
1398
/*
1399    public void setSQLXML(int parameterIndex, SQLXML x) throws SQLException {
1400        throw Message.getUnsupportedException();
1401    }
1402*/

1403    //#endif
1404

1405}
1406
Popular Tags