Package java.sql
Class DriverManager
java.lang.Object
java.sql.DriverManager
public class DriverManager extends Object
Provides facilities for managing JDBC drivers. The
android.database and
android.database.sqlite packages offer a higher-performance alternative for new
code.
Note that Android does not include any JDBC drivers by default; you must provide your own.
The DriverManager class loads JDBC drivers during its initialization,
from the list of drivers referenced by the system property
"jdbc.drivers".
-
Method Summary
Modifier and Type Method Description static voidderegisterDriver(Driver driver)Removes a driver from theDriverManager's registered driver list.static ConnectiongetConnection(String url)Attempts to establish a connection to the given database URL.static ConnectiongetConnection(String url, String user, String password)Attempts to establish a connection to the given database URL.static ConnectiongetConnection(String url, Properties info)Attempts to establish a connection to the given database URL.static DrivergetDriver(String url)Tries to find a driver that can interpret the supplied URL.static Enumeration<Driver>getDrivers()Returns anEnumerationthat contains all of the loaded JDBC drivers that the current caller can access.static intgetLoginTimeout()Returns the login timeout when connecting to a database in seconds.static PrintStreamgetLogStream()Deprecated.static PrintWritergetLogWriter()Retrieves the log writer.static voidprintln(String message)Prints a message to the current JDBC log stream.static voidregisterDriver(Driver driver)Registers a given JDBC driver with theDriverManager.static voidsetLoginTimeout(int seconds)Sets the login timeout when connecting to a database in seconds.static voidsetLogStream(PrintStream out)Deprecated.UsesetLogWriter(java.io.PrintWriter)instead.static voidsetLogWriter(PrintWriter out)Sets thePrintWriterthat is used by all loaded drivers, and also theDriverManager.
-
Method Details
-
deregisterDriver
Removes a driver from theDriverManager'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
DriverManagerwill not use this driver in the future when asked to get aConnection.- Parameters:
driver- the JDBC driver to remove.- Throws:
SQLException- if there is a problem interfering with accessing the database.
-
getConnection
Attempts to establish a connection to the given database URL.- Parameters:
url- a URL string representing the database target to connect with.- Returns:
- a
Connectionto the database identified by the URL.nullif no connection can be established. - Throws:
SQLException- if there is an error while attempting to connect to the database identified by the URL.
-
getConnection
Attempts to establish a connection to the given database URL.- Parameters:
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.- Returns:
- a
Connectionto the database identified by the URL.nullif no connection can be established. - Throws:
SQLException- if there is an error while attempting to connect to the database identified by the URL.
-
getConnection
public static Connection getConnection(String url, String user, String password) throws SQLExceptionAttempts to establish a connection to the given database URL.- Parameters:
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.- Returns:
- a
Connectionto the database identified by the URL.nullif no connection can be established. - Throws:
SQLException- if there is an error while attempting to connect to the database identified by the URL.
-
getDriver
Tries to find a driver that can interpret the supplied URL.- Parameters:
url- the URL of a database.- Returns:
- a
Driverthat matches the provided URL.nullif noDriverunderstands the URL - Throws:
SQLException- if there is any kind of problem accessing the database.
-
getDrivers
Returns anEnumerationthat contains all of the loaded JDBC drivers that the current caller can access.- Returns:
- An
Enumerationcontaining all the currently loaded JDBCDrivers.
-
getLoginTimeout
public static int getLoginTimeout()Returns the login timeout when connecting to a database in seconds.- Returns:
- the login timeout in seconds.
-
getLogStream
Deprecated.UsegetLogWriter()instead.Gets the logPrintStreamused by theDriverManagerand all the JDBC Drivers.- Returns:
- the
PrintStreamused for logging activities.
-
getLogWriter
Retrieves the log writer.- Returns:
- A
PrintWriterobject used as the log writer.nullif no log writer is set.
-
println
Prints a message to the current JDBC log stream. This is either thePrintWriteror (deprecated) thePrintStream, if set.- Parameters:
message- the message to print to the JDBC log stream.
-
registerDriver
Registers a given JDBC driver with theDriverManager.A newly loaded JDBC driver class should register itself with the
DriverManagerby calling this method.- Parameters:
driver- theDriverto register with theDriverManager.- Throws:
SQLException- if a database access error occurs.
-
setLoginTimeout
public static void setLoginTimeout(int seconds)Sets the login timeout when connecting to a database in seconds.- Parameters:
seconds- seconds until timeout. 0 indicates wait forever.
-
setLogStream
Deprecated.UsesetLogWriter(java.io.PrintWriter)instead.Sets the print stream to use for logging data from theDriverManagerand the JDBC drivers.- Parameters:
out- thePrintStreamto use for logging.
-
setLogWriter
Sets thePrintWriterthat is used by all loaded drivers, and also theDriverManager.- Parameters:
out- thePrintWriterto be used.
-
getLogWriter()instead.