View Javadoc
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 cvsVersion = "$Id: Constructors.java,v 1.1 2003/04/29 18:07:50 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 jdbcVersionName = null; 49 50 private static java.lang.reflect.Constructor resultSetCtor = null; 51 private static java.lang.reflect.Constructor preparedStatementCtor = null; 52 private static java.lang.reflect.Constructor callableStatementCtor = null; 53 private static java.lang.reflect.Constructor connectionCtor = null; 54 55 56 static private java.lang.reflect.Constructor getCtor( 57 String classNamePrefix, 58 Class paramTypes[]) 59 throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException 60 { 61 java.lang.reflect.Constructor result = null; 62 String className = null; 63 Class 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 76 { 77 try 78 { 79 Class 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 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 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 connectionParamTypes[] = 98 { 99 java.lang.Class.forName("java.util.Properties") 100 }; 101 102 103 try 104 { 105 Class statement = java.lang.Class.forName("java.sql.Statement"); 106 java.lang.reflect.Method execBatch = 107 statement.getDeclaredMethod("executeBatch", new Class[0]); 108 109 jdbcVersion = JDBC2_0; 110 jdbcVersionName = "2_0"; 111 } 112 catch (NoSuchMethodException 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 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 e) 151 { 152 System.err.println("Couldn't find the class"); // XXX remove println 153 throw new java.sql.SQLException(e.getMessage()); 154 } 155 catch(java.lang.NoSuchMethodException e) 156 { 157 System.err.println("Couldn't find a constructor"); 158 throw new java.sql.SQLException(e.getMessage()); 159 } 160 161 dejavu = true; 162 } /* init() */ 163 164 165 166 public static java.sql.ResultSet newResultSet( 167 Tds tds_, 168 java.sql.Statement stmt_, 169 Columns columns_) throws java.sql.SQLException 170 { 171 if (!dejavu) 172 { 173 init(); 174 } 175 176 java.sql.ResultSet result = null; 177 try 178 { 179 Object params[] = {tds_, stmt_, columns_}; 180 181 result = (java.sql.ResultSet)resultSetCtor.newInstance(params); 182 } 183 catch (java.lang.reflect.InvocationTargetException e) 184 { 185 throw new java.sql.SQLException(e.getTargetException().getMessage()); 186 } 187 catch (Throwable e) 188 { 189 e.printStackTrace(); 190 throw new java.sql.SQLException(e.getMessage()); 191 } 192 return result; 193 } 194 195 public static java.sql.CallableStatement newCallableStatement( 196 Object cx_, 197 com.internetcds.jdbc.tds.Tds tds_, 198 java.lang.String sql_) throws java.sql.SQLException 199 { 200 if (!dejavu) 201 { 202 init(); 203 } 204 205 try 206 { 207 Object params[] = {cx_, tds_, sql_}; 208 209 return (java.sql.CallableStatement)callableStatementCtor.newInstance(params); 210 } 211 catch (java.lang.reflect.InvocationTargetException e) 212 { 213 throw new java.sql.SQLException(e.getTargetException().getMessage()); 214 } 215 catch (Throwable e) 216 { 217 throw new java.sql.SQLException(e.getMessage()); 218 } 219 } 220 221 222 public static java.sql.PreparedStatement newPreparedStatement( 223 Object cx_, 224 com.internetcds.jdbc.tds.Tds tds_, 225 java.lang.String sql_) throws java.sql.SQLException 226 { 227 if (!dejavu) 228 { 229 init(); 230 } 231 232 try 233 { 234 Object params[] = {cx_, tds_, sql_}; 235 236 return (java.sql.PreparedStatement)preparedStatementCtor.newInstance(params); 237 } 238 catch (java.lang.reflect.InvocationTargetException e) 239 { 240 throw new java.sql.SQLException(e.getTargetException().getMessage()); 241 } 242 catch (Throwable e) 243 { 244 throw new java.sql.SQLException(e.getMessage()); 245 } 246 } 247 248 249 public static java.sql.Connection newConnection( 250 java.util.Properties props_) 251 throws java.sql.SQLException, com.internetcds.jdbc.tds.TdsException 252 { 253 if (!dejavu) 254 { 255 init(); 256 } 257 258 try 259 { 260 Object params[] = 261 { 262 props_, 263 }; 264 265 return (java.sql.Connection)connectionCtor.newInstance(params); 266 } 267 catch (java.lang.reflect.InvocationTargetException e) 268 { 269 throw new java.sql.SQLException(e.getTargetException().getMessage()); 270 } 271 catch (Throwable e) 272 { 273 throw new java.sql.SQLException(e.getMessage()); 274 } 275 } /* newConnection() */ 276 277 } 278

This page was automatically generated by Maven