java.sql
Class DriverManager

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.sql.DriverManager

public class DriverManager
extends Object sample code for java.lang.Object definition code for java.lang.Object

The basic service for managing a set of JDBC drivers.
NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to connect to a data source. The use of a DataSource object is the preferred means of connecting to a data source.

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:

 jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver
 
A program can also explicitly load JDBC drivers at any time. For example, the my.sql.Driver is loaded with the following statement:
 Class.forName("my.sql.Driver");
 

When the method 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.

Starting with the Java 2 SDK, Standard Edition, version 1.3, a logging stream can be set only if the proper permission has been granted. Normally this will be done with the tool PolicyTool, which can be used to grant permission java.sql.SQLPermission "setLog".

See Also:
Driver sample code for java.sql.Driver definition code for java.sql.Driver , Connection sample code for java.sql.Connection definition code for java.sql.Connection

Method Summary
static void deregisterDriver sample code for java.sql.DriverManager.deregisterDriver(java.sql.Driver) definition code for java.sql.DriverManager.deregisterDriver(java.sql.Driver) (Driver sample code for java.sql.Driver definition code for java.sql.Driver  driver)
          Drops a driver from the DriverManager's list.
static Connection sample code for java.sql.Connection definition code for java.sql.Connection getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String) definition code for java.sql.DriverManager.getConnection(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  url)
          Attempts to establish a connection to the given database URL.
static Connection sample code for java.sql.Connection definition code for java.sql.Connection getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String, java.util.Properties) definition code for java.sql.DriverManager.getConnection(java.lang.String, java.util.Properties) (String sample code for java.lang.String definition code for java.lang.String  url, Properties sample code for java.util.Properties definition code for java.util.Properties  info)
          Attempts to establish a connection to the given database URL.
static Connection sample code for java.sql.Connection definition code for java.sql.Connection getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String) definition code for java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  url, String sample code for java.lang.String definition code for java.lang.String  user, String sample code for java.lang.String definition code for java.lang.String  password)
          Attempts to establish a connection to the given database URL.
static Driver sample code for java.sql.Driver definition code for java.sql.Driver getDriver sample code for java.sql.DriverManager.getDriver(java.lang.String) definition code for java.sql.DriverManager.getDriver(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  url)
          Attempts to locate a driver that understands the given URL.
static Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <Driver sample code for java.sql.Driver definition code for java.sql.Driver > getDrivers sample code for java.sql.DriverManager.getDrivers() definition code for java.sql.DriverManager.getDrivers() ()
          Retrieves an Enumeration with all of the currently loaded JDBC drivers to which the current caller has access.
static int getLoginTimeout sample code for java.sql.DriverManager.getLoginTimeout() definition code for java.sql.DriverManager.getLoginTimeout() ()
          Gets the maximum time in seconds that a driver can wait when attempting to log in to a database.
static PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream getLogStream sample code for java.sql.DriverManager.getLogStream() definition code for java.sql.DriverManager.getLogStream() ()
          Deprecated.  
static PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter getLogWriter sample code for java.sql.DriverManager.getLogWriter() definition code for java.sql.DriverManager.getLogWriter() ()
          Retrieves the log writer.
static void println sample code for java.sql.DriverManager.println(java.lang.String) definition code for java.sql.DriverManager.println(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  message)
          Prints a message to the current JDBC log stream.
static void registerDriver sample code for java.sql.DriverManager.registerDriver(java.sql.Driver) definition code for java.sql.DriverManager.registerDriver(java.sql.Driver) (Driver sample code for java.sql.Driver definition code for java.sql.Driver  driver)
          Registers the given driver with the DriverManager.
static void setLoginTimeout sample code for java.sql.DriverManager.setLoginTimeout(int) definition code for java.sql.DriverManager.setLoginTimeout(int) (int seconds)
          Sets the maximum time in seconds that a driver will wait while attempting to connect to a database.
static void setLogStream sample code for java.sql.DriverManager.setLogStream(java.io.PrintStream) definition code for java.sql.DriverManager.setLogStream(java.io.PrintStream) (PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream  out)
          Deprecated.  
static void setLogWriter sample code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter) definition code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter) (PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter  out)
          Sets the logging/tracing PrintWriter object that is used by the DriverManager and all drivers.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Method Detail

getLogWriter sample code for java.sql.DriverManager.getLogWriter() definition code for java.sql.DriverManager.getLogWriter()

public static PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter  getLogWriter()
Retrieves the log writer. The getLogWriter and setLogWriter methods should be used instead of the get/setlogStream methods, which are deprecated.

Returns:
a java.io.PrintWriter object
Since:
1.2
See Also:
setLogWriter(java.io.PrintWriter) sample code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter) definition code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter)

setLogWriter sample code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter) definition code for java.sql.DriverManager.setLogWriter(java.io.PrintWriter)

public static void setLogWriter(PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter  out)
Sets the logging/tracing PrintWriter object that is used by the DriverManager and all drivers.

There is a minor versioning problem created by the introduction of the method setLogWriter. The method setLogWriter cannot create a PrintStream object that will be returned by getLogStream---the Java platform does not provide a backward conversion. As a result, 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.

In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks to see that there is an SQLPermission object before setting the logging stream. If a SecurityManager exists and its checkPermission method denies setting the log writer, this method throws a java.lang.SecurityException.

Parameters:
out - the new logging/tracing PrintStream object; null to disable logging and tracing
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method denies setting the log writer
Since:
1.2
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , getLogWriter() sample code for java.sql.DriverManager.getLogWriter() definition code for java.sql.DriverManager.getLogWriter()

getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String, java.util.Properties) definition code for java.sql.DriverManager.getConnection(java.lang.String, java.util.Properties)

