< prev index next >

src/java.sql/share/classes/java/sql/DriverManager.java

Print this page

 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.sql;
 27 
 28 import java.util.ArrayList;
 29 import java.util.Collections;
 30 import java.util.Enumeration;
 31 import java.util.Iterator;
 32 import java.util.List;
 33 import java.util.ServiceLoader;
 34 import java.security.AccessController;
 35 import java.security.PrivilegedAction;
 36 import java.util.concurrent.CopyOnWriteArrayList;
 37 import java.util.stream.Stream;
 38 
 39 import jdk.internal.reflect.CallerSensitive;

 40 import jdk.internal.reflect.Reflection;
 41 
 42 
 43 /**
 44  * The basic service for managing a set of JDBC drivers.
 45  * <p>
 46  * <strong>NOTE:</strong> The {@link javax.sql.DataSource} interface, provides
 47  * another way to connect to a data source.
 48  * The use of a {@code DataSource} object is the preferred means of
 49  * connecting to a data source.
 50  * <P>
 51  * As part of its initialization, the {@code DriverManager} class will
 52  * attempt to load available JDBC drivers by using:
 53  * <ul>
 54  * <li>The {@code jdbc.drivers} system property which contains a
 55  * colon separated list of fully qualified class names of JDBC drivers. Each
 56  * driver is loaded using the {@linkplain ClassLoader#getSystemClassLoader
 57  * system class loader}:
 58  * <ul>
 59  * <li>{@code jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver}

630                 String[] driversList = drivers.split(":");
631                 println("number of Drivers:" + driversList.length);
632                 for (String aDriver : driversList) {
633                     try {
634                         println("DriverManager.Initialize: loading " + aDriver);
635                         Class.forName(aDriver, true,
636                                 ClassLoader.getSystemClassLoader());
637                     } catch (Exception ex) {
638                         println("DriverManager.Initialize: load failed: " + ex);
639                     }
640                 }
641             }
642 
643             driversInitialized = true;
644             println("JDBC DriverManager initialized");
645         }
646     }
647 
648 
649     //  Worker method called by the public getConnection() methods.

650     private static Connection getConnection(
651         String url, java.util.Properties info, Class<?> caller) throws SQLException {
652         /*
653          * When callerCl is null, we should check the application's
654          * (which is invoking this class indirectly)
655          * classloader, so that the JDBC driver class outside rt.jar
656          * can be loaded from here.
657          */
658         ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
659         if (callerCL == null || callerCL == ClassLoader.getPlatformClassLoader()) {
660             callerCL = Thread.currentThread().getContextClassLoader();
661         }
662 
663         if (url == null) {
664             throw new SQLException("The url cannot be null", "08001");
665         }
666 
667         println("DriverManager.getConnection(\"" + url + "\")");
668 
669         ensureDriversInitialized();

 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.sql;
 27 
 28 import java.util.ArrayList;
 29 import java.util.Collections;
 30 import java.util.Enumeration;
 31 import java.util.Iterator;
 32 import java.util.List;
 33 import java.util.ServiceLoader;
 34 import java.security.AccessController;
 35 import java.security.PrivilegedAction;
 36 import java.util.concurrent.CopyOnWriteArrayList;
 37 import java.util.stream.Stream;
 38 
 39 import jdk.internal.reflect.CallerSensitive;
 40 import jdk.internal.reflect.CallerSensitiveAdapter;
 41 import jdk.internal.reflect.Reflection;
 42 
 43 
 44 /**
 45  * The basic service for managing a set of JDBC drivers.
 46  * <p>
 47  * <strong>NOTE:</strong> The {@link javax.sql.DataSource} interface, provides
 48  * another way to connect to a data source.
 49  * The use of a {@code DataSource} object is the preferred means of
 50  * connecting to a data source.
 51  * <P>
 52  * As part of its initialization, the {@code DriverManager} class will
 53  * attempt to load available JDBC drivers by using:
 54  * <ul>
 55  * <li>The {@code jdbc.drivers} system property which contains a
 56  * colon separated list of fully qualified class names of JDBC drivers. Each
 57  * driver is loaded using the {@linkplain ClassLoader#getSystemClassLoader
 58  * system class loader}:
 59  * <ul>
 60  * <li>{@code jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver}

631                 String[] driversList = drivers.split(":");
632                 println("number of Drivers:" + driversList.length);
633                 for (String aDriver : driversList) {
634                     try {
635                         println("DriverManager.Initialize: loading " + aDriver);
636                         Class.forName(aDriver, true,
637                                 ClassLoader.getSystemClassLoader());
638                     } catch (Exception ex) {
639                         println("DriverManager.Initialize: load failed: " + ex);
640                     }
641                 }
642             }
643 
644             driversInitialized = true;
645             println("JDBC DriverManager initialized");
646         }
647     }
648 
649 
650     //  Worker method called by the public getConnection() methods.
651     @CallerSensitiveAdapter
652     private static Connection getConnection(
653         String url, java.util.Properties info, Class<?> caller) throws SQLException {
654         /*
655          * When callerCl is null, we should check the application's
656          * (which is invoking this class indirectly)
657          * classloader, so that the JDBC driver class outside rt.jar
658          * can be loaded from here.
659          */
660         ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
661         if (callerCL == null || callerCL == ClassLoader.getPlatformClassLoader()) {
662             callerCL = Thread.currentThread().getContextClassLoader();
663         }
664 
665         if (url == null) {
666             throw new SQLException("The url cannot be null", "08001");
667         }
668 
669         println("DriverManager.getConnection(\"" + url + "\")");
670 
671         ensureDriversInitialized();
< prev index next >