KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// Copyright 1999 Craig Spannring
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 Craig Spannring
16
// 4. The name of Craig Spannring 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 CRAIG SPANNRING ``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 CRAIG SPANNRING 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
37
38 public class Constructors
39 {
40    public static final String JavaDoc cvsVersion = "$Id: Constructors.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $";
41
42    static boolean dejavu = false;
43
44    static final int JDBC2_0 = 2;
45    static final int JDBC1_0 = 1;
46
47    private static int jdbcVersion = JDBC1_0;
48    private static String JavaDoc jdbcVersionName = null;
49
50    private static java.lang.reflect.Constructor JavaDoc resultSetCtor = null;
51    private static java.lang.reflect.Constructor JavaDoc preparedStatementCtor = null;
52    private static java.lang.reflect.Constructor JavaDoc callableStatementCtor = null;
53    private static java.lang.reflect.Constructor JavaDoc connectionCtor = null;
54
55
56    static private java.lang.reflect.Constructor JavaDoc getCtor(
57       String JavaDoc classNamePrefix,
58       Class JavaDoc paramTypes[])
59       throws java.lang.ClassNotFoundException JavaDoc, java.lang.NoSuchMethodException JavaDoc
60    {
61       java.lang.reflect.Constructor JavaDoc result = null;
62       String JavaDoc className = null;
63       Class JavaDoc theClass = null;
64
65       className = (classNamePrefix + "_" + jdbcVersionName);
66       theClass = java.lang.Class.forName(className);
67
68       result = theClass.getConstructor(paramTypes);
69       
70       return result;
71    } /* getCtor() */
72
73
74    static private void init()
75       throws java.sql.SQLException JavaDoc
76    {
77       try
78       {
79          Class JavaDoc resultSetParamTypes[] =
80          {
81             java.lang.Class.forName("com.internetcds.jdbc.tds.Tds"),
82             java.lang.Class.forName("com.internetcds.jdbc.tds.Statement"),
83             java.lang.Class.forName("com.internetcds.jdbc.tds.Columns")
84          };
85          Class JavaDoc preparedStatementParamTypes[] =
86          {
87             java.lang.Class.forName("java.sql.Connection"),
88             java.lang.Class.forName("com.internetcds.jdbc.tds.Tds"),
89             java.lang.Class.forName("java.lang.String")
90          };
91          Class JavaDoc callableStatementParamTypes[] =
92          {
93             java.lang.Class.forName("java.sql.Connection"),
94             java.lang.Class.forName("com.internetcds.jdbc.tds.Tds"),
95             java.lang.Class.forName("java.lang.String")
96          };
97          Class JavaDoc connectionParamTypes[] =
98          {
99             java.lang.Class.forName("java.util.Properties")
100          };
101          
102          
103          try
104          {
105             Class JavaDoc statement = java.lang.Class.forName("java.sql.Statement");
106             java.lang.reflect.Method JavaDoc execBatch =
107                 statement.getDeclaredMethod("executeBatch", new Class JavaDoc[0]);
108
109             jdbcVersion = JDBC2_0;
110             jdbcVersionName = "2_0";
111          }
112          catch (NoSuchMethodException JavaDoc nsme)
113          {
114             jdbcVersion = JDBC1_0;
115             jdbcVersionName = "1_0";
116          }
117
118          try
119          {
120             resultSetCtor = getCtor("com.internetcds.jdbc.tds.ResultSet",
121                                             resultSetParamTypes);
122             preparedStatementCtor = getCtor("com.internetcds.jdbc.tds.PreparedStatement",
123                                             preparedStatementParamTypes);
124             callableStatementCtor = getCtor("com.internetcds.jdbc.tds.CallableStatement",
125                                             callableStatementParamTypes);
126             connectionCtor = getCtor("com.internetcds.jdbc.tds.Connection",
127                                             connectionParamTypes);
128          }
129          catch(java.lang.ClassNotFoundException JavaDoc e)
130          {
131             if (jdbcVersion == JDBC2_0)
132             {
133                //
134
// If we couldn't find the 2.0 classes, let's try to fall back
135
// to JDBC 1.0
136
//
137
jdbcVersion = JDBC1_0;
138                jdbcVersionName = "1_0";
139                resultSetCtor = getCtor("com.internetcds.jdbc.tds.ResultSet",
140                                                resultSetParamTypes);
141                preparedStatementCtor = getCtor("com.internetcds.jdbc.tds.PreparedStatement",
142                                                preparedStatementParamTypes);
143                callableStatementCtor = getCtor("com.internetcds.jdbc.tds.CallableStatement",
144                                                callableStatementParamTypes);
145                connectionCtor = getCtor("com.internetcds.jdbc.tds.Connection",
146                                                connectionParamTypes);
147             }
148          }
149       }
150       catch(java.lang.ClassNotFoundException JavaDoc e)
151       {
152          System.err.println("Couldn't find the class"); // XXX remove println
153
throw new java.sql.SQLException JavaDoc(e.getMessage());
154       }
155       catch(java.lang.NoSuchMethodException JavaDoc e)
156       {
157          System.err.println("Couldn't find a constructor");
158          throw new java.sql.SQLException JavaDoc(e.getMessage());
159       }
160
161       dejavu = true;
162    } /* init() */
163
164
165
166    public static java.sql.ResultSet JavaDoc newResultSet(
167       Tds tds_,
168       java.sql.Statement JavaDoc stmt_,
169       Columns columns_) throws java.sql.SQLException JavaDoc
170    {
171       if (!dejavu)
172       {
173          init();
174       }
175
176       java.sql.ResultSet JavaDoc result = null;
177       try
178       {
179          Object JavaDoc params[] = {tds_, stmt_, columns_};
180
181          result = (java.sql.ResultSet JavaDoc)resultSetCtor.newInstance(params);
182       }
183       catch (java.lang.reflect.InvocationTargetException JavaDoc e)
184       {
185          throw new java.sql.SQLException JavaDoc(e.getTargetException().getMessage());
186       }
187       catch (Throwable JavaDoc e)
188       {
189          e.printStackTrace();
190          throw new java.sql.SQLException JavaDoc(e.getMessage());
191       }
192       return result;
193    }
194
195    public static java.sql.CallableStatement JavaDoc newCallableStatement(
196       Object JavaDoc cx_,
197       com.internetcds.jdbc.tds.Tds tds_,
198       java.lang.String JavaDoc sql_) throws java.sql.SQLException JavaDoc
199    {
200       if (!dejavu)
201       {
202          init();
203       }
204
205       try
206       {
207          Object JavaDoc params[] = {cx_, tds_, sql_};
208
209          return (java.sql.CallableStatement JavaDoc)callableStatementCtor.newInstance(params);
210       }
211       catch (java.lang.reflect.InvocationTargetException JavaDoc e)
212       {
213          throw new java.sql.SQLException JavaDoc(e.getTargetException().getMessage());
214       }
215       catch (Throwable JavaDoc e)
216       {
217          throw new java.sql.SQLException JavaDoc(e.getMessage());
218       }
219    }
220
221
222    public static java.sql.PreparedStatement JavaDoc newPreparedStatement(
223       Object JavaDoc cx_,
224       com.internetcds.jdbc.tds.Tds tds_,
225       java.lang.String JavaDoc sql_) throws java.sql.SQLException JavaDoc
226    {
227       if (!dejavu)
228       {
229          init();
230       }
231
232       try
233       {
234          Object JavaDoc params[] = {cx_, tds_, sql_};
235
236          return (java.sql.PreparedStatement JavaDoc)preparedStatementCtor.newInstance(params);
237       }
238       catch (java.lang.reflect.InvocationTargetException JavaDoc e)
239       {
240          throw new java.sql.SQLException JavaDoc(e.getTargetException().getMessage());
241       }
242       catch (Throwable JavaDoc e)
243       {
244          throw new java.sql.SQLException JavaDoc(e.getMessage());
245       }
246    }
247
248
249     public static java.sql.Connection JavaDoc newConnection(
250        java.util.Properties JavaDoc props_)
251         throws java.sql.SQLException JavaDoc, com.internetcds.jdbc.tds.TdsException
252    {
253       if (!dejavu)
254       {
255          init();
256       }
257
258       try
259       {
260          Object JavaDoc params[] =
261          {
262             props_,
263          };
264
265          return (java.sql.Connection JavaDoc)connectionCtor.newInstance(params);
266       }
267       catch (java.lang.reflect.InvocationTargetException JavaDoc e)
268       {
269          throw new java.sql.SQLException JavaDoc(e.getTargetException().getMessage());
270       }
271       catch (Throwable JavaDoc e)
272       {
273          throw new java.sql.SQLException JavaDoc(e.getMessage());
274       }
275    } /* newConnection() */
276
277 }
278
279
Popular Tags