public static Connection sample code for java.sql.Connection definition code for java.sql.Connection  getConnection(String sample code for java.lang.String definition code for java.lang.String  url,
                                       Properties sample code for java.util.Properties definition code for java.util.Properties  info)
                                throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
info - a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and "password" property should be included
Returns:
a Connection to the URL
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String) definition code for java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String)

public static Connection sample code for java.sql.Connection definition code for java.sql.Connection  getConnection(String sample code for java.lang.String definition code for java.lang.String  url,
                                       String sample code for java.lang.String definition code for java.lang.String  user,
                                       String sample code for java.lang.String definition code for java.lang.String  password)
                                throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
user - the database user on whose behalf the connection is being made
password - the user's password
Returns:
a connection to the URL
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

getConnection sample code for java.sql.DriverManager.getConnection(java.lang.String) definition code for java.sql.DriverManager.getConnection(java.lang.String)

public static Connection sample code for java.sql.Connection definition code for java.sql.Connection  getConnection(String sample code for java.lang.String definition code for java.lang.String  url)
                                throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url - a database url of the form jdbc:subprotocol:subname
Returns:
a connection to the URL
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

getDriver sample code for java.sql.DriverManager.getDriver(java.lang.String) definition code for java.sql.DriverManager.getDriver(java.lang.String)

public static Driver sample code for java.sql.Driver definition code for java.sql.Driver  getDriver(String sample code for java.lang.String definition code for java.lang.String  url)
                        throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Attempts to locate a driver that understands the given URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url - a database URL of the form jdbc:subprotocol:subname
Returns:
a Driver object representing a driver that can connect to the given URL
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

registerDriver sample code for java.sql.DriverManager.registerDriver(java.sql.Driver) definition code for java.sql.DriverManager.registerDriver(java.sql.Driver)

public static void registerDriver(Driver sample code for java.sql.Driver definition code for java.sql.Driver  driver)
                           throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Registers the given driver with the DriverManager. A newly-loaded driver class should call the method registerDriver to make itself known to the DriverManager.

Parameters:
driver - the new JDBC Driver that is to be registered with the DriverManager
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

deregisterDriver sample code for java.sql.DriverManager.deregisterDriver(java.sql.Driver) definition code for java.sql.DriverManager.deregisterDriver(java.sql.Driver)

public static void deregisterDriver(Driver sample code for java.sql.Driver definition code for java.sql.Driver  driver)
                             throws SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException 
Drops a driver from the DriverManager's list. Applets can only deregister drivers from their own classloaders.

Parameters:
driver - the JDBC Driver to drop
Throws:
SQLException sample code for java.sql.SQLException definition code for java.sql.SQLException - if a database access error occurs

getDrivers sample code for java.sql.DriverManager.getDrivers() definition code for java.sql.DriverManager.getDrivers()

public static Enumeration sample code for java.util.Enumeration definition code for java.util.Enumeration <Driver sample code for java.sql.Driver definition code for java.sql.Driver > getDrivers()
Retrieves an Enumeration with all of the currently loaded JDBC drivers to which the current caller has access.

Note: The classname of a driver can be found using d.getClass().getName()

Returns:
the list of JDBC Drivers loaded by the caller's class loader

setLoginTimeout sample code for java.sql.DriverManager.setLoginTimeout(int) definition code for java.sql.DriverManager.setLoginTimeout(int)

public static void setLoginTimeout(int seconds)
Sets the maximum time in seconds that a driver will wait while attempting to connect to a database.

Parameters:
seconds - the login time limit in seconds
See Also:
getLoginTimeout() sample code for java.sql.DriverManager.getLoginTimeout() definition code for java.sql.DriverManager.getLoginTimeout()

getLoginTimeout sample code for java.sql.DriverManager.getLoginTimeout() definition code for java.sql.DriverManager.getLoginTimeout()

public static int getLoginTimeout()
Gets the maximum time in seconds that a driver can wait when attempting to log in to a database.

Returns:
the driver login time limit in seconds
See Also:
setLoginTimeout(int) sample code for java.sql.DriverManager.setLoginTimeout(int) definition code for java.sql.DriverManager.setLoginTimeout(int)

setLogStream sample code for java.sql.DriverManager.setLogStream(java.io.PrintStream) definition code for java.sql.DriverManager.setLogStream(java.io.PrintStream)

@Deprecated
public static void setLogStream(PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream  out)
Deprecated. 

Sets the logging/tracing PrintStream that is used by the DriverManager and all drivers.

In the Java 2 SDK, Standard Edition, version 1.3 release, this method checks to see that there is an SQLPermission object before setting the logging stream. If a SecurityManager exists and its checkPermission method denies setting the log writer, this method throws a java.lang.SecurityException.

Parameters:
out - the new logging/tracing PrintStream; to disable, set to null
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method denies setting the log stream
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , getLogStream() sample code for java.sql.DriverManager.getLogStream() definition code for java.sql.DriverManager.getLogStream()

getLogStream sample code for java.sql.DriverManager.getLogStream() definition code for java.sql.DriverManager.getLogStream()

@Deprecated
public static PrintStream sample code for java.io.PrintStream definition code for java.io.PrintStream  getLogStream()
Deprecated. 

Retrieves the logging/tracing PrintStream that is used by the DriverManager and all drivers.

Returns:
the logging/tracing PrintStream; if disabled, is null
See Also:
setLogStream(java.io.PrintStream) sample code for java.sql.DriverManager.setLogStream(java.io.PrintStream) definition code for java.sql.DriverManager.setLogStream(java.io.PrintStream)

println sample code for java.sql.DriverManager.println(java.lang.String) definition code for java.sql.DriverManager.println(java.lang.String)

public static void println(String sample code for java.lang.String definition code for java.lang.String  message)
Prints a message to the current JDBC log stream.

Parameters:
message - a log or tracing message