Most visited

Recently visited

Added in API level 1

DataSource

public interface DataSource
implements CommonDataSource, Wrapper

javax.sql.DataSource


连接DataSource对象所代表的物理数据源的工厂。 作为DriverManager工具的替代方案, DataSource对象是获取连接的首选方法。 实现DataSource接口的对象通常会使用基于Java TM命名和目录(JNDI)API的命名服务进行注册。

DataSource接口由驱动程序供应商实施。 有三种类型的实现:

  1. Basic implementation -- produces a standard Connection object
  2. Connection pooling implementation -- produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.
  3. Distributed transaction implementation -- produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.

一个DataSource对象具有可在必要时进行修改的属性。 例如,如果将数据源移动到其他服务器,则可以更改服务器的属性。 好处是,因为可以更改数据源的属性,所以访问该数据源的任何代码都不需要更改。

通过DataSource对象访问的驱动程序不会向DriverManager注册。 而是通过查找操作检索DataSource对象,然后用于创建Connection对象。 通过基本实现,通过DataSource对象获得的连接与通过DriverManager工具获得的连接完全相同。

摘要(Summary)

公共方法(Public methods)

abstract Connection getConnection()

试图建立与该数据源的连接 DataSource对象表示。

abstract Connection getConnection(String username, String password)

试图建立与该数据源的连接 DataSource对象表示。

继承方法(Inherited methods)

From interface javax.sql.CommonDataSource
From interface java.sql.Wrapper

公共方法(Public methods)

getConnection

Added in API level 1
Connection getConnection ()

Attempts to establish a connection with the data source that this DataSource object represents.

返回(Returns)
Connection a connection to the data source
抛出异常(Throws)
SQLException if a database access error occurs

getConnection

Added in API level 1
Connection getConnection (String username, 
                String password)

试图建立与该数据源的连接 DataSource对象表示。

参数(Parameters)
username String: the database user on whose behalf the connection is being made
password String: the user's password
返回(Returns)
Connection a connection to the data source
抛出异常(Throws)
SQLException if a database access error occurs

Hooray!