public class DriverManager extends Object
The DriverManager
class loads JDBC drivers during its initialization,
from the list of drivers referenced by the system property "jdbc.drivers"
.
Modifier and Type | Method and Description |
---|---|
static void |
deregisterDriver(Driver driver)
Removes a driver from the
DriverManager 's registered driver list. |
static Connection |
getConnection(String url)
Attempts to establish a connection to the given database URL.
|
static Connection |
getConnection(String url,
Properties info)
Attempts to establish a connection to the given database URL.
|
static Connection |
getConnection(String url,
String user,
String password)
Attempts to establish a connection to the given database URL.
|
static Driver |
getDriver(String url)
Tries to find a driver that can interpret the supplied URL.
|
static Enumeration<Driver> |
getDrivers()
Returns an
Enumeration that contains all of the loaded JDBC
drivers that the current caller can access. |
static int |
getLoginTimeout()
Returns the login timeout when connecting to a database in seconds.
|
static PrintStream |
getLogStream()
Deprecated.
use
getLogWriter() instead. |
static PrintWriter |
getLogWriter()
Retrieves the log writer.
|
static void |
println(String message)
Prints a message to the current JDBC log stream.
|
static void |
registerDriver(Driver driver)
Registers a given JDBC driver with the
DriverManager . |
static void |
setLoginTimeout(int seconds)
Sets the login timeout when connecting to a database in seconds.
|
static void |
setLogStream(PrintStream out)
Deprecated.
Use
setLogWriter(java.io.PrintWriter) instead. |
static void |
setLogWriter(PrintWriter out)
Sets the
PrintWriter that is used by all loaded drivers, and also
the DriverManager . |
public static void deregisterDriver(Driver driver) throws SQLException
DriverManager
's registered driver list.
This will only succeed when the caller's class loader loaded the driver
that is to be removed. If the driver was loaded by a different class
loader, the removal of the driver fails silently.
If the removal succeeds, the DriverManager
will not use this
driver in the future when asked to get a Connection
.
driver
- the JDBC driver to remove.SQLException
- if there is a problem interfering with accessing the
database.public static Connection getConnection(String url) throws SQLException
url
- a URL string representing the database target to connect with.Connection
to the database identified by the URL.
null
if no connection can be established.SQLException
- if there is an error while attempting to connect to the
database identified by the URL.public static Connection getConnection(String url, Properties info) throws SQLException
url
- a URL string representing the database target to connect withinfo
- a set of properties to use as arguments to set up the
connection. Properties are arbitrary string/value pairs.
Normally, at least the properties "user"
and "password"
should be passed, with appropriate settings for
the user ID and its corresponding password to get access to
the corresponding database.Connection
to the database identified by the URL.
null
if no connection can be established.SQLException
- if there is an error while attempting to connect to the
database identified by the URL.public static Connection getConnection(String url, String user, String password) throws SQLException
url
- a URL string representing the database target to connect with.user
- a user ID used to login to the database.password
- a password for the user ID to login to the database.Connection
to the database identified by the URL.
null
if no connection can be established.SQLException
- if there is an error while attempting to connect to the
database identified by the URL.public static Driver getDriver(String url) throws SQLException
url
- the URL of a database.Driver
that matches the provided URL. null
if
no Driver
understands the URLSQLException
- if there is any kind of problem accessing the database.public static Enumeration<Driver> getDrivers()
Enumeration
that contains all of the loaded JDBC
drivers that the current caller can access.Enumeration
containing all the currently loaded JDBC
Drivers
.public static int getLoginTimeout()
@Deprecated public static PrintStream getLogStream()
getLogWriter()
instead.PrintStream
used by the DriverManager
and
all the JDBC Drivers.PrintStream
used for logging activities.public static PrintWriter getLogWriter()
PrintWriter
object used as the log writer. null
if no log writer is set.public static void println(String message)
PrintWriter
or (deprecated) the PrintStream
, if set.message
- the message to print to the JDBC log stream.public static void registerDriver(Driver driver) throws SQLException
DriverManager
.
A newly loaded JDBC driver class should register itself with the
DriverManager
by calling this method.
driver
- the Driver
to register with the DriverManager
.SQLException
- if a database access error occurs.public static void setLoginTimeout(int seconds)
seconds
- seconds until timeout. 0 indicates wait forever.@Deprecated public static void setLogStream(PrintStream out)
setLogWriter(java.io.PrintWriter)
instead.DriverManager
and the JDBC drivers.out
- the PrintStream
to use for logging.public static void setLogWriter(PrintWriter out)
PrintWriter
that is used by all loaded drivers, and also
the DriverManager
.out
- the PrintWriter
to be used.