KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > internetcds > jdbc > tds > CallableStatement_base


1 //
2
// Copyright 1998, 1999 CDS Networks, Inc., Medford Oregon
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// 3. All advertising materials mentioning features or use of this software
14
// must display the following acknowledgement:
15
// This product includes software developed by CDS Networks, Inc.
16
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
17
// products derived from this software without specific prior
18
// written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
21
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
24
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
// SUCH DAMAGE.
31
//
32

33
34 package com.internetcds.jdbc.tds;
35
36 import java.sql.*;
37 import java.math.BigDecimal JavaDoc;
38 import java.util.Calendar JavaDoc;
39
40
41 /**
42  * <P>CallableStatement is used to execute SQL stored procedures.
43  *
44  * <P>JDBC provides a stored procedure SQL escape that allows stored
45  * procedures to be called in a standard way for all RDBMS's. This
46  * escape syntax has one form that includes a result parameter and one
47  * that does not. If used, the result parameter must be registered as
48  * an OUT parameter. The other parameters may be used for input,
49  * output or both. Parameters are refered to sequentially, by
50  * number. The first parameter is 1.
51  *
52  * <P><CODE>
53  * {?= call <procedure-name>[<arg1>,<arg2>, ...]}<BR>
54  * {call <procedure-name>[<arg1>,<arg2>, ...]}
55  * </CODE>
56  *
57  * <P>IN parameter values are set using the set methods inherited from
58  * PreparedStatement. The type of all OUT parameters must be
59  * registered prior to executing the stored procedure; their values
60  * are retrieved after execution via the get methods provided here.
61  *
62  * <P>A Callable statement may return a ResultSet or multiple
63  * ResultSets. Multiple ResultSets are handled using operations
64  * inherited from Statement.
65  *
66  * <P>For maximum portability, a call's ResultSets and update counts
67  * should be processed prior to getting the values of output
68  * parameters.
69  *
70  * @see Connection#prepareCall
71  * @see ResultSet
72  */

