KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > jdbc > BrokeredPreparedStatement


1 /*
2
3    Derby - Class org.apache.derby.iapi.jdbc.BrokeredPreparedStatement
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.jdbc;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.util.Calendar JavaDoc;
27
28 import java.sql.*;
29 import java.net.URL JavaDoc;
30
31 /**
32     JDBC 2 brokered PreparedStatement. Forwards calls off to a real prepared statement
33     obtained through the BrokeredStatementControl getRealPreparedStatement method.
34  */

35 public class BrokeredPreparedStatement extends BrokeredStatement
36     implements EnginePreparedStatement
37 {
38
39     /**
40         SQL used to create me.
41     */

42     final String JavaDoc sql;
43
44     public BrokeredPreparedStatement(BrokeredStatementControl control, int jdbcLevel, String JavaDoc sql) throws SQLException
45     {
46         super(control, jdbcLevel);
47         this.sql = sql;
48     }
49
50     /**
51      * Imitate the getParameterMetaData() function in JDBC 3.0
52      *
53      * Retrieves the number, types and properties of this PreparedStatement
54      * object's parameters.
55      *
56      * @return a EngineParameterMetaData object that contains information about the
57      * number, types and properties of this PreparedStatement object's parameters.
58      * @exception SQLException if a database access error occurs
59      */

60     public EngineParameterMetaData getEmbedParameterSetMetaData()
61     throws SQLException
62     {
63         return ((EnginePreparedStatement)getPreparedStatement()).getEmbedParameterSetMetaData();
64     }
65     /**
66      * A prepared SQL query is executed and its ResultSet is returned.
67      *
68      * @return a ResultSet that contains the data produced by the
69      * query; never null
70      * @exception SQLException thrown on failure.
71      */

72     public final ResultSet executeQuery() throws SQLException
73     {
74         return wrapResultSet(getPreparedStatement().executeQuery());
75     }
76
77     /**
78      * Execute a SQL INSERT, UPDATE or DELETE statement. In addition,
79      * SQL statements that return nothing such as SQL DDL statements
80      * can be executed.
81      *
82      * @return either the row count for INSERT, UPDATE or DELETE; or 0
83      * for SQL statements that return nothing
84      * @exception SQLException thrown on failure.
85      */

86     public final int executeUpdate() throws SQLException
87     {
88         return getPreparedStatement().executeUpdate();
89     }
90
91     /**
92      * Set a parameter to SQL NULL.
93      *
94      * <P><B>Note:</B> You must specify the parameter's SQL type.
95      *
96      * @param parameterIndex the first parameter is 1, the second is 2, ...
97      * @param sqlType SQL type code defined by java.sql.Types
98      * @exception SQLException thrown on failure.
99      */

100     public final void setNull(int parameterIndex, int sqlType) throws SQLException
101     {
102         getPreparedStatement().setNull( parameterIndex, sqlType);
103     }
104
105     /**
106      * Set a parameter to SQL NULL.
107      *
108      * <P><B>Note:</B> You must specify the parameter's SQL type.
109      *
110      * @param parameterIndex the first parameter is 1, the second is 2, ...
111      * @param sqlType SQL type code defined by java.sql.Types
112      * @exception SQLException thrown on failure.
113      */

114     public final void setNull(int parameterIndex, int sqlType, String JavaDoc typeName) throws SQLException
115     {
116         getPreparedStatement().setNull( parameterIndex, sqlType, typeName);
117     }
118
119     /**
120      * Set a parameter to a Java boolean value. According to the JDBC API spec,
121      * the driver converts this to a SQL BIT value when it sends it to the
122      * database. But we don't have to do this, since the database engine
123      * supports a boolean type.
124      *
125      * @param parameterIndex the first parameter is 1, the second is 2, ...
126      * @param x the parameter value
127      * @exception SQLException thrown on failure.
128      */

129     public final void setBoolean(int parameterIndex, boolean x) throws SQLException
130     {
131         getPreparedStatement().setBoolean( parameterIndex, x);
132     }
133
134     /**
135      * Set a parameter to a Java byte value. The driver converts this
136      * to a SQL TINYINT value when it sends it to the database.
137      *
138      * @param parameterIndex the first parameter is 1, the second is 2, ...
139      * @param x the parameter value
140      * @exception SQLException thrown on failure.
141      */

142     public final void setByte(int parameterIndex, byte x) throws SQLException
143     {
144         getPreparedStatement().setByte( parameterIndex, x);
145     }
146
147     /**
148      * Set a parameter to a Java short value. The driver converts this
149      * to a SQL SMALLINT value when it sends it to the database.
150      *
151      * @param parameterIndex the first parameter is 1, the second is 2, ...
152      * @param x the parameter value
153      * @exception SQLException thrown on failure.
154      */

155     public final void setShort(int parameterIndex, short x) throws SQLException
156     {
157         getPreparedStatement().setShort( parameterIndex, x);
158     }
159
160     /**
161      * Set a parameter to a Java int value. The driver converts this
162      * to a SQL INTEGER value when it sends it to the database.
163      *
164      * @param parameterIndex the first parameter is 1, the second is 2, ...
165      * @param x the parameter value
166      * @exception SQLException thrown on failure.
167      */

168     public final void setInt(int parameterIndex, int x) throws SQLException
169     {
170         getPreparedStatement().setInt( parameterIndex, x);
171     }
172
173     /**
174      * Set a parameter to a Java long value. The driver converts this
175      * to a SQL BIGINT value when it sends it to the database.
176      *
177      * @param parameterIndex the first parameter is 1, the second is 2, ...
178      * @param x the parameter value
179      * @exception SQLException thrown on failure.
180      */

181     public final void setLong(int parameterIndex, long x) throws SQLException
182     {
183         getPreparedStatement().setLong( parameterIndex, x);
184     }
185
186     /**
187      * Set a parameter to a Java float value. The driver converts this
188      * to a SQL FLOAT value when it sends it to the database.
189      *
190      * @param parameterIndex the first parameter is 1, the second is 2, ...
191      * @param x the parameter value
192      * @exception SQLException thrown on failure.
193      */

194     public final void setFloat(int parameterIndex, float x) throws SQLException
195     {
196         getPreparedStatement().setFloat( parameterIndex, x);
197     }
198
199     /**
200      * Set a parameter to a Java double value. The driver converts this
201      * to a SQL DOUBLE value when it sends it to the database.
202      *
203      * @param parameterIndex the first parameter is 1, the second is 2, ...
204      * @param x the parameter value
205      * @exception SQLException thrown on failure.
206      */

207     public final void setDouble(int parameterIndex, double x) throws SQLException
208     {
209         getPreparedStatement().setDouble( parameterIndex, x);
210     }
211
212
213     /**
214      * Set a parameter to a java.math.BigDecimal value.
215      * The driver converts this to a SQL NUMERIC value when
216      * it sends it to the database.
217      *
218      * @param parameterIndex the first parameter is 1, the second is 2, ...
219      * @param x the parameter value
220      * @exception SQLException thrown on failure.
221      */

222     public final void setBigDecimal(int parameterIndex, java.math.BigDecimal JavaDoc x) throws SQLException
223     {
224         getPreparedStatement().setBigDecimal( parameterIndex, x);
225     }
226
227     /**
228      * Set a parameter to a Java String value. The driver converts this
229      * to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments
230      * size relative to the driver's limits on VARCHARs) when it sends
231      * it to the database.
232      *
233      * @param parameterIndex the first parameter is 1, the second is 2, ...
234      * @param x the parameter value
235      * @exception SQLException thrown on failure.
236      */

237     public final void setString(int parameterIndex, String JavaDoc x) throws SQLException
238     {
239         getPreparedStatement().setString( parameterIndex, x);
240     }
241
242     /**
243      * Set a parameter to a Java array of bytes. The driver converts
244      * this to a SQL VARBINARY or LONGVARBINARY (depending on the
245      * argument's size relative to the driver's limits on VARBINARYs)
246      * when it sends it to the database.
247      *
248      * @param parameterIndex the first parameter is 1, the second is 2, ...
249      * @param x the parameter value
250      * @exception SQLException thrown on failure.
251      */

252     public final void setBytes(int parameterIndex, byte[] x) throws SQLException
253     {
254         getPreparedStatement().setBytes( parameterIndex, x);
255     }
256
257     /**
258      * Set a parameter to a java.sql.Date value. The driver converts this
259      * to a SQL DATE value when it sends it to the database.
260      *
261      * @param parameterIndex the first parameter is 1, the second is 2, ...
262      * @param x the parameter value
263      * @exception SQLException thrown on failure.
264      */

265     public final void setDate(int parameterIndex, Date x) throws SQLException
266     {
267         getPreparedStatement().setDate( parameterIndex, x);
268     }
269
270     /**
271      * Set a parameter to a java.sql.Time value. The driver converts this
272      * to a SQL TIME value when it sends it to the database.
273      *
274      * @param parameterIndex the first parameter is 1, the second is 2, ...
275      * @param x the parameter value
276      * @exception SQLException thrown on failure.
277      */

278     public final void setTime(int parameterIndex, Time x) throws SQLException
279     {
280         getPreparedStatement().setTime( parameterIndex, x);
281     }
282
283     /**
284      * Set a parameter to a java.sql.Timestamp value. The driver
285      * converts this to a SQL TIMESTAMP value when it sends it to the
286      * database.
287      *
288      * @param parameterIndex the first parameter is 1, the second is 2, ...
289      * @param x the parameter value
290      * @exception SQLException thrown on failure.
291      */

292     public final void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
293     {
294         getPreparedStatement().setTimestamp( parameterIndex, x);
295     }
296
297     /**
298      * We do this inefficiently and read it all in here. The target type
299      * is assumed to be a String.
300      *
301      * @param parameterIndex the first parameter is 1, the second is 2, ...
302      * @param x the java input stream which contains the ASCII parameter value
303      * @param length the number of bytes in the stream
304      * @exception SQLException thrown on failure.
305      */

306     public final void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
307     {
308         getPreparedStatement().setAsciiStream( parameterIndex, x, length);
309     }
310
311     /**
312      * We do this inefficiently and read it all in here. The target type
313      * is assumed to be a String. The unicode source is assumed to be
314      * in char[]. RESOLVE: might it be in UTF, instead? that'd be faster!
315      *
316      * @param parameterIndex the first parameter is 1, the second is 2, ...
317      * @param x the java input stream which contains the
318      * UNICODE parameter value
319      * @param length the number of bytes in the stream
320      * @exception SQLException thrown on failure.
321      */

322     public final void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
323     {
324         getPreparedStatement().setUnicodeStream( parameterIndex, x, length);
325     }
326
327     /**
328      * @param parameterIndex the first parameter is 1, the second is 2, ...
329      * @param x the java input stream which contains the binary parameter value
330      * @param length the number of bytes in the stream
331      * @exception SQLException thrown on failure.
332      */

333     public final void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
334     {
335         getPreparedStatement().setBinaryStream( parameterIndex, x, length);
336     }
337
338     /**
339      * JDBC 2.0
340      *
341      * Add a set of parameters to the batch.
342      *
343      * @exception SQLException if a database-access error occurs.
344      */

345     public final void addBatch() throws SQLException
346     {
347         getPreparedStatement().addBatch( );
348     }
349
350     /**
351      * <P>In general, parameter values remain in force for repeated use of a
352      * Statement. Setting a parameter value automatically clears its
353      * previous value. However, in some cases it is useful to immediately
354      * release the resources used by the current parameter values; this can
355      * be done by calling clearParameters.
356      * @exception SQLException thrown on failure.
357      */

358     public final void clearParameters() throws SQLException
359     {
360         getPreparedStatement().clearParameters( );
361     }
362
363     /**
364      * JDBC 2.0
365      *
366      * The number, types and properties of a ResultSet's columns
367      * are provided by the getMetaData method.
368      *
369      * @return the description of a ResultSet's columns
370      * @exception SQLException Feature not implemented for now.
371      */

372     public final java.sql.ResultSetMetaData JavaDoc getMetaData() throws SQLException
373     {
374         return getPreparedStatement().getMetaData();
375     }
376
377     /**
378      * The interface says that the type of the Object parameter must
379      * be compatible with the type of the targetSqlType. We check that,
380      * and if it flies, we expect the underlying engine to do the
381      * required conversion once we pass in the value using its type.
382      * So, an Integer converting to a CHAR is done via setInteger()
383      * support on the underlying CHAR type.
384      *
385      * <p>If x is null, it won't tell us its type, so we pass it on to setNull
386      *
387      * @param parameterIndex The first parameter is 1, the second is 2, ...
388      * @param x The object containing the input parameter value
389      * @param targetSqlType The SQL type (as defined in java.sql.Types) to be
390      * sent to the database. The scale argument may further qualify this type.
391      * @param scale For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types
392      * this is the number of digits after the decimal. For all other
393      * types this value will be ignored,
394      * @exception SQLException thrown on failure.
395      */

396     public final void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale)
397         throws SQLException
398     {
399         getPreparedStatement().setObject( parameterIndex, x, targetSqlType, scale);
400     }
401         
402     /**
403       * This method is like setObject above, but assumes a scale of zero.
404       * @exception SQLException thrown on failure.
405       */

