/*
 * Created on 2005.09.19 at 01:07:56 PDT
 * 
 * The Monterey Bay Aquarium Research Institute (MBARI) provides this
 * documentation and code 'as is', with no warranty, express or
 * implied, of its quality or consistency. It is provided without support and
 * without obligation on the part of MBARI to assist in its use, correction,
 * modification, or enhancement. This information should not be published or
 * distributed to third parties without specific written permission from MBARI
 */

/*
 * The Monterey Bay Aquarium Research Institute (MBARI) provides this
 * documentation and code 'as is', with no warranty, express or
 * implied, of its quality or consistency. It is provided without support and
 * without obligation on the part of MBARI to assist in its use, correction,
 * modification, or enhancement. This information should not be published or
 * distributed to third parties without specific written permission from MBARI
 */
package org.mbari.vdm.expd;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;

import org.mbari.sql.DBException;

/**
 * <p>Convience class used to establish a connection to a database. This class
 * is not called directly by a developer, rather it is used to support
 * <code>ConnectionFactory</code>.</p>
 *
 * @author <a href="http://www.mbari.org">MBARI</a>
 * @version $Id: DBConnection.java,v 1.5 2005/09/21 17:02:51 brian Exp $
 */
class DBConnection {

    //~--- fields -------------------------------------------------------------

    private String password;

    private String url;
    private String user;

    //~--- constructors -------------------------------------------------------

    /**
     * Constructs ...
     *
     *
     * @param ref
     *
     * @throws DBException
     */
    DBConnection(String ref) throws DBException {
        ResourceBundle rb = ResourceBundle.getBundle(ref);

        url = rb.getString("db.url");
        user = rb.getString("db.user");
        password = rb.getString("db.password");

        String driver = rb.getString("jdbc.driver");
        int timeout = Integer.parseInt(rb.getString("db.timeout"));

        // The driver used and the URL are specified in vars.properties
        try {
            Class.forName(driver);
        } catch (Exception e) {
            DBException s = new DBException();

            s.initCause(e);

            throw s;
        }

        DriverManager.setLoginTimeout(timeout);
    }

    //~--- get methods --------------------------------------------------------

    /**
     * <p><!-- Method description --></p>
     *
     *
     * @return
     *
     * @throws DBException
     */
    Connection getConnection() throws DBException {
        Connection con = null;

        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            DBException s = new DBException();

            s.initCause(e);

            throw s;
        }

        return con;
    }

}
