1package dev.rafex.ether.jdbc.datasource;
29import java.io.PrintWriter;
30import java.sql.Connection;
31import java.sql.DriverManager;
32import java.sql.SQLException;
33import java.sql.SQLFeatureNotSupportedException;
34import java.util.Objects;
35import java.util.Properties;
36import java.util.logging.Logger;
38import javax.sql.DataSource;
48 private final String url;
49 private final Properties properties;
50 private volatile PrintWriter logWriter;
51 private volatile int loginTimeout;
60 this(url,
new Properties());
71 public SimpleDataSource(
final String url,
final String username,
final String password) {
72 this(url, toProperties(username, password));
83 this.url = Objects.requireNonNull(url,
"url");
84 this.properties =
new Properties();
85 if (properties !=
null) {
86 this.properties.putAll(properties);
98 DriverManager.setLoginTimeout(loginTimeout);
99 if (logWriter !=
null) {
100 DriverManager.setLogWriter(logWriter);
102 return DriverManager.getConnection(url, properties);
114 public Connection
getConnection(
final String username,
final String password)
throws SQLException {
115 return DriverManager.getConnection(url, username, password);
125 this.logWriter = out;
130 this.loginTimeout = seconds;
146 throw new SQLFeatureNotSupportedException(
"Parent logger is not supported");
150 public <T> T unwrap(
final Class<T> iface)
throws SQLException {
151 if (iface.isInstance(
this)) {
152 return iface.cast(
this);
154 throw new SQLException(
"Unsupported unwrap type: " + iface.getName());
159 return iface.isInstance(
this);
162 private static Properties toProperties(
final String username,
final String password) {
163 final var properties =
new Properties();
164 if (username !=
null) {
165 properties.setProperty(
"user", username);
167 if (password !=
null) {
168 properties.setProperty(
"password", password);
Logger getParentLogger()
Not supported by this implementation.
void setLoginTimeout(final int seconds)
PrintWriter getLogWriter()
Connection getConnection(final String username, final String password)
Retrieves a database connection using the specified username and password.
SimpleDataSource(final String url, final String username, final String password)
Creates a new SimpleDataSource with the specified JDBC URL, username, and password.
void setLogWriter(final PrintWriter out)
SimpleDataSource(final String url)
Creates a new SimpleDataSource with the specified JDBC URL.
Connection getConnection()
Retrieves a database connection using the configured URL and properties.
SimpleDataSource(final String url, final Properties properties)
Creates a new SimpleDataSource with the specified JDBC URL and properties.
boolean isWrapperFor(final Class<?> iface)