Source for org.jfree.report.modules.data.sql.DriverConnectionProvider

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: DriverConnectionProvider.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.data.sql;
  32: 
  33: import java.sql.Connection;
  34: import java.sql.DriverManager;
  35: import java.sql.SQLException;
  36: import java.util.Properties;
  37: 
  38: /**
  39:  * Creation-Date: 07.04.2006, 15:04:26
  40:  *
  41:  * @author Thomas Morgner
  42:  */
  43: public class DriverConnectionProvider implements ConnectionProvider
  44: {
  45:   private Properties properties;
  46:   private String url;
  47:   private String driver;
  48:   private transient Connection connection;
  49: 
  50:   public DriverConnectionProvider()
  51:   {
  52:     this.properties = new Properties();
  53:   }
  54: 
  55:   public String getProperty(final String key)
  56:   {
  57:     return properties.getProperty(key);
  58:   }
  59: 
  60:   public Object setProperty(final String key, final String value)
  61:   {
  62:     return properties.setProperty(key, value);
  63:   }
  64: 
  65:   public String getUrl()
  66:   {
  67:     return url;
  68:   }
  69: 
  70:   public void setUrl(final String url)
  71:   {
  72:     this.url = url;
  73:   }
  74: 
  75:   public String getDriver()
  76:   {
  77:     return driver;
  78:   }
  79: 
  80:   public void setDriver(final String driver)
  81:   {
  82:     this.driver = driver;
  83:   }
  84: 
  85:   public Connection getConnection() throws SQLException
  86:   {
  87:     if (url == null)
  88:     {
  89:       throw new NullPointerException("URL must not be null when connecting");
  90:     }
  91: 
  92:     if (connection == null)
  93:     {
  94:       try
  95:       {
  96:         if (driver != null)
  97:         {
  98:           Class.forName(driver);
  99:         }
 100:       }
 101:       catch(Exception e)
 102:       {
 103:         throw new SQLException("Unable to load the driver", e.getMessage());
 104:       }
 105: 
 106:       connection = DriverManager.getConnection(url, properties);
 107:     }
 108:     return connection;
 109:   }
 110: }