DriverManagerDataSource.java :  » Database-ORM » TJDO » com » triactive » jdo » Java Open Source

Java Open Source » Database ORM » TJDO 
TJDO » com » triactive » jdo » DriverManagerDataSource.java
/*
 * Copyright 2002 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: DriverManagerDataSource.java,v 1.4 2003/05/24 03:28:35 jackknifebarber Exp $
 */

package com.triactive.jdo;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.jdo.JDOFatalUserException;
import javax.sql.DataSource;


public class DriverManagerDataSource implements DataSource
{
    private final String driverName;
    private final String URL;


    public DriverManagerDataSource(String driverName, String URL)
    {
        this.driverName = driverName;
        this.URL        = URL;

        if (driverName != null)
        {
            try
            {
                Class.forName(driverName).newInstance();
            }
            catch (Exception e)
            {
                throw new JDOFatalUserException("Invalid driver class " + driverName, e);
            }
        }
    }

    public Connection getConnection() throws SQLException
    {
        return DriverManager.getConnection(URL);
    }

    public Connection getConnection(String userName, String password) throws SQLException
    {
        return DriverManager.getConnection(URL, userName, password);
    }

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

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

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

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

    public boolean equals(Object obj)
    {
        if (obj == this)
            return true;

        if (!(obj instanceof DriverManagerDataSource))
            return false;

        DriverManagerDataSource dmds = (DriverManagerDataSource)obj;

        if (driverName == null) { if (dmds.driverName != null) return false; }
        else if (!driverName.equals(dmds.driverName)) return false;

        if (URL == null) { if (dmds.URL != null) return false; }
        else if (!URL.equals(dmds.URL)) return false;

        return true;
    }

    public int hashCode()
    {
        return (driverName == null ? 0 : driverName.hashCode())
             ^ (URL        == null ? 0 : URL.hashCode());
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.