All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.molcikas.photon.datasource.GenericDataSource Maven / Gradle / Ivy

The newest version!
package com.github.molcikas.photon.datasource;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;


public class GenericDataSource implements DataSource {

    private final String url;
    private final Properties properties;

    public GenericDataSource(String url, String user, String password) {

        if (!url.startsWith("jdbc")){
            url = "jdbc:" + url;
        }

        this.url = url;
        this.properties = new Properties();
        set(properties,user,password);
    }

    private void set(Properties info, String user, String password) {
        if (user != null) {
            info.put("user", user);
        }
        if (password != null) {
            info.put("password", password);
        }
    }

    public GenericDataSource(String url, Properties properties) {

        if (!url.startsWith("jdbc")){
            url = "jdbc:" + url;
        }

        this.url = url;
        this.properties = properties;
    }

    public String getUrl() {
        return url;
    }

    public String getUser() {
        return properties.getProperty("user");
    }

    public String getPassword() {
        return properties.getProperty("password");
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(this.getUrl(), properties);
    }

    public Connection getConnection(String username, String password) throws SQLException {
        Properties info = new Properties(this.properties);
        set(info,username,password);
        return DriverManager.getConnection(this.getUrl(), info);
    }

    public PrintWriter getLogWriter() throws SQLException {
        return DriverManager.getLogWriter();
    }

    public void setLogWriter(PrintWriter printWriter) throws SQLException {
        DriverManager.setLogWriter(printWriter);
    }

    public void setLoginTimeout(int i) throws SQLException {
        DriverManager.setLoginTimeout(i);
    }

    public int getLoginTimeout() throws SQLException {
        return DriverManager.getLoginTimeout();
    }

    public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }

    public  T unwrap(Class tClass) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    public boolean isWrapperFor(Class aClass) throws SQLException {
        return false;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy