/*
 * Created on Dec 1, 2004
 * Copyright (C) Andrea Schweer, 2004
 *
 * This file is part of the Greenstone Alerting Service.
 * Refer to the COPYING file in the base directory of this package
 * for licensing information.
 */
package org.greenstone.gsdlas.database;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;


/**
 * @author andrea
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DatabaseManager {

    private static DatabaseManager instance;
    private Connection database;

    private DatabaseManager() {
        // hide constructor
    }
    
    public static DatabaseManager getInstance() {
        if (instance == null) {
            instance = new DatabaseManager();
        }
        return instance;
    }
    
    public Connection getDatabaseConnection() throws DatabaseException {
        if(database == null) {
            String connectionString = "jdbc:mysql://localhost/alerting?user=gsdl3admin";
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                database = DriverManager.getConnection(connectionString);
            } catch (Exception e) {
                if (e instanceof SQLException) {
                    SQLException sqle = (SQLException) e;
                    System.err.println("SQLState: " + sqle.getSQLState());
                }
                throw new DatabaseException("Could not establish connection to database (" + connectionString + ")", e);
            }
        }
        return database;
    }
    
    
}
