Stay organized with collections
Save and categorize content based on your preferences.
DataSource
public
interface
DataSource
implements
CommonDataSource,
Wrapper
A factory for connections to the physical data source that this
DataSource
object represents. An alternative to the
DriverManager
facility, a DataSource
object
is the preferred means of getting a connection. An object that implements
the DataSource
interface will typically be
registered with a naming service based on the
JavaTM Naming and Directory (JNDI) API.
The DataSource
interface is implemented by a driver vendor.
There are three types of implementations:
- Basic implementation -- produces a standard
Connection
object
- Connection pooling implementation -- produces a
Connection
object that will automatically participate in connection pooling. This
implementation works with a middle-tier connection pooling manager.
- 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.
A DataSource
object has properties that can be modified
when necessary. For example, if the data source is moved to a different
server, the property for the server can be changed. The benefit is that
because the data source's properties can be changed, any code accessing
that data source does not need to be changed.
A driver that is accessed via a DataSource
object does not
register itself with the DriverManager
. Rather, a
DataSource
object is retrieved though a lookup operation
and then used to create a Connection
object. With a basic
implementation, the connection obtained through a DataSource
object is identical to a connection obtained through the
DriverManager
facility.
Summary
Public methods |
abstract
Connection
|
getConnection()
Attempts to establish a connection with the data source that
this DataSource object represents.
|
abstract
Connection
|
getConnection(String username, String password)
Attempts to establish a connection with the data source that
this DataSource object represents.
|
Inherited methods |
From interface
javax.sql.CommonDataSource
abstract
PrintWriter
|
getLogWriter()
Retrieves the log writer for this DataSource
object.
|
abstract
int
|
getLoginTimeout()
Gets the maximum time in seconds that this data source can wait
while attempting to connect to a database.
|
abstract
Logger
|
getParentLogger()
Return the parent Logger of all the Loggers used by this data source.
|
abstract
void
|
setLogWriter(PrintWriter out)
Sets the log writer for this DataSource
object to the given java.io.PrintWriter object.
|
abstract
void
|
setLoginTimeout(int seconds)
Sets the maximum time in seconds that this data source will wait
while attempting to connect to a database.
|
|
From interface
java.sql.Wrapper
abstract
boolean
|
isWrapperFor(Class<?> iface)
Returns true if this either implements the interface argument or is directly or indirectly a wrapper
for an object that does.
|
abstract
<T>
T
|
unwrap(Class<T> iface)
Returns an object that implements the given interface to allow access to
non-standard methods, or standard methods not exposed by the proxy.
|
|
Public methods
getConnection
public abstract Connection getConnection ()
Attempts to establish a connection with the data source that
this DataSource
object represents.
Returns |
Connection |
a connection to the data source |
getConnection
public abstract Connection getConnection (String username,
String password)
Attempts to establish a connection with the data source that
this DataSource
object represents.
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 |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# DataSource\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nDataSource\n==========\n\n\n`\npublic\n\n\ninterface\nDataSource\n`\n\n\n`\n\n\nimplements\n\n`[CommonDataSource](/reference/javax/sql/CommonDataSource)`,\n\n`[Wrapper](/reference/java/sql/Wrapper)`\n\n\n`\n\n|----------------------|\n| javax.sql.DataSource |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\u003cbr /\u003e\n\nA factory for connections to the physical data source that this\n`DataSource` object represents. An alternative to the\n`DriverManager` facility, a `DataSource` object\nis the preferred means of getting a connection. An object that implements\nthe `DataSource` interface will typically be\nregistered with a naming service based on the\nJava^TM^ Naming and Directory (JNDI) API.\n\n\nThe `DataSource` interface is implemented by a driver vendor.\nThere are three types of implementations:\n\n1. Basic implementation -- produces a standard `Connection` object\n2. Connection pooling implementation -- produces a `Connection` object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.\n3. 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.\n\n\nA `DataSource` object has properties that can be modified\nwhen necessary. For example, if the data source is moved to a different\nserver, the property for the server can be changed. The benefit is that\nbecause the data source's properties can be changed, any code accessing\nthat data source does not need to be changed.\n\n\nA driver that is accessed via a `DataSource` object does not\nregister itself with the `DriverManager`. Rather, a\n`DataSource` object is retrieved though a lookup operation\nand then used to create a `Connection` object. With a basic\nimplementation, the connection obtained through a `DataSource`\nobject is identical to a connection obtained through the\n`DriverManager` facility.\n\nSummary\n-------\n\n| ### Public methods ||\n|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract `[Connection](/reference/java/sql/Connection) | ` `[getConnection](/reference/javax/sql/DataSource#getConnection())`() ` \u003cbr /\u003e Attempts to establish a connection with the data source that this `DataSource` object represents. |\n| ` abstract `[Connection](/reference/java/sql/Connection) | ` `[getConnection](/reference/javax/sql/DataSource#getConnection(java.lang.String,%20java.lang.String))`(`[String](/reference/java/lang/String)` username, `[String](/reference/java/lang/String)` password) ` \u003cbr /\u003e Attempts to establish a connection with the data source that this `DataSource` object represents. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` `[javax.sql.CommonDataSource](/reference/javax/sql/CommonDataSource)` ` |-----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract `[PrintWriter](/reference/java/io/PrintWriter) | ` `[getLogWriter](/reference/javax/sql/CommonDataSource#getLogWriter())`() ` \u003cbr /\u003e Retrieves the log writer for this `DataSource` object. | | ` abstract int` | ` `[getLoginTimeout](/reference/javax/sql/CommonDataSource#getLoginTimeout())`() ` Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. | | ` abstract `[Logger](/reference/java/util/logging/Logger) | ` `[getParentLogger](/reference/javax/sql/CommonDataSource#getParentLogger())`() ` Return the parent Logger of all the Loggers used by this data source. | | ` abstract void` | ` `[setLogWriter](/reference/javax/sql/CommonDataSource#setLogWriter(java.io.PrintWriter))`(`[PrintWriter](/reference/java/io/PrintWriter)` out) ` \u003cbr /\u003e Sets the log writer for this `DataSource` object to the given `java.io.PrintWriter` object. | | ` abstract void` | ` `[setLoginTimeout](/reference/javax/sql/CommonDataSource#setLoginTimeout(int))`(int seconds) ` \u003cbr /\u003e Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. | ||\n| From interface ` `[java.sql.Wrapper](/reference/java/sql/Wrapper)` ` |---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract boolean` | ` `[isWrapperFor](/reference/java/sql/Wrapper#isWrapperFor(java.lang.Class\u003c?\u003e))`(`[Class](/reference/java/lang/Class)`\u003c?\u003e iface) ` Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. | | ` abstract \u003cT\u003e T` | ` `[unwrap](/reference/java/sql/Wrapper#unwrap(java.lang.Class\u003cT\u003e))`(`[Class](/reference/java/lang/Class)`\u003cT\u003e iface) ` Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy. | ||\n\nPublic methods\n--------------\n\n### getConnection\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract Connection getConnection ()\n```\n\n\u003cbr /\u003e\n\nAttempts to establish a connection with the data source that\nthis `DataSource` object represents.\n\n\u003cbr /\u003e\n\n| Returns ||\n|----------------------------------------------|----------------------------------------|\n| [Connection](/reference/java/sql/Connection) | a connection to the data source \u003cbr /\u003e |\n\n| Throws ||\n|--------------------------------------------------|-----------------------------------|\n| [SQLException](/reference/java/sql/SQLException) | if a database access error occurs |\n\n### getConnection\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract Connection getConnection (String username, \n String password)\n```\n\n\u003cbr /\u003e\n\nAttempts to establish a connection with the data source that\nthis `DataSource` object represents.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------------|---------------------------------------------------------------------------------|\n| `username` | `String`: the database user on whose behalf the connection is being made \u003cbr /\u003e |\n| `password` | `String`: the user's password \u003cbr /\u003e |\n\n| Returns ||\n|----------------------------------------------|----------------------------------------|\n| [Connection](/reference/java/sql/Connection) | a connection to the data source \u003cbr /\u003e |\n\n| Throws ||\n|--------------------------------------------------|-----------------------------------|\n| [SQLException](/reference/java/sql/SQLException) | if a database access error occurs |"]]