commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henri Yandell <flame...@gmail.com>
Subject [dbutils] SystemDataSource
Date Wed, 03 Aug 2005 17:35:42 GMT
Just had need to hack together a simple DataSource class and wondered
if it would fit nicely in dbutils. Name is either SystemDataSource
(after SystemClassLoader) or DriverManagerDataSource, it uses Java -D
properties and the DriverManager, so is very lightweight and something
nice to start with before moving up to a container that supplies a
real DataSource.

I imagine there are MockDataSources out there that are similar too for
unit testing, but nothing in DbUtils yet.

(code follows, it's pretty dumb)

public class SystemDataSource implements DataSource {

    private String driver = System.getProperty("jdbc.driver");
    private String username = System.getProperty("jdbc.user");
    private String password = System.getProperty("jdbc.password");
    private String uri = System.getProperty("jdbc.uri");

    public SystemDataSource() {
        DbUtils.loadDriver(driver);
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(this.uri, this.username,
this.password);
    }

    public Connection getConnection(String username, String password)
throws SQLException {
        return DriverManager.getConnection(this.uri, username, password);
    }

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

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

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

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

}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message