📄 drivermanager.java
字号:
for (i = 0; i < drivers.size(); i++) { di = (DriverInfo)drivers.elementAt(i); if (di.driver == driver) { break; } } // If we can't find the driver just return. if (i >= drivers.size()) { println(" couldn't find driver to unload"); return; } // If the caller does not have permission to load the driver then // throw a security exception. if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { throw new SecurityException(); } // Remove the driver. Other entries in drivers get shuffled down. drivers.removeElementAt(i); } /** * Retrieves an Enumeration with all of the currently loaded JDBC drivers * to which the current caller has access. * * <P><B>Note:</B> The classname of a driver can be found using * <CODE>d.getClass().getName()</CODE> * * @return the list of JDBC Drivers loaded by the caller's class loader */ public static synchronized java.util.Enumeration getDrivers() { java.util.Vector result = new java.util.Vector(); if (!initialized) { initialize(); } // Gets the classloader of the code that called this method, may // be null. ClassLoader callerCL = DriverManager.getCallerClassLoader(); // Walk through the loaded drivers. for (int i = 0; i < drivers.size(); i++) { DriverInfo di = (DriverInfo)drivers.elementAt(i); // If the caller does not have permission to load the driver then // skip it. if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { println(" skipping: " + di); continue; } result.addElement(di.driver); } return (result.elements()); } /** * Sets the maximum time in seconds that a driver will wait * while attempting to connect to a database. * * @param seconds the login time limit in seconds * @see #getLoginTimeout */ public static void setLoginTimeout(int seconds) { loginTimeout = seconds; } /** * Gets the maximum time in seconds that a driver can wait * when attempting to log in to a database. * * @return the driver login time limit in seconds * @see #setLoginTimeout */ public static int getLoginTimeout() { return (loginTimeout); } /** * Sets the logging/tracing PrintStream that is used * by the <code>DriverManager</code> * and all drivers. *<P> * In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks * to see that there is an <code>SQLPermission</code> object before setting * the logging stream. If a <code>SecurityManager</code> exists and its * <code>checkPermission</code> method denies setting the log writer, this * method throws a <code>java.lang.SecurityException</code>. * * @param out the new logging/tracing PrintStream; to disable, set to <code>null</code> * @deprecated * @throws SecurityException if a security manager exists and its * <code>checkPermission</code> method denies setting the log stream * * @see SecurityManager#checkPermission * @see #getLogStream */ public static synchronized void setLogStream(java.io.PrintStream out) { SecurityManager sec = System.getSecurityManager(); if (sec != null) { sec.checkPermission(SET_LOG_PERMISSION); } logStream = out; if ( out != null ) logWriter = new java.io.PrintWriter(out); else logWriter = null; } /** * Retrieves the logging/tracing PrintStream that is used by the <code>DriverManager</code> * and all drivers. * * @return the logging/tracing PrintStream; if disabled, is <code>null</code> * @deprecated * @see #setLogStream */ public static java.io.PrintStream getLogStream() { return logStream; } /** * Prints a message to the current JDBC log stream. * * @param message a log or tracing message */ public static void println(String message) { synchronized (logSync) { if (logWriter != null) { logWriter.println(message); // automatic flushing is never enabled, so we must do it ourselves logWriter.flush(); } } } //------------------------------------------------------------------------ // Returns the class object that would be created if the code calling the // driver manager had loaded the driver class, or null if the class // is inaccessible. private static Class getCallerClass(ClassLoader callerClassLoader, String driverClassName) { Class callerC = null; try { callerC = Class.forName(driverClassName, true, callerClassLoader); } catch (Exception ex) { callerC = null; // being very careful } return callerC; } private static void loadInitialDrivers() { String drivers; try { drivers = (String) java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction("jdbc.drivers")); } catch (Exception ex) { drivers = null; } println("DriverManager.initialize: jdbc.drivers = " + drivers); if (drivers == null) { return; } while (drivers.length() != 0) { int x = drivers.indexOf(':'); String driver; if (x < 0) { driver = drivers; drivers = ""; } else { driver = drivers.substring(0, x); drivers = drivers.substring(x+1); } if (driver.length() == 0) { continue; } try { println("DriverManager.Initialize: loading " + driver); Class.forName(driver, true, ClassLoader.getSystemClassLoader()); } catch (Exception ex) { println("DriverManager.Initialize: load failed: " + ex); } } } // Worker method called by the public getConnection() methods. private static synchronized Connection getConnection( String url, java.util.Properties info, ClassLoader callerCL) throws SQLException { if(url == null) { throw new SQLException("The url cannot be null", "08001"); } println("DriverManager.getConnection(\"" + url + "\")"); if (!initialized) { initialize(); } // Walk through the loaded drivers attempting to make a connection. // Remember the first exception that gets raised so we can reraise it. SQLException reason = null; for (int i = 0; i < drivers.size(); i++) { DriverInfo di = (DriverInfo)drivers.elementAt(i); // If the caller does not have permission to load the driver then // skip it. if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) { println(" skipping: " + di); continue; } try { println(" trying " + di); Connection result = di.driver.connect(url, info); if (result != null) { // Success! println("getConnection returning " + di); return (result); } } catch (SQLException ex) { if (reason == null) { reason = ex; } } } // if we got here nobody could connect. if (reason != null) { println("getConnection failed: " + reason); throw reason; } println("getConnection: no suitable driver"); throw new SQLException("No suitable driver", "08001"); } // Class initialization. static void initialize() { if (initialized) { return; } initialized = true; loadInitialDrivers(); println("JDBC DriverManager initialized"); } // Prevent the DriverManager class from being instantiated. private DriverManager(){} private static java.util.Vector drivers = new java.util.Vector(); private static int loginTimeout = 0; private static java.io.PrintWriter logWriter = null; private static java.io.PrintStream logStream = null; private static boolean initialized = false; private static Object logSync = new Object(); // Returns the caller's class loader, or null if none private static native ClassLoader getCallerClassLoader();}// DriverInfo is a package-private support class.class DriverInfo { Driver driver; Class driverClass; String driverClassName; public String toString() { return ("driver[className=" + driverClassName + "," + driver + "]"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -