File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcCallableStatement.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbc;
8
9 import java.io.InputStream;
10 import java.io.Reader;
11 import java.math.BigDecimal;
12 import java.net.URL;
13 import java.sql.Array;
14 import java.sql.Blob;
15 import java.sql.CallableStatement;
16 import java.sql.Clob;
17 import java.sql.Date;
18 import java.sql.Ref;
19 import java.sql.ResultSetMetaData;
20 //## Java 1.6 ##
21 import java.sql.NClob;
22 import java.sql.SQLXML;
23 import java.sql.RowId;
24 //*/
25 import java.sql.SQLException;
26 import java.sql.Time;
27 import java.sql.Timestamp;
28 import java.util.Calendar;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.h2.constant.ErrorCode;
32 import org.h2.expression.ParameterInterface;
33 import org.h2.message.DbException;
34 import org.h2.message.TraceObject;
35 import org.h2.util.BitField;
36 import org.h2.util.New;
37 import org.h2.value.ValueNull;
38
39 /**
40 * Represents a callable statement.
41 *
42 * @author Sergi Vladykin
43 * @author Thomas Mueller
44 */
45 public class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement {
46
47 private BitField outParameters;
48 private int maxOutParameters;
49 private HashMap<String, Integer> namedParameters;
50
51 JdbcCallableStatement(JdbcConnection conn, String sql, int id, int resultSetType, int resultSetConcurrency) {
52 super(conn, sql, id, resultSetType, resultSetConcurrency, false);
53 setTrace(session.getTrace(), TraceObject.CALLABLE_STATEMENT, id);
54 }
55
56 /**
57 * Executes a statement (insert, update, delete, create, drop)
58 * and returns the update count.
59 * If another result set exists for this statement, this will be closed
60 * (even if this statement fails).
61 *
62 * If auto commit is on, this statement will be committed.
63 * If the statement is a DDL statement (create, drop, alter) and does not
64 * throw an exception, the current transaction (if any) is committed after
65 * executing the statement.
66 *
67 * @return the update count (number of row affected by an insert, update or
68 * delete, or 0 if no rows or the statement was a create, drop,
69 * commit or rollback)
70 * @throws SQLException if this object is closed or invalid
71 */
72 public int executeUpdate() throws SQLException {
73 try {
74 checkClosed();
75 if (command.isQuery()) {
76 super.executeQuery();
77 return 0;
78 }
79 return super.executeUpdate();
80 } catch (Exception e) {
81 throw logAndConvert(e);
82 }
83 }
84
85 /**
86 * Registers the given OUT parameter.
87 *
88 * @param parameterIndex the parameter index (1, 2, ...)
89 * @param sqlType the data type (Types.x) - ignored
90 */
91 public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
92 registerOutParameter(parameterIndex);
93 }
94
95 /**
96 * Registers the given OUT parameter.
97 *
98 * @param parameterIndex the parameter index (1, 2, ...)
99 * @param sqlType the data type (Types.x) - ignored
100 * @param typeName the SQL type name - ignored
101 */
102 public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException {
103 registerOutParameter(parameterIndex);
104 }
105
106 /**
107 * Registers the given OUT parameter.
108 *
109 * @param parameterIndex the parameter index (1, 2, ...)
110 * @param sqlType the data type (Types.x)
111 * @param scale is ignored
112 */
113 public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException {
114 registerOutParameter(parameterIndex);
115 }
116
117 /**
118 * Registers the given OUT parameter.
119 *
120 * @param parameterName the parameter name
121 * @param sqlType the data type (Types.x) - ignored
122 * @param typeName the SQL type name - ignored
123 */
124 public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException {
125 registerOutParameter(getIndexForName(parameterName), sqlType, typeName);
126 }
127
128 /**
129 * Registers the given OUT parameter.
130 *
131 * @param parameterName the parameter name
132 * @param sqlType the data type (Types.x) - ignored
133 * @param scale is ignored
134 */
135 public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException {
136 registerOutParameter(getIndexForName(parameterName), sqlType, scale);
137 }
138
139 /**
140 * Registers the given OUT parameter.
141 *
142 * @param parameterName the parameter name
143 * @param sqlType the data type (Types.x) - ignored
144 */
145 public void registerOutParameter(String parameterName, int sqlType) throws SQLException {
146 registerOutParameter(getIndexForName(parameterName), sqlType);
147 }
148
149 /**
150 * Returns whether the last column accessed was null.
151 *
152 * @return true if the last column accessed was null
153 */
154 public boolean wasNull() throws SQLException {
155 return getOpenResultSet().wasNull();
156 }
157
158 /**
159 * [Not supported]
160 */
161 public URL getURL(int parameterIndex) throws SQLException {
162 throw unsupported("url");
163 }
164
165 /**
166 * Returns the value of the specified column as a String.
167 *
168 * @param parameterIndex (1,2,...)
169 * @return the value
170 * @throws SQLException if the column is not found or if the result set is closed
171 */
172 public String getString(int parameterIndex) throws SQLException {
173 checkRegistered(parameterIndex);
174 return getOpenResultSet().getString(parameterIndex);
175 }
176
177 /**
178 * Returns the value of the specified column as a boolean.
179 *
180 * @param parameterIndex (1,2,...)
181 * @return the value
182 * @throws SQLException if the column is not found or if the result set is
183 * closed
184 */
185 public boolean getBoolean(int parameterIndex) throws SQLException {
186 checkRegistered(parameterIndex);
187 return getOpenResultSet().getBoolean(parameterIndex);
188 }
189
190 /**
191 * Returns the value of the specified column as a byte.
192 *
193 * @param parameterIndex (1,2,...)
194 * @return the value
195 * @throws SQLException if the column is not found or if the result set is
196 * closed
197 */
198 public byte getByte(int parameterIndex) throws SQLException {
199 checkRegistered(parameterIndex);
200 return getOpenResultSet().getByte(parameterIndex);
201 }
202
203 /**
204 * Returns the value of the specified column as a short.
205 *
206 * @param parameterIndex (1,2,...)
207 * @return the value
208 * @throws SQLException if the column is not found or if the result set is
209 * closed
210 */
211 public short getShort(int parameterIndex) throws SQLException {
212 checkRegistered(parameterIndex);
213 return getOpenResultSet().getShort(parameterIndex);
214 }
215
216 /**
217 * Returns the value of the specified column as an int.
218 *
219 * @param parameterIndex the parameter index (1, 2, ...)
220 * @return the value
221 * @throws SQLException if the column is not found or if the result set is closed
222 */
223 public int getInt(int parameterIndex) throws SQLException {
224 checkRegistered(parameterIndex);
225 return getOpenResultSet().getInt(parameterIndex);
226 }
227
228 /**
229 * Returns the value of the specified column as a long.
230 *
231 * @param parameterIndex the parameter index (1, 2, ...)
232 * @return the value
233 * @throws SQLException if the column is not found or if the result set is
234 * closed
235 */
236 public long getLong(int parameterIndex) throws SQLException {
237 checkRegistered(parameterIndex);
238 return getOpenResultSet().getLong(parameterIndex);
239 }
240
241 /**
242 * Returns the value of the specified column as a float.
243 *
244 * @param parameterIndex the parameter index (1, 2, ...)
245 * @return the value
246 * @throws SQLException if the column is not found or if the result set is
247 * closed
248 */
249 public float getFloat(int parameterIndex) throws SQLException {
250 checkRegistered(parameterIndex);
251 return getOpenResultSet().getFloat(parameterIndex);
252 }
253
254 /**
255 * Returns the value of the specified column as a double.
256 *
257 * @param parameterIndex the parameter index (1, 2, ...)
258 * @return the value
259 * @throws SQLException if the column is not found or if the result set is
260 * closed
261 */
262 public double getDouble(int parameterIndex) throws SQLException {
263 checkRegistered(parameterIndex);
264 return getOpenResultSet().getDouble(parameterIndex);
265 }
266
267 /**
268 * Returns the value of the specified column as a BigDecimal.
269 *
270 * @deprecated use {@link #getBigDecimal(int)}
271 *
272 * @param parameterIndex the parameter index (1, 2, ...)
273 * @param scale is ignored
274 * @return the value
275 * @throws SQLException if the column is not found or if the result set is
276 * closed
277 */
278 public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException {
279 checkRegistered(parameterIndex);
280 return getOpenResultSet().getBigDecimal(parameterIndex, scale);
281 }
282
283 /**
284 * Returns the value of the specified column as a byte array.
285 *
286 * @param parameterIndex the parameter index (1, 2, ...)
287 * @return the value
288 * @throws SQLException if the column is not found or if the result set is
289 * closed
290 */
291 public byte[] getBytes(int parameterIndex) throws SQLException {
292 checkRegistered(parameterIndex);
293 return getOpenResultSet().getBytes(parameterIndex);
294 }
295
296 /**
297 * Returns the value of the specified column as a java.sql.Date.
298 *
299 * @param parameterIndex the parameter index (1, 2, ...)
300 * @return the value
301 * @throws SQLException if the column is not found or if the result set is closed
302 */
303 public Date getDate(int parameterIndex) throws SQLException {
304 checkRegistered(parameterIndex);
305 return getOpenResultSet().getDate(parameterIndex);
306 }
307
308 /**
309 * Returns the value of the specified column as a java.sql.Time.
310 *
311 * @param parameterIndex the parameter index (1, 2, ...)
312 * @return the value
313 * @throws SQLException if the column is not found or if the result set is closed
314 */
315 public Time getTime(int parameterIndex) throws SQLException {
316 checkRegistered(parameterIndex);
317 return getOpenResultSet().getTime(parameterIndex);
318 }
319
320 /**
321 * Returns the value of the specified column as a java.sql.Timestamp.
322 *
323 * @param parameterIndex the parameter index (1, 2, ...)
324 * @return the value
325 * @throws SQLException if the column is not found or if the result set is closed
326 */
327 public Timestamp getTimestamp(int parameterIndex) throws SQLException {
328 checkRegistered(parameterIndex);
329 return getOpenResultSet().getTimestamp(parameterIndex);
330 }
331
332 /**
333 * Returns a column value as a Java object. The data is
334 * de-serialized into a Java object (on the client side).
335 *
336 * @param parameterIndex the parameter index (1, 2, ...)
337 * @return the value or null
338 * @throws SQLException if the column is not found or if the result set is
339 * closed
340 */
341 public Object getObject(int parameterIndex) throws SQLException {
342 checkRegistered(parameterIndex);
343 return getOpenResultSet().getObject(parameterIndex);
344 }
345
346 /**
347 * Returns the value of the specified column as a BigDecimal.
348 *
349 * @param parameterIndex the parameter index (1, 2, ...)
350 * @return the value
351 * @throws SQLException if the column is not found or if the result set is closed
352 */
353 public BigDecimal getBigDecimal(int parameterIndex) throws SQLException {
354 checkRegistered(parameterIndex);
355 return getOpenResultSet().getBigDecimal(parameterIndex);
356 }
357
358 /**
359 * [Not supported] Gets a column as a object using the specified type
360 * mapping.
361 */
362 public Object getObject(int parameterIndex, Map<String, Class<?>> map) throws SQLException {
363 throw unsupported("map");
364 }
365
366 /**
367 * [Not supported] Gets a column as a reference.
368 */
369 public Ref getRef(int parameterIndex) throws SQLException {
370 throw unsupported("ref");
371 }
372
373 /**
374 * Returns the value of the specified column as a Blob.
375 *
376 * @param parameterIndex the parameter index (1, 2, ...)
377 * @return the value
378 * @throws SQLException if the column is not found or if the result set is
379 * closed
380 */
381 public Blob getBlob(int parameterIndex) throws SQLException {
382 checkRegistered(parameterIndex);
383 return getOpenResultSet().getBlob(parameterIndex);
384 }
385
386 /**
387 * Returns the value of the specified column as a Clob.
388 *
389 * @param parameterIndex the parameter index (1, 2, ...)
390 * @return the value
391 * @throws SQLException if the column is not found or if the result set is
392 * closed
393 */
394 public Clob getClob(int parameterIndex) throws SQLException {
395 checkRegistered(parameterIndex);
396 return getOpenResultSet().getClob(parameterIndex);
397 }
398
399 /**
400 * Returns the value of the specified column as an Array.
401 *
402 * @param parameterIndex the parameter index (1, 2, ...)
403 * @return the value
404 * @throws SQLException if the column is not found or if the result set is
405 * closed
406 */
407 public Array getArray(int parameterIndex) throws SQLException {
408 checkRegistered(parameterIndex);
409 return getOpenResultSet().getArray(parameterIndex);
410 }
411
412 /**
413 * Returns the value of the specified column as a java.sql.Date using a
414 * specified time zone.
415 *
416 * @param parameterIndex the parameter index (1, 2, ...)
417 * @param cal the calendar
418 * @return the value
419 * @throws SQLException if the column is not found or if the result set is
420 * closed
421 */
422 public Date getDate(int parameterIndex, Calendar cal) throws SQLException {
423 checkRegistered(parameterIndex);
424 return getOpenResultSet().getDate(parameterIndex, cal);
425 }
426
427 /**
428 * Returns the value of the specified column as a java.sql.Time using a
429 * specified time zone.
430 *
431 * @param parameterIndex the parameter index (1, 2, ...)
432 * @param cal the calendar
433 * @return the value
434 * @throws SQLException if the column is not found or if the result set is
435 * closed
436 */
437 public Time getTime(int parameterIndex, Calendar cal) throws SQLException {
438 checkRegistered(parameterIndex);
439 return getOpenResultSet().getTime(parameterIndex, cal);
440 }
441
442 /**
443 * Returns the value of the specified column as a java.sql.Timestamp using a
444 * specified time zone.
445 *
446 * @param parameterIndex the parameter index (1, 2, ...)
447 * @param cal the calendar
448 * @return the value
449 * @throws SQLException if the column is not found or if the result set is
450 * closed
451 */
452 public Timestamp getTimestamp(int parameterIndex, Calendar cal) throws SQLException {
453 checkRegistered(parameterIndex);
454 return getOpenResultSet().getTimestamp(parameterIndex, cal);
455 }
456
457 /**
458 * [Not supported]
459 */
460 public URL getURL(String parameterName) throws SQLException {
461 throw unsupported("url");
462 }
463
464 /**
465 * Returns the value of the specified column as a java.sql.Timestamp using a
466 * specified time zone.
467 *
468 * @param parameterName the parameter name
469 * @param cal the calendar
470 * @return the value
471 * @throws SQLException if the column is not found or if the result set is
472 * closed
473 */
474 public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLException {
475 return getTimestamp(getIndexForName(parameterName), cal);
476 }
477
478 /**
479 * Returns the value of the specified column as a java.sql.Time using a
480 * specified time zone.
481 *
482 * @param parameterName the parameter name
483 * @param cal the calendar
484 * @return the value
485 * @throws SQLException if the column is not found or if the result set is
486 * closed
487 */
488 public Time getTime(String parameterName, Calendar cal) throws SQLException {
489 return getTime(getIndexForName(parameterName), cal);
490 }
491
492 /**
493 * Returns the value of the specified column as a java.sql.Date using a
494 * specified time zone.
495 *
496 * @param parameterName the parameter name
497 * @param cal the calendar
498 * @return the value
499 * @throws SQLException if the column is not found or if the result set is closed
500 */
501 public Date getDate(String parameterName, Calendar cal) throws SQLException {
502 return getDate(getIndexForName(parameterName), cal);
503 }
504
505 /**
506 * Returns the value of the specified column as an Array.
507 *
508 * @param parameterName the parameter name
509 * @return the value
510 * @throws SQLException if the column is not found or if the result set is
511 * closed
512 */
513 public Array getArray(String parameterName) throws SQLException {
514 return getArray(getIndexForName(parameterName));
515 }
516
517 /**
518 * Returns the value of the specified column as a Clob.
519 *
520 * @param parameterName the parameter name
521 * @return the value
522 * @throws SQLException if the column is not found or if the result set is
523 * closed
524 */
525 public Clob getClob(String parameterName) throws SQLException {
526 return getClob(getIndexForName(parameterName));
527 }
528
529 /**
530 * Returns the value of the specified column as a Blob.
531 *
532 * @param parameterName the parameter name
533 * @return the value
534 * @throws SQLException if the column is not found or if the result set is
535 * closed
536 */
537 public Blob getBlob(String parameterName) throws SQLException {
538 return getBlob(getIndexForName(parameterName));
539 }
540
541 /**
542 * [Not supported] Gets a column as a reference.
543 */
544 public Ref getRef(String parameterName) throws SQLException {
545 throw unsupported("ref");
546 }
547
548 /**
549 * [Not supported] Gets a column as a object using the specified type
550 * mapping.
551 */
552 public Object getObject(String parameterName, Map<String, Class<?>> map) throws SQLException {
553 throw unsupported("map");
554 }
555
556 /**
557 * Returns the value of the specified column as a BigDecimal.
558 *
559 * @param parameterName the parameter name
560 * @return the value
561 * @throws SQLException if the column is not found or if the result set is
562 * closed
563 */
564 public BigDecimal getBigDecimal(String parameterName) throws SQLException {
565 return getBigDecimal(getIndexForName(parameterName));
566 }
567
568 /**
569 * Returns a column value as a Java object. The data is
570 * de-serialized into a Java object (on the client side).
571 *
572 * @param parameterName the parameter name
573 * @return the value or null
574 * @throws SQLException if the column is not found or if the result set is
575 * closed
576 */
577 public Object getObject(String parameterName) throws SQLException {
578 return getObject(getIndexForName(parameterName));
579 }
580
581 /**
582 * Returns the value of the specified column as a java.sql.Timestamp.
583 *
584 * @param parameterName the parameter name
585 * @return the value
586 * @throws SQLException if the column is not found or if the result set is
587 * closed
588 */
589 public Timestamp getTimestamp(String parameterName) throws SQLException {
590 return getTimestamp(getIndexForName(parameterName));
591 }
592
593 /**
594 * Returns the value of the specified column as a java.sql.Time.
595 *
596 * @param parameterName the parameter name
597 * @return the value
598 * @throws SQLException if the column is not found or if the result set is
599 * closed
600 */
601 public Time getTime(String parameterName) throws SQLException {
602 return getTime(getIndexForName(parameterName));
603 }
604
605 /**
606 * Returns the value of the specified column as a java.sql.Date.
607 *
608 * @param parameterName the parameter name
609 * @return the value
610 * @throws SQLException if the column is not found or if the result set is closed
611 */
612 public Date getDate(String parameterName) throws SQLException {
613 return getDate(getIndexForName(parameterName));
614 }
615
616 /**
617 * Returns the value of the specified column as a byte array.
618 *
619 * @param parameterName the parameter name
620 * @return the value
621 * @throws SQLException if the column is not found or if the result set is
622 * closed
623 */
624 public byte[] getBytes(String parameterName) throws SQLException {
625 return getBytes(getIndexForName(parameterName));
626 }
627
628 /**
629 * Returns the value of the specified column as a double.
630 *
631 * @param parameterName the parameter name
632 * @return the value
633 * @throws SQLException if the column is not found or if the result set is
634 * closed
635 */
636 public double getDouble(String parameterName) throws SQLException {
637 return getDouble(getIndexForName(parameterName));
638 }
639
640 /**
641 * Returns the value of the specified column as a float.
642 *
643 * @param parameterName the parameter name
644 * @return the value
645 * @throws SQLException if the column is not found or if the result set is
646 * closed
647 */
648 public float getFloat(String parameterName) throws SQLException {
649 return getFloat(getIndexForName(parameterName));
650 }
651
652 /**
653 * Returns the value of the specified column as a long.
654 *
655 * @param parameterName the parameter name
656 * @return the value
657 * @throws SQLException if the column is not found or if the result set is
658 * closed
659 */
660 public long getLong(String parameterName) throws SQLException {
661 return getLong(getIndexForName(parameterName));
662 }
663
664 /**
665 * Returns the value of the specified column as an int.
666 *
667 * @param parameterName the parameter name
668 * @return the value
669 * @throws SQLException if the column is not found or if the result set is closed
670 */
671 public int getInt(String parameterName) throws SQLException {
672 return getInt(getIndexForName(parameterName));
673 }
674
675 /**
676 * Returns the value of the specified column as a short.
677 *
678 * @param parameterName the parameter name
679 * @return the value
680 * @throws SQLException if the column is not found or if the result set is
681 * closed
682 */
683 public short getShort(String parameterName) throws SQLException {
684 return getShort(getIndexForName(parameterName));
685 }
686
687 /**
688 * Returns the value of the specified column as a byte.
689 *
690 * @param parameterName the parameter name
691 * @return the value
692 * @throws SQLException if the column is not found or if the result set is
693 * closed
694 */
695 public byte getByte(String parameterName) throws SQLException {
696 return getByte(getIndexForName(parameterName));
697 }
698
699 /**
700 * Returns the value of the specified column as a boolean.
701 *
702 * @param parameterName the parameter name
703 * @return the value
704 * @throws SQLException if the column is not found or if the result set is
705 * closed
706 */
707 public boolean getBoolean(String parameterName) throws SQLException {
708 return getBoolean(getIndexForName(parameterName));
709 }
710
711 /**
712 * Returns the value of the specified column as a String.
713 *
714 * @param parameterName the parameter name
715 * @return the value
716 * @throws SQLException if the column is not found or if the result set is closed
717 */
718 public String getString(String parameterName) throws SQLException {
719 return getString(getIndexForName(parameterName));
720 }
721
722 /**
723 * [Not supported] Returns the value of the specified column as a row id.
724 *
725 * @param parameterIndex the parameter index (1, 2, ...)
726 */
727 //## Java 1.6 ##
728 public RowId getRowId(int parameterIndex) throws SQLException {
729 throw unsupported("rowId");
730 }
731 //*/
732
733 /**
734 * [Not supported] Returns the value of the specified column as a row id.
735 *
736 * @param parameterName the parameter name
737 */
738 //## Java 1.6 ##
739 public RowId getRowId(String parameterName) throws SQLException {
740 throw unsupported("rowId");
741 }
742 //*/
743
744 /**
745 * Returns the value of the specified column as a Clob.
746 *
747 * @param parameterIndex the parameter index (1, 2, ...)
748 * @return the value
749 * @throws SQLException if the column is not found or if the result set is closed
750 */
751 //## Java 1.6 ##
752 public NClob getNClob(int parameterIndex) throws SQLException {
753 checkRegistered(parameterIndex);
754 return getOpenResultSet().getNClob(parameterIndex);
755 }
756 //*/
757
758 /**
759 * Returns the value of the specified column as a Clob.
760 *
761 * @param parameterName the parameter name
762 * @return the value
763 * @throws SQLException if the column is not found or if the result set is closed
764 */
765 //## Java 1.6 ##
766 public NClob getNClob(String parameterName) throws SQLException {
767 return getNClob(getIndexForName(parameterName));
768 }
769 //*/
770
771 /**
772 * [Not supported] Returns the value of the specified column as a SQLXML object.
773 */
774 //## Java 1.6 ##
775 public SQLXML getSQLXML(int parameterIndex) throws SQLException {
776 throw unsupported("SQLXML");
777 }
778 //*/
779
780 /**
781 * [Not supported] Returns the value of the specified column as a SQLXML object.
782 */
783 //## Java 1.6 ##
784 public SQLXML getSQLXML(String parameterName) throws SQLException {
785 throw unsupported("SQLXML");
786 }
787 //*/
788
789 /**
790 * Returns the value of the specified column as a String.
791 *
792 * @param parameterIndex the parameter index (1, 2, ...)
793 * @return the value
794 * @throws SQLException if the column is not found or if the result set is
795 * closed
796 */
797 public String getNString(int parameterIndex) throws SQLException {
798 checkRegistered(parameterIndex);
799 return getOpenResultSet().getNString(parameterIndex);
800 }
801
802 /**
803 * Returns the value of the specified column as a String.
804 *
805 * @param parameterName the parameter name
806 * @return the value
807 * @throws SQLException if the column is not found or if the result set is
808 * closed
809 */
810 public String getNString(String parameterName) throws SQLException {
811 return getNString(getIndexForName(parameterName));
812 }
813
814 /**
815 * Returns the value of the specified column as a reader.
816 *
817 * @param parameterIndex the parameter index (1, 2, ...)
818 * @return the value
819 * @throws SQLException if the column is not found or if the result set is
820 * closed
821 */
822 public Reader getNCharacterStream(int parameterIndex)
823 throws SQLException {
824 checkRegistered(parameterIndex);
825 return getOpenResultSet().getNCharacterStream(parameterIndex);
826 }
827
828 /**
829 * Returns the value of the specified column as a reader.
830 *
831 * @param parameterName the parameter name
832 * @return the value
833 * @throws SQLException if the column is not found or if the result set is
834 * closed
835 */
836 public Reader getNCharacterStream(String parameterName)
837 throws SQLException {
838 return getNCharacterStream(getIndexForName(parameterName));
839 }
840
841 /**
842 * Returns the value of the specified column as a reader.
843 *
844 * @param parameterIndex the parameter index (1, 2, ...)
845 * @return the value
846 * @throws SQLException if the column is not found or if the result set is
847 * closed
848 */
849 public Reader getCharacterStream(int parameterIndex)
850 throws SQLException {
851 checkRegistered(parameterIndex);
852 return getOpenResultSet().getCharacterStream(parameterIndex);
853 }
854
855 /**
856 * Returns the value of the specified column as a reader.
857 *
858 * @param parameterName the parameter name
859 * @return the value
860 * @throws SQLException if the column is not found or if the result set is
861 * closed
862 */
863 public Reader getCharacterStream(String parameterName)
864 throws SQLException {
865 return getCharacterStream(getIndexForName(parameterName));
866 }
867
868 // =============================================================
869
870 /**
871 * Sets a parameter to null.
872 *
873 * @param parameterName the parameter name
874 * @param sqlType the data type (Types.x)
875 * @param typeName this parameter is ignored
876 * @throws SQLException if this object is closed
877 */
878 public void setNull(String parameterName, int sqlType, String typeName) throws SQLException {
879 setNull(getIndexForName(parameterName), sqlType, typeName);
880 }
881
882 /**
883 * Sets a parameter to null.
884 *
885 * @param parameterName the parameter name
886 * @param sqlType the data type (Types.x)
887 * @throws SQLException if this object is closed
888 */
889 public void setNull(String parameterName, int sqlType) throws SQLException {
890 setNull(getIndexForName(parameterName), sqlType);
891 }
892
893 /**
894 * Sets the timestamp using a specified time zone. The value will be
895 * converted to the local time zone.
896 *
897 * @param parameterName the parameter name
898 * @param x the value
899 * @param cal the calendar
900 * @throws SQLException if this object is closed
901 */
902 public void setTimestamp(String parameterName, Timestamp x, Calendar cal) throws SQLException {
903 setTimestamp(getIndexForName(parameterName), x, cal);
904 }
905
906 /**
907 * Sets the time using a specified time zone. The value will be converted to
908 * the local time zone.
909 *
910 * @param parameterName the parameter name
911 * @param x the value
912 * @param cal the calendar
913 * @throws SQLException if this object is closed
914 */
915 public void setTime(String parameterName, Time x, Calendar cal) throws SQLException {
916 setTime(getIndexForName(parameterName), x, cal);
917 }
918
919 /**
920 * Sets the date using a specified time zone. The value will be converted to
921 * the local time zone.
922 *
923 * @param parameterName the parameter name
924 * @param x the value
925 * @param cal the calendar
926 * @throws SQLException if this object is closed
927 */
928 public void setDate(String parameterName, Date x, Calendar cal) throws SQLException {
929 setDate(getIndexForName(parameterName), x, cal);
930 }
931
932 /**
933 * Sets the value of a parameter as a character stream.
934 * This method does not close the reader.
935 * The reader may be closed after executing the statement.
936 *
937 * @param parameterName the parameter name
938 * @param x the value
939 * @param length the maximum number of characters
940 * @throws SQLException if this object is closed
941 */
942 public void setCharacterStream(String parameterName, Reader x, int length) throws SQLException {
943 setCharacterStream(getIndexForName(parameterName), x, length);
944 }
945
946 /**
947 * Sets the value of a parameter.
948 * Objects of unknown classes are serialized (on the client side).
949 *
950 * @param parameterName the parameter name
951 * @param x the value
952 * @throws SQLException if this object is closed
953 */
954 public void setObject(String parameterName, Object x) throws SQLException {
955 setObject(getIndexForName(parameterName), x);
956 }
957
958 /**
959 * Sets the value of a parameter. The object is converted, if required, to
960 * the specified data type before sending to the database.
961 * Objects of unknown classes are serialized (on the client side).
962 *
963 * @param parameterName the parameter name
964 * @param x the value, null is allowed
965 * @param targetSqlType the type as defined in java.sql.Types
966 * @throws SQLException if this object is closed
967 */
968 public void setObject(String parameterName, Object x, int targetSqlType) throws SQLException {
969 setObject(getIndexForName(parameterName), x, targetSqlType);
970 }
971
972 /**
973 * Sets the value of a parameter. The object is converted, if required, to
974 * the specified data type before sending to the database.
975 * Objects of unknown classes are serialized (on the client side).
976 *
977 * @param parameterName the parameter name
978 * @param x the value, null is allowed
979 * @param targetSqlType the type as defined in java.sql.Types
980 * @param scale is ignored
981 * @throws SQLException if this object is closed
982 */
983 public void setObject(String parameterName, Object x, int targetSqlType, int scale) throws SQLException {
984 setObject(getIndexForName(parameterName), x, targetSqlType, scale);
985 }
986
987 /**
988 * Sets the value of a parameter as an input stream.
989 * This method does not close the stream.
990 * The stream may be closed after executing the statement.
991 *
992 * @param parameterName the parameter name
993 * @param x the value
994 * @param length the maximum number of bytes
995 * @throws SQLException if this object is closed
996 */
997 public void setBinaryStream(String parameterName, InputStream x, int length) throws SQLException {
998 setBinaryStream(getIndexForName(parameterName), x, length);
999 }
1000
1001 /**
1002 * Sets the value of a parameter as an ASCII stream.
1003 * This method does not close the stream.
1004 * The stream may be closed after executing the statement.
1005 *
1006 * @param parameterName the parameter name
1007 * @param x the value
1008 * @param length the maximum number of bytes
1009 * @throws SQLException if this object is closed
1010 */
1011 public void setAsciiStream(String parameterName,
1012 InputStream x, long length) throws SQLException {
1013 setAsciiStream(getIndexForName(parameterName), x, length);
1014 }
1015
1016 /**
1017 * Sets the value of a parameter.
1018 *
1019 * @param parameterName the parameter name
1020 * @param x the value
1021 * @throws SQLException if this object is closed
1022 */
1023 public void setTimestamp(String parameterName, Timestamp x) throws SQLException {
1024 setTimestamp(getIndexForName(parameterName), x);
1025 }
1026
1027 /**
1028 * Sets the time using a specified time zone.
1029 *
1030 * @param parameterName the parameter name
1031 * @param x the value
1032 * @throws SQLException if this object is closed
1033 */
1034 public void setTime(String parameterName, Time x) throws SQLException {
1035 setTime(getIndexForName(parameterName), x);
1036 }
1037
1038 /**
1039 * Sets the value of a parameter.
1040 *
1041 * @param parameterName the parameter name
1042 * @param x the value
1043 * @throws SQLException if this object is closed
1044 */
1045 public void setDate(String parameterName, Date x) throws SQLException {
1046 setDate(getIndexForName(parameterName), x);
1047 }
1048
1049 /**
1050 * Sets the value of a parameter as a byte array.
1051 *
1052 * @param parameterName the parameter name
1053 * @param x the value
1054 * @throws SQLException if this object is closed
1055 */
1056 public void setBytes(String parameterName, byte[] x) throws SQLException {
1057 setBytes(getIndexForName(parameterName), x);
1058 }
1059
1060 /**
1061 * Sets the value of a parameter.
1062 *
1063 * @param parameterName the parameter name
1064 * @param x the value
1065 * @throws SQLException if this object is closed
1066 */
1067 public void setString(String parameterName, String x) throws SQLException {
1068 setString(getIndexForName(parameterName), x);
1069 }
1070
1071 /**
1072 * Sets the value of a parameter.
1073 *
1074 * @param parameterName the parameter name
1075 * @param x the value
1076 * @throws SQLException if this object is closed
1077 */
1078 public void setBigDecimal(String parameterName, BigDecimal x) throws SQLException {
1079 setBigDecimal(getIndexForName(parameterName), x);
1080 }
1081
1082 /**
1083 * Sets the value of a parameter.
1084 *
1085 * @param parameterName the parameter name
1086 * @param x the value
1087 * @throws SQLException if this object is closed
1088 */
1089 public void setDouble(String parameterName, double x) throws SQLException {
1090 setDouble(getIndexForName(parameterName), x);
1091 }
1092
1093 /**
1094 * Sets the value of a parameter.
1095 *
1096 * @param parameterName the parameter name
1097 * @param x the value
1098 * @throws SQLException if this object is closed
1099 */
1100 public void setFloat(String parameterName, float x) throws SQLException {
1101 setFloat(getIndexForName(parameterName), x);
1102 }
1103
1104 /**
1105 * Sets the value of a parameter.
1106 *
1107 * @param parameterName the parameter name
1108 * @param x the value
1109 * @throws SQLException if this object is closed
1110 */
1111 public void setLong(String parameterName, long x) throws SQLException {
1112 setLong(getIndexForName(parameterName), x);
1113 }
1114
1115 /**
1116 * Sets the value of a parameter.
1117 *
1118 * @param parameterName the parameter name
1119 * @param x the value
1120 * @throws SQLException if this object is closed
1121 */
1122 public void setInt(String parameterName, int x) throws SQLException {
1123 setInt(getIndexForName(parameterName), x);
1124 }
1125
1126 /**
1127 * Sets the value of a parameter.
1128 *
1129 * @param parameterName the parameter name
1130 * @param x the value
1131 * @throws SQLException if this object is closed
1132 */
1133 public void setShort(String parameterName, short x) throws SQLException {
1134 setShort(getIndexForName(parameterName), x);
1135 }
1136
1137 /**
1138 * Sets the value of a parameter.
1139 *
1140 * @param parameterName the parameter name
1141 * @param x the value
1142 * @throws SQLException if this object is closed
1143 */
1144 public void setByte(String parameterName, byte x) throws SQLException {
1145 setByte(getIndexForName(parameterName), x);
1146 }
1147
1148 /**
1149 * Sets the value of a parameter.
1150 *
1151 * @param parameterName the parameter name
1152 * @param x the value
1153 * @throws SQLException if this object is closed
1154 */
1155 public void setBoolean(String parameterName, boolean x) throws SQLException {
1156 setBoolean(getIndexForName(parameterName), x);
1157 }
1158
1159 /**
1160 * [Not supported]
1161 */
1162 public void setURL(String parameterName, URL val) throws SQLException {
1163 throw unsupported("url");
1164 }
1165
1166 /**
1167 * [Not supported] Sets the value of a parameter as a row id.
1168 */
1169 //## Java 1.6 ##
1170 public void setRowId(String parameterName, RowId x)
1171 throws SQLException {
1172 throw unsupported("rowId");
1173 }
1174 //*/
1175
1176 /**
1177 * Sets the value of a parameter.
1178 *
1179 * @param parameterName the parameter name
1180 * @param x the value
1181 * @throws SQLException if this object is closed
1182 */
1183 public void setNString(String parameterName, String x)
1184 throws SQLException {
1185 setNString(getIndexForName(parameterName), x);
1186 }
1187
1188 /**
1189 * Sets the value of a parameter as a character stream.
1190 * This method does not close the reader.
1191 * The reader may be closed after executing the statement.
1192 *
1193 * @param parameterName the parameter name
1194 * @param x the value
1195 * @param length the maximum number of characters
1196 * @throws SQLException if this object is closed
1197 */
1198 public void setNCharacterStream(String parameterName,
1199 Reader x, long length) throws SQLException {
1200 setNCharacterStream(getIndexForName(parameterName), x, length);
1201 }
1202
1203 /**
1204 * Sets the value of a parameter as a Clob.
1205 *
1206 * @param parameterName the parameter name
1207 * @param x the value
1208 * @throws SQLException if this object is closed
1209 */
1210 //## Java 1.6 ##
1211 public void setNClob(String parameterName, NClob x)
1212 throws SQLException {
1213 setNClob(getIndexForName(parameterName), x);
1214 }
1215 //*/
1216
1217 /**
1218 * Sets the value of a parameter as a Clob.
1219 * This method does not close the reader.
1220 * The reader may be closed after executing the statement.
1221 *
1222 * @param parameterName the parameter name
1223 * @param x the value
1224 * @param length the maximum number of characters
1225 * @throws SQLException if this object is closed
1226 */
1227 public void setClob(String parameterName, Reader x,
1228 long length) throws SQLException {
1229 setClob(getIndexForName(parameterName), x, length);
1230 }
1231
1232 /**
1233 * Sets the value of a parameter as a Blob.
1234 * This method does not close the stream.
1235 * The stream may be closed after executing the statement.
1236 *
1237 * @param parameterName the parameter name
1238 * @param x the value
1239 * @param length the maximum number of bytes
1240 * @throws SQLException if this object is closed
1241 */
1242 public void setBlob(String parameterName, InputStream x,
1243 long length) throws SQLException {
1244 setBlob(getIndexForName(parameterName), x, length);
1245 }
1246
1247 /**
1248 * Sets the value of a parameter as a Clob.
1249 * This method does not close the reader.
1250 * The reader may be closed after executing the statement.
1251 *
1252 * @param parameterName the parameter name
1253 * @param x the value
1254 * @param length the maximum number of characters
1255 * @throws SQLException if this object is closed
1256 */
1257 public void setNClob(String parameterName, Reader x,
1258 long length) throws SQLException {
1259 setNClob(getIndexForName(parameterName), x, length);
1260 }
1261
1262 /**
1263 * Sets the value of a parameter as a Blob.
1264 *
1265 * @param parameterName the parameter name
1266 * @param x the value
1267 * @throws SQLException if this object is closed
1268 */
1269 public void setBlob(String parameterName, Blob x)
1270 throws SQLException {
1271 setBlob(getIndexForName(parameterName), x);
1272 }
1273
1274 /**
1275 * Sets the value of a parameter as a Clob.
1276 *
1277 * @param parameterName the parameter name
1278 * @param x the value
1279 * @throws SQLException if this object is closed
1280 */
1281 public void setClob(String parameterName, Clob x) throws SQLException {
1282 setClob(getIndexForName(parameterName), x);
1283 }
1284
1285 /**
1286 * Sets the value of a parameter as an ASCII stream.
1287 * This method does not close the stream.
1288 * The stream may be closed after executing the statement.
1289 *
1290 * @param parameterName the parameter name
1291 * @param x the value
1292 * @throws SQLException if this object is closed
1293 */
1294 public void setAsciiStream(String parameterName, InputStream x)
1295 throws SQLException {
1296 setAsciiStream(getIndexForName(parameterName), x);
1297 }
1298
1299 /**
1300 * Sets the value of a parameter as an ASCII stream.
1301 * This method does not close the stream.
1302 * The stream may be closed after executing the statement.
1303 *
1304 * @param parameterName the parameter name
1305 * @param x the value
1306 * @param length the maximum number of bytes
1307 * @throws SQLException if this object is closed
1308 */
1309 public void setAsciiStream(String parameterName,
1310 InputStream x, int length) throws SQLException {
1311 setAsciiStream(getIndexForName(parameterName), x, length);
1312 }
1313
1314 /**
1315 * Sets the value of a parameter as an input stream.
1316 * This method does not close the stream.
1317 * The stream may be closed after executing the statement.
1318 *
1319 * @param parameterName the parameter name
1320 * @param x the value
1321 * @throws SQLException if this object is closed
1322 */
1323 public void setBinaryStream(String parameterName,
1324 InputStream x) throws SQLException {
1325 setBinaryStream(getIndexForName(parameterName), x);
1326 }
1327
1328 /**
1329 * Sets the value of a parameter as an input stream.
1330 * This method does not close the stream.
1331 * The stream may be closed after executing the statement.
1332 *
1333 * @param parameterName the parameter name
1334 * @param x the value
1335 * @param length the maximum number of bytes
1336 * @throws SQLException if this object is closed
1337 */
1338 public void setBinaryStream(String parameterName,
1339 InputStream x, long length) throws SQLException {
1340 setBinaryStream(getIndexForName(parameterName), x, length);
1341 }
1342
1343 /**
1344 * Sets the value of a parameter as a Blob.
1345 * This method does not close the stream.
1346 * The stream may be closed after executing the statement.
1347 *
1348 * @param parameterName the parameter name
1349 * @param x the value
1350 * @throws SQLException if this object is closed
1351 */
1352 public void setBlob(String parameterName, InputStream x)
1353 throws SQLException {
1354 setBlob(getIndexForName(parameterName), x);
1355 }
1356
1357 /**
1358 * Sets the value of a parameter as a character stream.
1359 * This method does not close the reader.
1360 * The reader may be closed after executing the statement.
1361 *
1362 * @param parameterName the parameter name
1363 * @param x the value
1364 * @throws SQLException if this object is closed
1365 */
1366 public void setCharacterStream(String parameterName, Reader x)
1367 throws SQLException {
1368 setCharacterStream(getIndexForName(parameterName), x);
1369 }
1370
1371 /**
1372 * Sets the value of a parameter as a character stream.
1373 * This method does not close the reader.
1374 * The reader may be closed after executing the statement.
1375 *
1376 * @param parameterName the parameter name
1377 * @param x the value
1378 * @param length the maximum number of characters
1379 * @throws SQLException if this object is closed
1380 */
1381 public void setCharacterStream(String parameterName,
1382 Reader x, long length) throws SQLException {
1383 setCharacterStream(getIndexForName(parameterName), x, length);
1384 }
1385
1386 /**
1387 * Sets the value of a parameter as a character stream.
1388 * This method does not close the reader.
1389 * The reader may be closed after executing the statement.
1390 *
1391 * @param parameterName the parameter name
1392 * @param x the value
1393 * @throws SQLException if this object is closed
1394 */
1395 public void setClob(String parameterName, Reader x) throws SQLException {
1396 setClob(getIndexForName(parameterName), x);
1397 }
1398
1399 /**
1400 * Sets the value of a parameter as a character stream.
1401 * This method does not close the reader.
1402 * The reader may be closed after executing the statement.
1403 *
1404 * @param parameterName the parameter name
1405 * @param x the value
1406 * @throws SQLException if this object is closed
1407 */
1408 public void setNCharacterStream(String parameterName, Reader x)
1409 throws SQLException {
1410 setNCharacterStream(getIndexForName(parameterName), x);
1411 }
1412
1413 /**
1414 * Sets the value of a parameter as a Clob.
1415 * This method does not close the reader.
1416 * The reader may be closed after executing the statement.
1417 *
1418 * @param parameterName the parameter name
1419 * @param x the value
1420 * @throws SQLException if this object is closed
1421 */
1422 public void setNClob(String parameterName, Reader x)
1423 throws SQLException {
1424 setNClob(getIndexForName(parameterName), x);
1425 }
1426
1427 /**
1428 * [Not supported] Sets the value of a parameter as a SQLXML object.
1429 */
1430 //## Java 1.6 ##
1431 public void setSQLXML(String parameterName, SQLXML x)
1432 throws SQLException {
1433 throw unsupported("SQLXML");
1434 }
1435 //*/
1436
1437 /**
1438 * [Not supported]
1439 *
1440 * @param parameterIndex the parameter index (1, 2, ...)
1441 * @param type the class of the returned value
1442 */
1443 /*## Java 1.7 ##
1444 public <T> T getObject(int parameterIndex, Class<T> type) {
1445 return null;
1446 }
1447 //*/
1448
1449 /**
1450 * [Not supported]
1451 *
1452 * @param parameterName the parameter name
1453 * @param type the class of the returned value
1454 */
1455 /*## Java 1.7 ##
1456 public <T> T getObject(String parameterName, Class<T> type) {
1457 return null;
1458 }
1459 //*/
1460
1461 private ResultSetMetaData getCheckedMetaData() throws SQLException {
1462 ResultSetMetaData meta = getMetaData();
1463 if (meta == null) {
1464 throw DbException.getUnsupportedException("Supported only for calling stored procedures");
1465 }
1466 return meta;
1467 }
1468
1469 private void checkIndexBounds(int parameterIndex) {
1470 checkClosed();
1471 if (parameterIndex < 1 || parameterIndex > maxOutParameters) {
1472 throw DbException.getInvalidValueException("parameterIndex", parameterIndex);
1473 }
1474 }
1475
1476 private void registerOutParameter(int parameterIndex) throws SQLException {
1477 try {
1478 checkClosed();
1479 if (outParameters == null) {
1480 maxOutParameters = Math.min(
1481 getParameterMetaData().getParameterCount(),
1482 getCheckedMetaData().getColumnCount());
1483 outParameters = new BitField();
1484 }
1485 checkIndexBounds(parameterIndex);
1486 ParameterInterface param = command.getParameters().get(--parameterIndex);
1487 if (param.getParamValue() == null) {
1488 param.setValue(ValueNull.INSTANCE, false);
1489 }
1490 outParameters.set(parameterIndex);
1491 } catch (Exception e) {
1492 throw logAndConvert(e);
1493 }
1494 }
1495
1496 private void checkRegistered(int parameterIndex) throws SQLException {
1497 try {
1498 checkIndexBounds(parameterIndex);
1499 if (!outParameters.get(parameterIndex - 1)) {
1500 throw DbException.getInvalidValueException("parameterIndex", parameterIndex);
1501 }
1502 } catch (Exception e) {
1503 throw logAndConvert(e);
1504 }
1505 }
1506
1507 private int getIndexForName(String parameterName) throws SQLException {
1508 try {
1509 checkClosed();
1510 if (namedParameters == null) {
1511 ResultSetMetaData meta = getCheckedMetaData();
1512 int columnCount = meta.getColumnCount();
1513 HashMap<String, Integer> map = New.hashMap(columnCount);
1514 for (int i = 1; i <= columnCount; i++) {
1515 map.put(meta.getColumnLabel(i), i);
1516 }
1517 namedParameters = map;
1518 }
1519 Integer index = namedParameters.get(parameterName);
1520 if (index == null) {
1521 throw DbException.getInvalidValueException("parameterName", parameterName);
1522 }
1523 return index;
1524 } catch (Exception e) {
1525 throw logAndConvert(e);
1526 }
1527 }
1528
1529 private JdbcResultSet getOpenResultSet() throws SQLException {
1530 try {
1531 checkClosed();
1532 if (resultSet == null) {
1533 throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
1534 }
1535 if (resultSet.isBeforeFirst()) {
1536 resultSet.next();
1537 }
1538 return resultSet;
1539 } catch (Exception e) {
1540 throw logAndConvert(e);
1541 }
1542 }
1543
1544 }
1545