406     public final void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType)
407         throws SQLException
408     {
409         getPreparedStatement().setObject( parameterIndex, x, targetSqlType);
410     }
411
412     /**
413      * <p>Set the value of a parameter using an object; use the
414      * java.lang equivalent objects for integral values.
415      *
416      * <p>The JDBC specification specifies a standard mapping from
417      * Java Object types to SQL types. The given argument java object
418      * will be converted to the corresponding SQL type before being
419      * sent to the database.
420      *
421      * <p>Note that this method may be used to pass datatabase
422      * specific abstract data types, by using a Driver specific Java
423      * type.
424      *
425      * @param parameterIndex The first parameter is 1, the second is 2, ...
426      * @param x The object containing the input parameter value
427      * @exception SQLException thrown on failure.
428      */

429     public final void setObject(int parameterIndex, Object JavaDoc x)
430         throws SQLException
431     {
432         getPreparedStatement().setObject( parameterIndex, x);
433     }
434
435     /**
436      * @see java.sql.Statement#execute
437      * @exception SQLException thrown on failure.
438      */

439     public final boolean execute() throws SQLException
440     {
441         return getPreparedStatement().execute();
442     }
443
444     public final void setCharacterStream(int parameterIndex,
445                                    Reader JavaDoc reader,
446                                    int length)
447         throws SQLException
448     {
449         getPreparedStatement().setCharacterStream( parameterIndex, reader, length);
450     }
451
452     public final void setRef(int i,
453                        Ref x)
454         throws SQLException
455     {
456         getPreparedStatement().setRef( i, x);
457     }
458
459     public final void setBlob(int i,
460                        Blob x)
461         throws SQLException
462     {
463         getPreparedStatement().setBlob( i, x);
464     }
465
466     public final void setClob(int i,
467                        Clob x)
468         throws SQLException
469     {
470         getPreparedStatement().setClob( i, x);
471     }
472
473     public final void setArray(int i,
474                          Array x)
475         throws SQLException
476     {
477         getPreparedStatement().setArray( i, x);
478     }
479
480     public final void setDate(int i,
481                         Date x,
482                         Calendar JavaDoc cal)
483         throws SQLException
484     {
485         getPreparedStatement().setDate( i, x, cal);
486     }
487
488     public final void setTime(int i,
489                         Time x,
490                         Calendar JavaDoc cal)
491         throws SQLException
492     {
493         getPreparedStatement().setTime( i, x, cal);
494     }
495
496     public final void setTimestamp(int i,
497                              Timestamp x,
498                              Calendar JavaDoc cal)
499         throws SQLException
500     {
501         getPreparedStatement().setTimestamp( i, x, cal);
502     }
503
504     /*
505     ** Control methods.
506     */

507
508     /**
509      * Access the underlying PreparedStatement. This method
510      * is package protected to restrict access to the underlying
511      * object to the brokered objects. Allowing the application to
512      * access the underlying object thtough a public method would
513      *
514      */

515     PreparedStatement getPreparedStatement() throws SQLException {
516         return control.getRealPreparedStatement();
517     }
518
519     /**
520         Override the BrokeredStatement's getStatement() to always return a PreparedStatement.
521     */

522     public final Statement getStatement() throws SQLException {
523         return getPreparedStatement();
524     }
525
526     /**
527         Create a duplicate PreparedStatement to this, including state, from the passed in Connection.
528     */

529     public PreparedStatement createDuplicateStatement(Connection conn, PreparedStatement oldStatement) throws SQLException {
530
531         PreparedStatement newStatement = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
532
533         setStatementState(oldStatement, newStatement);
534
535         return newStatement;
536     }
537 }
538
Popular Tags