⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drivermanager.java

📁 一个JDBC数据库连接的组件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)DriverManager.java	1.12 98/04/24 * * Copyright 1996-1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */package java.sql;/** * <P>The DriverManager provides a basic service for managing a set of * JDBC drivers. * * <P>As part of its initialization, the DriverManager class will * attempt to load the driver classes referenced in the "jdbc.drivers" * system property. This allows a user to customize the JDBC Drivers * used by their applications. For example in your * ~/.hotjava/properties file you might specify: * <CODE>jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver</CODE> * * A program can also explicitly load JDBC drivers at any time. For * example, the my.sql.Driver is loaded with the following statement: * <CODE>Class.forName("my.sql.Driver");</CODE> * * <P>When getConnection is called the DriverManager will attempt to * locate a suitable driver from amongst those loaded at * initialization and those loaded explicitly using the same classloader * as the current applet or application. * * @see Driver * @see Connection  */public class DriverManager {    //--------------------------JDBC 2.0-----------------------------    /**     * JDBC 2.0     *     * Get the log writer.       *     * The getLogWriter() and setLogWriter() methods should be used instead     * of the old get/setlogStream() methods.     */    public static java.io.PrintWriter getLogWriter() {      return logWriter;    }    /**     * JDBC 2.0     *     * Set the logging/tracing Writer that is used by the DriverManager     * and all drivers.     *     * There is a minor versioning problem introduced by the introduction     * of setLogWriter().  SetLogWriter() can not create a PrintStream     * that will be returned by getLogStream()---the Java platform does     * not provide a backward conversion.  So, a new application     * that uses setLogWriter() and also uses a JDBC 1.0 driver that uses     * getLogStream() will likely not see debugging information written      * by that driver.     *     * @param out the new logging/tracing PrintStream; to disable, set to null     */    public static void setLogWriter(java.io.PrintWriter out) {      logStream = null;      logWriter = out;    }    //---------------------------------------------------------------    /**     * Attempt to establish a connection to the given database URL.     * The DriverManager attempts to select an appropriate driver from     * the set of registered JDBC drivers.     *     * @param url a database url of the form jdbc:<em>subprotocol</em>:<em>subname</em>     * @param info a list of arbitrary string tag/value pairs as     * connection arguments; normally at least a "user" and     * "password" property should be included     * @return a Connection to the URL      * @exception SQLException if a database-access error occurs.     */    public static synchronized Connection getConnection(String url,             java.util.Properties info) throws SQLException {	if(url == null) {	    throw new SQLException("The url cannot be null", "08001");	}        println("DriverManager.getConnection(\"" + url + "\")");        if (!initialized) {            initialize();        }        // Figure out the current security context.        Object currentSecurityContext = getSecurityContext();        // 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 driver isn't part of the base system and doesn't come            // from the same security context as the current caller, skip it.            if (di.securityContext != null &&                         di.securityContext != currentSecurityContext) {                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");    }    /**     * Attempt to establish a connection to the given database URL.     * The DriverManager attempts to select an appropriate driver from     * the set of registered JDBC drivers.     *     * @param url a database url of the form jdbc:<em>subprotocol</em>:<em>subname</em>     * @param user the database user on whose behalf the Connection is being made     * @param password the user's password     * @return a Connection to the URL      * @exception SQLException if a database-access error occurs.     */    public static synchronized Connection getConnection(String url, 		            String user, String password) throws SQLException {        java.util.Properties info = new java.util.Properties();	if (user != null) {	    info.put("user", user);	}	if (password != null) {	    info.put("password", password);	}        return (getConnection(url, info));    }    /**     * Attempt to establish a connection to the given database URL.     * The DriverManager attempts to select an appropriate driver from     * the set of registered JDBC drivers.     *     * @param url a database url of the form jdbc:<em>subprotocol</em>:<em>subname</em>     * @return a Connection to the URL      * @exception SQLException if a database-access error occurs.     */    public static synchronized Connection getConnection(String url)                                     throws SQLException {        java.util.Properties info = new java.util.Properties();        return (getConnection(url, info));    }    /**     * Attempt to locate a driver that understands the given URL.     * The DriverManager attempts to select an appropriate driver from     * the set of registered JDBC drivers.      *     * @param url a database url of the form jdbc:<em>subprotocol</em>:<em>subname</em>     * @return a Driver that can connect to the URL      * @exception SQLException if a database-access error occurs.     */    public static Driver getDriver(String url) throws SQLException {        println("DriverManager.getDriver(\"" + url + "\")");        if (!initialized) {            initialize();        }        // Figure out the current security context.        Object currentSecurityContext = getSecurityContext();        // Walk through the loaded drivers attempting to locate someone	// who understands the given URL.        for (int i = 0; i < drivers.size(); i++) {            DriverInfo di = (DriverInfo)drivers.elementAt(i);            // If the driver isn't part of the base system and doesn't come            // from the same security context as the current caller, skip it.            if (di.securityContext != null &&                         di.securityContext != currentSecurityContext) {                println("    skipping: " + di);                continue;            }            try {                println("    trying " + di);		if (di.driver.acceptsURL(url)) {		    // Success!                    println("getDriver returning " + di);                    return (di.driver);                }            } catch (SQLException ex) {		// Drop through and try the next driver.            }        }        println("getDriver: no suitable driver");        throw new SQLException("No suitable driver", "08001");    }    /**     * A newly loaded driver class should call registerDriver to make itself     * known to the DriverManager.     *     * @param driver the new JDBC Driver      * @exception SQLException if a database-access error occurs.     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -