Provides facilities for managing JDBC drivers.
The {@code DriverManager} class loads JDBC drivers during its initialization,
from the list of drivers referenced by the system property {@code
"jdbc.drivers"}.
| Method from java.sql.DriverManager Detail: |
public static void deregisterDriver(Driver driver) throws SQLException {
if (driver == null) {
return;
}
ClassLoader callerClassLoader = VM.callerClassLoader();
if (!DriverManager.isClassFromClassLoader(driver, callerClassLoader)) {
// sql.1=DriverManager: calling class not authorized to deregister
// JDBC driver
throw new SecurityException(Messages.getString("sql.1")); //$NON-NLS-1$
} // end if
synchronized (theDrivers) {
theDrivers.remove(driver);
}
}
Removes a driver from the {@code 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 {@code DriverManager} will not use this
driver in the future when asked to get a {@code Connection}. |
public static Connection getConnection(String url) throws SQLException {
return getConnection(url, new Properties());
}
Attempts to establish a connection to the given database URL. |
public static Connection getConnection(String url,
Properties info) throws SQLException {
// 08 - connection exception
// 001 - SQL-client unable to establish SQL-connection
String sqlState = "08001"; //$NON-NLS-1$
if (url == null) {
// sql.5=The url cannot be null
throw new SQLException(Messages.getString("sql.5"), sqlState); //$NON-NLS-1$
}
synchronized (theDrivers) {
/*
* Loop over the drivers in the DriverSet checking to see if one can
* open a connection to the supplied URL - return the first
* connection which is returned
*/
for (Driver theDriver : theDrivers) {
Connection theConnection = theDriver.connect(url, info);
if (theConnection != null) {
return theConnection;
}
}
}
// If we get here, none of the drivers are able to resolve the URL
// sql.6=No suitable driver
throw new SQLException(Messages.getString("sql.6"), sqlState); //$NON-NLS-1$
}
Attempts to establish a connection to the given database URL. |
public static Connection getConnection(String url,
String user,
String password) throws SQLException {
Properties theProperties = new Properties();
if (null != user) {
theProperties.setProperty("user", user); //$NON-NLS-1$
}
if (null != password) {
theProperties.setProperty("password", password); //$NON-NLS-1$
}
return getConnection(url, theProperties);
}
Attempts to establish a connection to the given database URL. |
public static Driver getDriver(String url) throws SQLException {
ClassLoader callerClassLoader = VM.callerClassLoader();
synchronized (theDrivers) {
/*
* Loop over the drivers in the DriverSet checking to see if one
* does understand the supplied URL - return the first driver which
* does understand the URL
*/
Iterator< Driver > theIterator = theDrivers.iterator();
while (theIterator.hasNext()) {
Driver theDriver = theIterator.next();
if (theDriver.acceptsURL(url)
&& DriverManager.isClassFromClassLoader(theDriver,
callerClassLoader)) {
return theDriver;
}
}
}
// If no drivers understand the URL, throw an SQLException
// sql.6=No suitable driver
// SQLState: 08 - connection exception
// 001 - SQL-client unable to establish SQL-connection
throw new SQLException(Messages.getString("sql.6"), "08001"); //$NON-NLS-1$ //$NON-NLS-2$
}
Tries to find a driver that can interpret the supplied URL. |
public static Enumeration<Driver> getDrivers() {
ClassLoader callerClassLoader = VM.callerClassLoader();
/*
* Synchronize to avoid clashes with additions and removals of drivers
* in the DriverSet
*/
synchronized (theDrivers) {
/*
* Create the Enumeration by building a Vector from the elements of
* the DriverSet
*/
Vector< Driver > theVector = new Vector< Driver >();
Iterator< Driver > theIterator = theDrivers.iterator();
while (theIterator.hasNext()) {
Driver theDriver = theIterator.next();
if (DriverManager.isClassFromClassLoader(theDriver,
callerClassLoader)) {
theVector.add(theDriver);
}
}
return theVector.elements();
}
}
Returns an {@code Enumeration} that contains all of the loaded JDBC
drivers that the current caller can access. |
public static PrintStream getLogStream() {
return thePrintStream;
} Deprecated! use - #getLogWriter() instead.
Gets the log {@code PrintStream} used by the {@code DriverManager} and
all the JDBC Drivers. |
public static PrintWriter getLogWriter() {
return thePrintWriter;
}
Retrieves the log writer. |
public static int getLoginTimeout() {
return loginTimeout;
}
Returns the login timeout when connecting to a database in seconds. |
public static void println(String message) {
if (thePrintWriter != null) {
thePrintWriter.println(message);
thePrintWriter.flush();
} else if (thePrintStream != null) {
thePrintStream.println(message);
thePrintStream.flush();
}
/*
* If neither the PrintWriter not the PrintStream are set, then silently
* do nothing the message is not recorded and no exception is generated.
*/
return;
}
Prints a message to the current JDBC log stream. This is either the
{@code PrintWriter} or (deprecated) the {@code PrintStream}, if set. |
public static void registerDriver(Driver driver) throws SQLException {
if (driver == null) {
throw new NullPointerException();
}
synchronized (theDrivers) {
theDrivers.add(driver);
}
}
Registers a given JDBC driver with the {@code DriverManager}.
A newly loaded JDBC driver class should register itself with the
{@code DriverManager} by calling this method. |
public static void setLogStream(PrintStream out) {
checkLogSecurity();
thePrintStream = out;
} Deprecated! Use - #setLogWriter instead.
Sets the print stream to use for logging data from the {@code
DriverManager} and the JDBC drivers. |
public static void setLogWriter(PrintWriter out) {
checkLogSecurity();
thePrintWriter = out;
}
Sets the {@code PrintWriter} that is used by all loaded drivers, and also
the {@code DriverManager}. |
public static void setLoginTimeout(int seconds) {
loginTimeout = seconds;
return;
}
Sets the login timeout when connecting to a database in seconds. |