⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionentry.java

📁 报表设计软件,很好的
💻 JAVA
字号:
/**
 * ========================================================================================
 * JFreeReport Designer : a graphical designer for JFreeReport - a free Java report library
 * ========================================================================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner (taquera@sherito.org);
 *
 * (C) Copyright 2003, by Heiko Evermann (heiko@evermann.de) and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package org.jfree.designer.db;

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

import org.jfree.report.util.Log;

public final class ConnectionEntry
{
  private String driverClass;
  private String url;
  private String username;
  private String password;
  private String database;
  private String name;
  private boolean savepwd;

  public ConnectionEntry (final Properties p, final int entry)
  {
    name = p.getProperty("CON" + entry + "_NAME");
    if (name == null)
    {
      throw new NullPointerException("Name is not set.");
    }
    driverClass = p.getProperty("CON" + entry + "_DRIVER");
    if (driverClass == null)
    {
      throw new NullPointerException("DriverClass is not set.");
    }
    url = p.getProperty("CON" + entry + "_URL");
    if (url == null)
    {
      throw new NullPointerException("URL is not set.");
    }
    username = p.getProperty("CON" + entry + "_USERNAME");
    database = p.getProperty("CON" + entry + "_DATABASE");
    password = p.getProperty("CON" + entry + "_PASSWORD");
    savepwd = (password != null);
  }

  public ConnectionEntry (final String driverClass, final String url,
                          final String username, final String password,
                          final String database, final String name,
                          final boolean savepwd)
  {
    if (url == null)
    {
      throw new NullPointerException("URL is not set.");
    }
    if (driverClass == null)
    {
      throw new NullPointerException("DriverClass is not set.");
    }
    if (name == null)
    {
      throw new NullPointerException("Name is not set.");
    }
    this.driverClass = driverClass;
    this.url = url;
    this.username = username;
    this.password = password;
    this.database = database;
    this.name = name;
    this.savepwd = savepwd;
  }

  public final void saveIntoProperties (final Properties p, final int entry)
  {
    p.setProperty("CON" + entry + "_NAME", name);
    p.setProperty("CON" + entry + "_DRIVER", driverClass);
    p.setProperty("CON" + entry + "_URL", url);
    p.setProperty("CON" + entry + "_USERNAME", username);
    if (savepwd == true)
    {
      p.setProperty("CON" + entry + "_PASSWORD", password);
    }
    p.setProperty("CON" + entry + "_DATABASE", database);
  }

  public final String toString ()
  {
    return name;
  }

  public final String getDriverClass ()
  {
    return driverClass;
  }

  public final String getUrl ()
  {
    return url;
  }

  public final String getUsername ()
  {
    return username;
  }

  public final String getPassword ()
  {
    return password;
  }

  public final String getDatabase ()
  {
    return database;
  }

  public final String getName ()
  {
    return name;
  }

  public final boolean isSavepwd ()
  {
    return savepwd;
  }

  public final boolean equals (final Object o)
  {
    if (this == o)
    {
      return true;
    }
    if (!(o instanceof ConnectionEntry))
    {
      return false;
    }

    final ConnectionEntry connectionEntry = (ConnectionEntry) o;

    if (database != null)
    {
      if (!database.equals(connectionEntry.database))
      {
        return false;
      }
    }
    else
    {
      if (connectionEntry.database != null)
      {
        return false;
      }
    }
    if (driverClass != null)
    {
      if (!driverClass.equals(connectionEntry.driverClass))
      {
        return false;
      }
    }
    else
    {
      if (connectionEntry.driverClass != null)
      {
        return false;
      }
    }
    if (name != null)
    {
      if (!name.equals(connectionEntry.name))
      {
        return false;
      }
    }
    else
    {
      if (connectionEntry.name != null)
      {
        return false;
      }
    }
    if (url != null)
    {
      if (!url.equals(connectionEntry.url))
      {
        return false;
      }
    }
    else
    {
      if (connectionEntry.url != null)
      {
        return false;
      }
    }
    if (username != null)
    {
      if (!username.equals(connectionEntry.username))
      {
        return false;
      }
    }
    else
    {
      if (connectionEntry.username != null)
      {
        return false;
      }
    }

    return true;
  }

  public final int hashCode ()
  {
    int result;
    result = (driverClass != null ? driverClass.hashCode() : 0);
    result = 29 * result + (url != null ? url.hashCode() : 0);
    result = 29 * result + (username != null ? username.hashCode() : 0);
    result = 29 * result + (database != null ? database.hashCode() : 0);
    result = 29 * result + (name != null ? name.hashCode() : 0);
    return result;
  }

  public final Connection createConnection ()
          throws SQLException
  {
    try
    {
      Class.forName(driverClass).newInstance();
      final Connection conn = DriverManager.getConnection(url, username, password);
      return conn;
    }
    catch (SQLException sqle)
    {
      throw sqle;
    }
    catch (Exception ex)
    {
      Log.debug("Failed to create connection: ", ex);
      throw new SQLException("Failed to create connection: " + ex.toString());
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -