package innerClass;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * The SINGLETON pattern
 * 
 * Using the Singleton pattern, create a unique instance of the inner 
 * class JDBCMySQLConnection.  Since we always need a database connection,
 * create its Connection instance eagerly.
 */
public class ConnectionManager {
	
    private ConnectionManager() {}  // Private constructor!
	
    // This method returns THE connection
    public static Connection getMySQLConnection() {
        return JDBCMySQLConnection.getConnection();
    }

 
   /**
    * This is a private inner class.  Only usable by its owner 
    * ConnectionManager.
    */
	private static class JDBCMySQLConnection {
	
		private static final String URL = "jdbc:mysql://localhost/wordnet30";
		private static final String USER = "root";
		private static final String PASSWORD = "p@ssw0rd";
		private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; 
		private static Connection mySQLconnection = createConnection();
		 
	    //private constructor
	    private static Connection createConnection() {
	        try {
	            // Load MySQL Java driver
	            Class.forName(DRIVER_CLASS);
	            // Establish Java MySQL connection
	            mySQLconnection = DriverManager.getConnection(URL, USER, PASSWORD);
	            return mySQLconnection;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        }
	    }
	    
	    private static Connection getConnection() {
	    	return mySQLconnection;
	    }   
	}
}
