DataSource
Stay organized with collections
Save and categorize content based on your preferences.
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.
Public Method Summary
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 Method Summary
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
public
abstract
Connection
getConnection
()
Attempts to establish a connection with the data source that
this DataSource
object represents.
Returns
- a connection to the data source
public
abstract
Connection
getConnection
(String username, String password)
Attempts to establish a connection with the data source that
this DataSource
object represents.
Parameters
username |
the database user on whose behalf the connection is
being made |
password |
the user's password |
Returns
- a connection to the data source
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eDataSource\u003c/code\u003e is the preferred method for obtaining database connections in Java, offering an alternative to \u003ccode\u003eDriverManager\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt acts as a factory for connections to a specific physical data source and is commonly registered with a naming service using JNDI.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDataSource\u003c/code\u003e implementations can provide basic connections, connection pooling, or support for distributed transactions.\u003c/p\u003e\n"],["\u003cp\u003eIt allows for modifying connection properties without requiring code changes and does not register itself with the \u003ccode\u003eDriverManager\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDataSource\u003c/code\u003e offers two \u003ccode\u003egetConnection\u003c/code\u003e methods, one without parameters and another accepting username and password for database authentication.\u003c/p\u003e\n"]]],[],null,["# DataSource\n\npublic interface **DataSource** implements [CommonDataSource](../../../reference/javax/sql/CommonDataSource.html) [Wrapper](../../../reference/java/sql/Wrapper.html) \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\n### Public Method Summary\n\n|--------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Connection](../../../reference/java/sql/Connection.html) | [getConnection](../../../reference/javax/sql/DataSource.html#getConnection())() Attempts to establish a connection with the data source that this `DataSource` object represents. |\n| abstract [Connection](../../../reference/java/sql/Connection.html) | [getConnection](../../../reference/javax/sql/DataSource.html#getConnection(java.lang.String,%20java.lang.String))([String](../../../reference/java/lang/String.html) username, [String](../../../reference/java/lang/String.html) password) Attempts to establish a connection with the data source that this `DataSource` object represents. |\n\n### Inherited Method Summary\n\nFrom interface [javax.sql.CommonDataSource](../../../reference/javax/sql/CommonDataSource.html) \n\n|---------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [PrintWriter](../../../reference/java/io/PrintWriter.html) | [getLogWriter](../../../reference/javax/sql/CommonDataSource.html#getLogWriter())() Retrieves the log writer for this `DataSource` object. |\n| abstract int | [getLoginTimeout](../../../reference/javax/sql/CommonDataSource.html#getLoginTimeout())() Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. |\n| abstract [Logger](../../../reference/java/util/logging/Logger.html) | [getParentLogger](../../../reference/javax/sql/CommonDataSource.html#getParentLogger())() Return the parent Logger of all the Loggers used by this data source. |\n| abstract void | [setLogWriter](../../../reference/javax/sql/CommonDataSource.html#setLogWriter(java.io.PrintWriter))([PrintWriter](../../../reference/java/io/PrintWriter.html) out) Sets the log writer for this `DataSource` object to the given `java.io.PrintWriter` object. |\n| abstract void | [setLoginTimeout](../../../reference/javax/sql/CommonDataSource.html#setLoginTimeout(int))(int seconds) Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. |\n\nFrom interface [java.sql.Wrapper](../../../reference/java/sql/Wrapper.html) \n\n|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract boolean | [isWrapperFor](../../../reference/java/sql/Wrapper.html#isWrapperFor(java.lang.Class\u003c?\u003e))([Class](../../../reference/java/lang/Class.html)\\\u003c?\\\u003e iface) Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. |\n| abstract \\\u003cT\\\u003e T | [unwrap](../../../reference/java/sql/Wrapper.html#unwrap(java.lang.Class\u003cT\u003e))([Class](../../../reference/java/lang/Class.html)\\\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#### public abstract [Connection](../../../reference/java/sql/Connection.html)\n**getConnection**\n()\n\n\u003cbr /\u003e\n\nAttempts to establish a connection with the data source that\nthis `DataSource` object represents. \n\n##### Returns\n\n- a connection to the data source \n\n##### Throws\n\n| [SQLException](../../../reference/java/sql/SQLException.html) | if a database access error occurs |\n|---------------------------------------------------------------|-----------------------------------|\n\n#### public abstract [Connection](../../../reference/java/sql/Connection.html)\n**getConnection**\n([String](../../../reference/java/lang/String.html) username, [String](../../../reference/java/lang/String.html) password)\n\n\u003cbr /\u003e\n\nAttempts to establish a connection with the data source that\nthis `DataSource` object represents. \n\n##### Parameters\n\n| username | the database user on whose behalf the connection is being made |\n| password | the user's password |\n|----------|----------------------------------------------------------------|\n\n##### Returns\n\n- a connection to the data source \n\n##### Throws\n\n| [SQLException](../../../reference/java/sql/SQLException.html) | if a database access error occurs |\n|---------------------------------------------------------------|-----------------------------------|"]]