73 public class CallableStatement_base
74    extends com.internetcds.jdbc.tds.PreparedStatement_base
75 {
76    public static final String JavaDoc cvsVersion = "$Id: CallableStatement_base.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $";
77
78
79    private String JavaDoc procedureName = null;
80
81
82    public CallableStatement_base(
83       java.sql.Connection JavaDoc conn_,
84       Tds tds_,
85       String JavaDoc sql)
86       throws SQLException
87    {
88       super(conn_, tds_, sql);
89       int i;
90       procedureName = "";
91       i = 0;
92       while(i<sql.length()
93             && (!
94                 (Character.isLetterOrDigit(sql.charAt(i))
95                  || sql.charAt(i) == '#')))
96       {
97          i++;
98       }
99       
100       while(i<sql.length()
101             && (Character.isLetterOrDigit(sql.charAt(i))
102                 || sql.charAt(i) == '#'
103                 || sql.charAt(i) == '_'))
104       {
105          procedureName = procedureName + sql.charAt(i);
106          i++;
107       }
108       
109       if (procedureName.length() == 0)
110       {
111          throw new SQLException("Did not find name in sql string");
112       }
113    }
114
115
116
117
118    /**
119     * Get the value of a NUMERIC parameter as a java.math.BigDecimal object.
120     *
121     * @param parameterIndex the first parameter is 1, the second is 2, ...
122     *
123     * @param scale a value greater than or equal to zero representing the
124     * desired number of digits to the right of the decimal point
125     *
126     * @return the parameter value; if the value is SQL NULL, the result is
127     * null
128     * @exception SQLException if a database-access error occurs.
129     */

130    public BigDecimal JavaDoc getBigDecimal(int parameterIndex, int scale)
131       throws SQLException
132    {
133       throw new SQLException("Not implemented");
134    }
135
136
137    /**
138     * Get the value of a BIT parameter as a Java boolean.
139     *
140     * @param parameterIndex the first parameter is 1, the second is 2, ...
141     * @return the parameter value; if the value is SQL NULL, the result is false
142     * @exception SQLException if a database-access error occurs.
143     */

144    public boolean getBoolean(int parameterIndex) throws SQLException
145    {
146       throw new SQLException("Not implemented");
147    }
148
149
150    /**
151     * Get the value of a TINYINT parameter as a Java byte.
152     *
153     * @param parameterIndex the first parameter is 1, the second is 2, ...
154     * @return the parameter value; if the value is SQL NULL, the result is 0
155     * @exception SQLException if a database-access error occurs.
156     */

157    public byte getByte(int parameterIndex) throws SQLException
158    {
159       throw new SQLException("Not implemented");
160    }
161
162
163    /**
164     * Get the value of a SQL BINARY or VARBINARY parameter as a Java byte[]
165     *
166     * @param parameterIndex the first parameter is 1, the second is 2, ...
167     * @return the parameter value; if the value is SQL NULL, the result is null
168     * @exception SQLException if a database-access error occurs.
169     */

170    public byte[] getBytes(int parameterIndex) throws SQLException
171    {
172       throw new SQLException("Not implemented");
173    }
174
175
176    /**
177     * Get the value of a SQL DATE parameter as a java.sql.Date object
178     *
179     * @param parameterIndex the first parameter is 1, the second is 2, ...
180     * @return the parameter value; if the value is SQL NULL, the result is null
181     * @exception SQLException if a database-access error occurs.
182     */

183    public java.sql.Date JavaDoc getDate(int parameterIndex) throws SQLException
184    {
185       throw new SQLException("Not implemented");
186    }
187
188
189    /**
190     * Get the value of a DOUBLE parameter as a Java double.
191     *
192     * @param parameterIndex the first parameter is 1, the second is 2, ...
193     * @return the parameter value; if the value is SQL NULL, the result is 0
194     * @exception SQLException if a database-access error occurs.
195     */

196    public double getDouble(int parameterIndex) throws SQLException
197    {
198       throw new SQLException("Not implemented");
199    }
200
201
202    /**
203     * Get the value of a FLOAT parameter as a Java float.
204     *
205     * @param parameterIndex the first parameter is 1, the second is 2, ...
206     * @return the parameter value; if the value is SQL NULL, the result is 0
207     * @exception SQLException if a database-access error occurs.
208     */

209    public float getFloat(int parameterIndex) throws SQLException
210    {
211       throw new SQLException("Not implemented");
212    }
213
214
215    /**
216     * Get the value of an INTEGER parameter as a Java int.
217     *
218     * @param parameterIndex the first parameter is 1, the second is 2, ...
219     * @return the parameter value; if the value is SQL NULL, the result is 0
220     * @exception SQLException if a database-access error occurs.
221     */

222    public int getInt(int parameterIndex) throws SQLException
223    {
224       throw new SQLException("Not implemented");
225    }
226
227
228    /**
229     * Get the value of a BIGINT parameter as a Java long.
230     *
231     * @param parameterIndex the first parameter is 1, the second is 2, ...
232     * @return the parameter value; if the value is SQL NULL, the result is 0
233     * @exception SQLException if a database-access error occurs.
234     */

235    public long getLong(int parameterIndex) throws SQLException
236    {
237       throw new SQLException("Not implemented");
238    }
239
240
241    //----------------------------------------------------------------------
242
// Advanced features:
243

244
245    /**
246     * Get the value of a parameter as a Java object.
247     *
248     * <p>This method returns a Java object whose type coresponds to the SQL
249     * type that was registered for this parameter using registerOutParameter.
250     *
251     * <p>Note that this method may be used to read
252     * datatabase-specific, abstract data types. This is done by
253     * specifying a targetSqlType of java.sql.types.OTHER, which
254     * allows the driver to return a database-specific Java type.
255     *
256     * @param parameterIndex The first parameter is 1, the second is 2, ...
257     * @return A java.lang.Object holding the OUT parameter value.
258     * @exception SQLException if a database-access error occurs.
259     * @see Types
260     */

261    public Object JavaDoc getObject(int parameterIndex) throws SQLException
262    {
263       throw new SQLException("Not implemented");
264    }
265
266
267    /**
268     * Get the value of a SMALLINT parameter as a Java short.
269     *
270     * @param parameterIndex the first parameter is 1, the second is 2, ...
271     * @return the parameter value; if the value is SQL NULL, the result is 0
272     * @exception SQLException if a database-access error occurs.
273     */

274    public short getShort(int parameterIndex) throws SQLException
275    {
276       throw new SQLException("Not implemented");
277    }
278
279
280    /**
281     * Get the value of a CHAR, VARCHAR, or LONGVARCHAR parameter as a Java String.
282     *
283     * @param parameterIndex the first parameter is 1, the second is 2, ...
284     * @return the parameter value; if the value is SQL NULL, the result is null
285     * @exception SQLException if a database-access error occurs.
286     */

287    public String JavaDoc getString(int parameterIndex) throws SQLException
288    {
289       throw new SQLException("Not implemented");
290    }
291
292
293    /**
294     * Get the value of a SQL TIME parameter as a java.sql.Time object.
295     *
296     * @param parameterIndex the first parameter is 1, the second is 2, ...
297     * @return the parameter value; if the value is SQL NULL, the result is null
298     * @exception SQLException if a database-access error occurs.
299     */

300    public java.sql.Time JavaDoc getTime(int parameterIndex) throws SQLException
301    {
302       throw new SQLException("Not implemented");
303    }
304
305
306    /**
307     * Get the value of a SQL TIMESTAMP parameter as a java.sql.Timestamp object.
308     *
309     * @param parameterIndex the first parameter is 1, the second is 2, ...
310     * @return the parameter value; if the value is SQL NULL, the result is null
311     * @exception SQLException if a database-access error occurs.
312     */

313    public java.sql.Timestamp JavaDoc getTimestamp(int parameterIndex)
314       throws SQLException
315    {
316       throw new SQLException("Not implemented");
317    }
318
319
320    /**
321     * Before executing a stored procedure call, you must explicitly
322     * call registerOutParameter to register the java.sql.Type of each
323     * out parameter.
324     *
325     * <P><B>Note:</B> When reading the value of an out parameter, you
326     * must use the getXXX method whose Java type XXX corresponds to the
327     * parameter's registered SQL type.
328     *
329     * @param parameterIndex the first parameter is 1, the second is 2,...
330     * @param sqlType SQL type code defined by java.sql.Types;
331     * for parameters of type Numeric or Decimal use the version of
332     * registerOutParameter that accepts a scale value
333     * @exception SQLException if a database-access error occurs.
334     * @see Type
335     */

336    public void registerOutParameter(int parameterIndex, int sqlType)
337       throws SQLException
338    {
339       throw new SQLException("Not implemented");
340    }
341
342
343    /**
344     * Use this version of registerOutParameter for registering
345     * Numeric or Decimal out parameters.
346     *
347     * <P><B>Note:</B> When reading the value of an out parameter, you
348     * must use the getXXX method whose Java type XXX corresponds to the
349     * parameter's registered SQL type.
350     *
351     * @param parameterIndex the first parameter is 1, the second is 2, ...
352     * @param sqlType use either java.sql.Type.NUMERIC or java.sql.Type.DECIMAL
353     * @param scale a value greater than or equal to zero representing the
354     * desired number of digits to the right of the decimal point
355     * @exception SQLException if a database-access error occurs.
356     * @see Type
357     */

358    public void registerOutParameter(int parameterIndex, int sqlType, int scale)
359       throws SQLException
360    {
361       throw new SQLException("Not implemented");
362    }
363
364
365    /**
366     * An OUT parameter may have the value of SQL NULL; wasNull reports
367     * whether the last value read has this special value.
368     *
369     * <P><B>Note:</B> You must first call getXXX on a parameter to
370     * read its value and then call wasNull() to see if the value was
371     * SQL NULL.
372     *
373     * @return true if the last parameter read was SQL NULL
374     * @exception SQLException if a database-access error occurs.
375     */

376    public boolean wasNull() throws SQLException
377    {
378       throw new SQLException("Not implemented");
379    }
380
381    public boolean execute()
382       throws SQLException
383    {
384       boolean result;
385
386       closeResults();
387       updateCount = -2;
388
389       // First make sure the caller has filled in all the parameters.
390
ParameterUtils.verifyThatParametersAreSet(parameterList);
391
392       // execute the stored procedure
393
result = executeCall(procedureName,
394                            parameterList,
395                            parameterList);
396
397       return result;
398    }
399
400
401
402    //--------------------------JDBC 2.0-----------------------------
403

404    /**
405     * JDBC 2.0
406     *
407     * Gets the value of a JDBC <code>NUMERIC</code> parameter as a
408     * <code>java.math.BigDecimal</code> object with as many digits to the
409     * right of the decimal point as the value contains.
410     * @param parameterIndex the first parameter is 1, the second is 2,
411     * and so on
412     * @return the parameter value in full precision. If the value is
413     * SQL NULL, the result is <code>null</code>.
414     * @exception SQLException if a database access error occurs
415     */

416    public BigDecimal JavaDoc getBigDecimal(int parameterIndex) throws SQLException
417       {
418          NotImplemented();
419          return null;
420       }
421
422
423    
424    /**
425     * Gets the value of a JDBC <code>DATE</code> parameter as a
426     * <code>java.sql.Date</code> object, using
427     * the given <code>Calendar</code> object
428     * to construct the date.
429     * With a <code>Calendar</code> object, the driver
430     * can calculate the date taking into account a custom timezone and locale.
431     * If no <code>Calendar</code> object is specified, the driver uses the
432     * default timezone and locale.
433     *
434     * @param parameterIndex the first parameter is 1, the second is 2,
435     * and so on
436     * @param cal the <code>Calendar</code> object the driver will use
437     * to construct the date
438     * @return the parameter value. If the value is SQL NULL, the result is
439     * <code>null</code>.
440     * @exception SQLException if a database access error occurs
441     */

442    public java.sql.Date JavaDoc getDate(int parameterIndex, Calendar JavaDoc cal)
443       throws SQLException
444       {
445          NotImplemented();
446          return null;
447       }
448    
449    
450    /**
451     * Gets the value of a JDBC <code>TIME</code> parameter as a
452     * <code>java.sql.Time</code> object, using
453     * the given <code>Calendar</code> object
454     * to construct the time.
455     * With a <code>Calendar</code> object, the driver
456     * can calculate the time taking into account a custom timezone and locale.
457     * If no <code>Calendar</code> object is specified, the driver uses the
458     * default timezone and locale.
459     *
460     * @param parameterIndex the first parameter is 1, the second is 2,
461     * and so on
462     * @param cal the <code>Calendar</code> object the driver will use
463     * to construct the time
464     * @return the parameter value; if the value is SQL NULL, the result is
465     * <code>null</code>.
466     * @exception SQLException if a database access error occurs
467     */

468    public java.sql.Time JavaDoc getTime(int parameterIndex, Calendar JavaDoc cal)
469       throws SQLException
470    {
471       NotImplemented();
472       return null;
473    }
474    
475    
476    /**
477     * Gets the value of a JDBC <code>TIMESTAMP</code> parameter as a
478     * <code>java.sql.Timestamp</code> object, using
479     * the given <code>Calendar</code> object to construct
480     * the <code>Timestamp</code> object.
481     * With a <code>Calendar</code> object, the driver
482     * can calculate the timestamp taking into account a custom timezone and locale.
483     * If no <code>Calendar</code> object is specified, the driver uses the
484     * default timezone and locale.
485     *
486     *
487     * @param parameterIndex the first parameter is 1, the second is 2,
488     * and so on
489     * @param cal the <code>Calendar</code> object the driver will use
490     * to construct the timestamp
491     * @return the parameter value. If the value is SQL NULL, the result is
492     * <code>null</code>.
493     * @exception SQLException if a database access error occurs
494     */

495    public java.sql.Timestamp JavaDoc getTimestamp(int parameterIndex, Calendar JavaDoc cal)
496       throws SQLException
497    {
498       NotImplemented();
499       return null;
500    }
501    
502
503
504    /**
505     * JDBC 2.0
506     *
507     * Registers the designated output parameter. This version of
508     * the method <code>registerOutParameter</code>
509     * should be used for a user-named or REF output parameter. Examples
510     * of user-named types include: STRUCT, DISTINCT, JAVA_OBJECT, and
511     * named array types.
512     *
513     * Before executing a stored procedure call, you must explicitly
514     * call <code>registerOutParameter</code> to register the type from
515     * <code>java.sql.Types</code> for each
516     * OUT parameter. For a user-named parameter the fully-qualified SQL
517     * type name of the parameter should also be given, while a REF
518     * parameter requires that the fully-qualified type name of the
519     * referenced type be given. A JDBC driver that does not need the
520     * type code and type name information may ignore it. To be portable,
521     * however, applications should always provide these values for
522     * user-named and REF parameters.
523     *
524     * Although it is intended for user-named and REF parameters,
525     * this method may be used to register a parameter of any JDBC type.
526     * If the parameter does not have a user-named or REF type, the
527     * typeName parameter is ignored.
528     *
529     * <P><B>Note:</B> When reading the value of an out parameter, you
530     * must use the <code>getXXX</code> method whose Java type XXX corresponds to the
531     * parameter's registered SQL type.
532     *
533     * @param parameterIndex the first parameter is 1, the second is 2,...
534     * @param sqlType a value from {@link java.sql.Types}
535     * @param typeName the fully-qualified name of an SQL structured type
536     * @exception SQLException if a database-access error occurs
537     * @see Types
538     */

539    public void registerOutParameter (int paramIndex, int sqlType, String JavaDoc typeName)
540       throws SQLException
541    {
542       NotImplemented();
543    }
544    
545
546
547
548    static public void main(String JavaDoc args[])
549       throws java.lang.ClassNotFoundException JavaDoc,
550       java.lang.IllegalAccessException JavaDoc,
551       java.lang.InstantiationException JavaDoc
552    {
553       try
554       {
555          String JavaDoc url = url = ""
556             + "jdbc:freetds:"
557             + "//"
558             + "kap"
559             + "/"
560             + "pubs";
561
562          Class.forName("com.internetcds.jdbc.tds.Driver").newInstance();
563          java.sql.Connection JavaDoc connection;
564          connection = DriverManager.getConnection(url,
565                                                    "testuser",
566                                                    "password");
567
568          java.sql.CallableStatement JavaDoc call = connection.prepareCall(
569             "sp_tables ?");
570          call.setString(1, "%");
571          java.sql.ResultSet JavaDoc rs = call.executeQuery();
572
573          while(rs.next())
574          {
575             String JavaDoc qualifier = rs.getString("TABLE_QUALIFIER");
576             String JavaDoc owner = rs.getString("TABLE_OWNER");
577             String JavaDoc name = rs.getString("TABLE_NAME");
578             String JavaDoc type = rs.getString("TABLE_TYPE");
579             String JavaDoc remarks = rs.getString("REMARKS");
580
581             System.out.println("qualifier: " + qualifier);
582             System.out.println("owner: " + owner);
583             System.out.println("name: " + name);
584             System.out.println("type: " + type);
585             System.out.println("remarks: " + remarks);
586             System.out.println("");
587          }
588       }
589       catch(SQLException e)
590       {
591          e.printStackTrace();
592          System.out.println(e.getMessage());
593       }
594    }
595 }
596
Popular Tags