public class DriverManager
extends Object
| java.lang.Object | |
| java.sql.DriverManager | |
管理一组JDBC驱动程序的基本服务。
注意: JDBC 2.0 API中新增的DataSource接口提供了另一种连接数据源的方式。 使用DataSource对象是连接数据源的首选方式。
作为初始化的一部分, DriverManager类将尝试加载“jdbc.drivers”系统属性中引用的驱动程序类。 这允许用户自定义其应用程序使用的JDBC驱动程序。 例如,在你的/ .hotjava / properties文件中你可以指定:
jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver
DriverManager方法getConnection和getDrivers已得到增强,以支持Java标准版Service Provider机制。 JDBC 4.0驱动程序必须包含文件META-INF/services/java.sql.Driver 。 该文件包含java.sql.Driver的JDBC驱动程序实现的java.sql.Driver 。 例如,要加载my.sql.Driver类, META-INF/services/java.sql.Driver文件将包含条目:
my.sql.Driver
应用程序不再需要使用Class.forName()来显式加载JDBC驱动程序。 当前使用Class.forName()加载JDBC驱动程序的现有程序将继续运行而不进行修改。
当调用方法 getConnection , DriverManager将尝试从初始化加载的驱动程序中找到合适的驱动程序,并使用与当前小程序或应用程序相同的类加载程序显式加载。
从Java 2 SDK Standard Edition版本1.3开始,只有在授予适当权限后才能设置日志流。 通常这将通过工具PolicyTool完成,该工具可用于授予permission java.sql.SQLPermission "setLog" 。
也可以看看:
公共方法(Public methods) |
|
|---|---|
static void |
deregisterDriver(Driver driver) 从 |
static Connection |
getConnection(String url) 尝试建立到给定数据库URL的连接。 |
static Connection |
getConnection(String url, String user, String password) 尝试建立到给定数据库URL的连接。 |
static Connection |
getConnection(String url, Properties info) 尝试建立到给定数据库URL的连接。 |
static Driver |
getDriver(String url) 尝试找到理解给定URL的驱动程序。 |
static Enumeration<Driver> |
getDrivers() 使用当前调用者可以访问的所有当前加载的JDBC驱动程序检索Enumeration。 |
static PrintStream |
getLogStream() 此方法在API级别1中已弃用。请改用 |
static PrintWriter |
getLogWriter() 检索日志编写器。 |
static int |
getLoginTimeout() 获取驱动程序尝试登录数据库时可以等待的最长时间(以秒为单位)。 |
static void |
println(String message) 将消息打印到当前的JDBC日志流。 |
static void |
registerDriver(Driver driver) 向给定的驱动程序注册 |
static void |
setLogStream(PrintStream out) 此方法在API级别1中已弃用。请改为使用 |
static void |
setLogWriter(PrintWriter out) 设置 |
static void |
setLoginTimeout(int seconds) 设置驱动程序在尝试连接数据库时等待的最长时间(以秒为单位)。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
void deregisterDriver (Driver driver)
从DriverManager的列表中删除驱动程序。 Applets只能从他们自己的类加载器注销驱动程序。
| 参数(Parameters) | |
|---|---|
driver |
Driver: the JDBC Driver to drop |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
Connection getConnection (String url)
尝试建立到给定数据库URL的连接。 DriverManager尝试从注册JDBC驱动程序集中选择适当的驱动程序。
| 参数(Parameters) | |
|---|---|
url |
String: a database url of the form jdbc:subprotocol:subname |
| 返回(Returns) | |
|---|---|
Connection |
a connection to the URL |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
Connection getConnection (String url, String user, String password)
尝试建立到给定数据库URL的连接。 DriverManager尝试从注册JDBC驱动程序集中选择适当的驱动程序。
| 参数(Parameters) | |
|---|---|
url |
String: a database url of the form jdbc:subprotocol:subname |
user |
String: the database user on whose behalf the connection is being made |
password |
String: the user's password |
| 返回(Returns) | |
|---|---|
Connection |
a connection to the URL |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
Connection getConnection (String url, Properties info)
尝试建立到给定数据库URL的连接。 DriverManager尝试从已注册的JDBC驱动程序集中选择适当的驱动程序。
| 参数(Parameters) | |
|---|---|
url |
String: a database url of the form jdbc:subprotocol:subname |
info |
Properties: a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and "password" property should be included |
| 返回(Returns) | |
|---|---|
Connection |
a Connection to the URL |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
Driver getDriver (String url)
尝试找到理解给定URL的驱动程序。 DriverManager尝试从已注册的JDBC驱动程序集中选择适当的驱动程序。
| 参数(Parameters) | |
|---|---|
url |
String: a database URL of the form jdbc:subprotocol:subname |
| 返回(Returns) | |
|---|---|
Driver |
a Driver object representing a driver that can connect to the given URL |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
Enumeration<Driver> getDrivers ()
使用当前调用者可以访问的所有当前加载的JDBC驱动程序检索Enumeration。
注意:驱动程序的类名可以使用 d.getClass().getName()找到
| 返回(Returns) | |
|---|---|
Enumeration<Driver> |
the list of JDBC Drivers loaded by the caller's class loader |
PrintStream getLogStream ()
此方法在API级别1中已弃用。
改为使用getLogWriter 。
检索由 DriverManager和所有驱动程序使用的日志记录/跟踪PrintStream。
| 返回(Returns) | |
|---|---|
PrintStream |
the logging/tracing PrintStream; if disabled, is null |
也可以看看:
PrintWriter getLogWriter ()
检索日志编写器。 应该使用getLogWriter和setLogWriter方法,而不使用get/setlogStream方法,该方法已弃用。
| 返回(Returns) | |
|---|---|
PrintWriter |
a java.io.PrintWriter object |
也可以看看:
int getLoginTimeout ()
获取驱动程序尝试登录数据库时可以等待的最长时间(以秒为单位)。
| 返回(Returns) | |
|---|---|
int |
the driver login time limit in seconds |
也可以看看:
void println (String message)
将消息打印到当前的JDBC日志流。
| 参数(Parameters) | |
|---|---|
message |
String: a log or tracing message |
void registerDriver (Driver driver)
将给定的驱动程序注册到DriverManager 。 新加载的驱动程序类应该调用方法registerDriver以使自己知道DriverManager 。
| 参数(Parameters) | |
|---|---|
driver |
Driver: the new JDBC Driver that is to be registered with the DriverManager |
| 抛出异常(Throws) | |
|---|---|
SQLException |
if a database access error occurs |
void setLogStream (PrintStream out)
此方法在API级别1中已弃用。
改为使用setLogWriter 。
设置 DriverManager和所有驱动程序使用的记录/跟踪PrintStream。
在Java 2 SDK Standard Edition 1.3版本中,此方法在设置日志记录流之前检查是否有SQLPermission对象。 如果存在SecurityManager并且其checkPermission方法拒绝设置日志java.lang.SecurityException ,则此方法将引发java.lang.SecurityException 。
| 参数(Parameters) | |
|---|---|
out |
PrintStream: the new logging/tracing PrintStream; to disable, set to null |
| 抛出异常(Throws) | |
|---|---|
SecurityException |
if a security manager exists and its checkPermission method denies setting the log stream |
void setLogWriter (PrintWriter out)
设置 DriverManager和所有驱动程序使用的记录/跟踪 PrintWriter对象。
通过引入方法setLogWriter创建了一个次要的版本控制问题。 该方法setLogWriter不能创建PrintStream将被返回的对象getLogStream --- Java平台不提供向后转换。 其结果是,使用新的应用程序setLogWriter ,并且也使用了使用JDBC 1.0驱动程序getLogStream可能不会看到调试通过该驱动程序的书面资料。
从Java 2 SDK标准版1.3版本开始,此方法在设置日志流之前检查是否有SQLPermission对象。 如果存在SecurityManager ,并且其checkPermission方法拒绝设置日志java.lang.SecurityException ,则此方法将引发java.lang.SecurityException 。
| 参数(Parameters) | |
|---|---|
out |
PrintWriter: the new logging/tracing PrintStream object; null to disable logging and tracing |
| 抛出异常(Throws) | |
|---|---|
SecurityException |
if a security manager exists and its checkPermission method denies setting the log writer |
void setLoginTimeout (int seconds)
设置驱动程序在尝试连接数据库时等待的最长时间(以秒为单位)。
| 参数(Parameters) | |
|---|---|
seconds |
int: the login time limit in seconds; zero means there is no limit |
也可以看看: