1 16 package org.outerj.daisy.jdbcutil; 17 18 import java.util.ArrayList ; 19 import java.util.StringTokenizer ; 20 import java.util.Properties ; 21 import java.net.URL ; 22 import java.net.MalformedURLException ; 23 import java.net.URLClassLoader ; 24 import java.io.File ; 25 import java.sql.*; 26 27 public class DriverLoader { 28 34 public static void loadDatabaseDriver(String classpath, String driverClass) throws Exception { 35 ArrayList urls = new ArrayList (); 36 StringTokenizer tokenizer = new StringTokenizer (classpath, ","); 37 while (tokenizer.hasMoreTokens()) { 38 String filename = tokenizer.nextToken().trim(); 39 try 40 { 41 URL url = new File (filename).toURL(); 42 urls.add(url); 43 } 44 catch (MalformedURLException e) 45 { 46 throw new Exception ("Invalid filename in driver path.", e); 47 } 48 } 49 ClassLoader classLoader = new URLClassLoader ((URL [])urls.toArray(new URL []{}), Thread.currentThread().getContextClassLoader()); 50 Driver driver = (Driver) Class.forName(driverClass, true, classLoader).newInstance(); 51 DriverManager.registerDriver(new DriverShim(driver)); 52 } 53 54 61 private static final class DriverShim implements Driver 62 { 63 private Driver driver; 64 65 DriverShim(Driver d) 66 { 67 this.driver = d; 68 } 69 70 public final boolean acceptsURL(final String u) throws SQLException 71 { 72 return this.driver.acceptsURL(u); 73 } 74 75 public final Connection connect(final String u, final Properties p) throws SQLException 76 { 77 return this.driver.connect(u, p); 78 } 79 80 public final int getMajorVersion() 81 { 82 return this.driver.getMajorVersion(); 83 } 84 85 public final int getMinorVersion() 86 { 87 return this.driver.getMinorVersion(); 88 } 89 90 public final DriverPropertyInfo[] getPropertyInfo(final String u, final Properties p) throws SQLException 91 { 92 return this.driver.getPropertyInfo(u, p); 93 } 94 95 public final boolean jdbcCompliant() 96 { 97 return this.driver.jdbcCompliant(); 98 } 99 } 100 } 101 | Popular Tags |