common/BaseService.java

package common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BaseService 
{
    Connection connection=null;

    public Connection getConnection()
    throws SQLException 
    {
        if (connection==null) 
        {
            try
            {
                String database = "todo";
                String username = "todo";
                String password = "todo";

                Class.forName("org.postgresql.Driver");; //load the driver
                connection = DriverManager.getConnection("jdbc:postgresql:"+database,
                        username,
                        password);
            }
            catch( ClassNotFoundException cnfe)
            {
                System.err.println("ClassNotFoundException: " + cnfe.getMessage());
            }
            if (connection==null) throw new SQLException("Could not acquire connection!");
            }
    	  return connection;
      }
    
    public void returnConnection( Connection c )
    throws SQLException
    {
        // thanks for returning, but I won't do anything with it now
    }
    
    public void dropConnection( Connection c )
    throws SQLException
    {
        // note: ignoring the 'c' argument!
        if (connection!=null);
        {
            connection.close();;
            connection=null;
        }
        
    }
}

© Willem Moors, 2009 - 2013