Exemplo n.º 1
0
  /**
   * Creates a connection using the specified properties.
   *
   * @param props the properties to connect with
   * @return a connection to the database
   * @throws SQLException if an error occurs
   */
  protected java.sql.Connection getConnection(Properties props) throws SQLException {
    String jdbcUrlToUse = null;

    if (!this.explicitUrl) {
      StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");

      if (this.hostName != null) {
        jdbcUrl.append(this.hostName);
      }

      jdbcUrl.append(":");
      jdbcUrl.append(this.port);
      jdbcUrl.append("/");

      if (this.databaseName != null) {
        jdbcUrl.append(this.databaseName);
      }

      jdbcUrlToUse = jdbcUrl.toString();
    } else {
      jdbcUrlToUse = this.url;
    }

    //
    // URL should take precedence over properties
    //

    Properties urlProps = mysqlDriver.parseURL(jdbcUrlToUse, null);
    urlProps.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    urlProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    urlProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);

    Iterator<Object> keys = urlProps.keySet().iterator();

    while (keys.hasNext()) {
      String key = (String) keys.next();

      props.setProperty(key, urlProps.getProperty(key));
    }

    return mysqlDriver.connect(jdbcUrlToUse, props);
  }