Understanding DataSource Interface

The DataSource interface belongs to the javax.sql package and provides an alternative to DriverManager for establishing database connections. It is commonly used in enterprise applications because it supports features such as connection pooling and easier database configuration.

Methods of DataSource Interface

Methods

public Connection getConnection() throws SQLException

Establishes a connection to the database using the configured data source properties.

public Connection getConnection(String username, String password) throws SQLException

Establishes a database connection using the specified username and password.

public PrintWriter getLogWriter() throws SQLException

Returns the PrintWriter used for logging and tracing database operations.

public void setLogWriter(PrintWriter out) throws SQLException

Sets the PrintWriter used for logging and tracing database operations.

public void setLoginTimeout(int seconds) throws SQLException

Sets the maximum time allowed for establishing a database connection.

public int getLoginTimeout() throws SQLException

Returns the maximum time allowed for establishing a database connection.

public Logger getParentLogger()

Returns the parent logger used by the data source.

public <T> T unwrap(Class<T> iface) throws SQLException

Returns an object that implements the specified JDBC interface.

public boolean isWrapperFor(Class<?> iface) throws SQLException

Checks whether the data source supports the specified JDBC interface.

Scroll to Top