예제 #1
0
  public Connection getConnection() throws SQLException {
    // If we are already connected, test to see if we are
    // still connected. If not, reset our connection.
    if (this.connection != null && this.connection.isClosed())
      try {
        Log.debug("Closing dead JDBC connection");
        this.connection.close();
      } catch (final SQLException e) {
        // We don't care. Ignore it.
      } finally {
        this.connection = null;
      }

    // If we are not connected, we should attempt to (re)connect now.
    if (this.connection == null) {
      Log.debug("Establishing JDBC connection");
      // Start out with no driver at all.
      Class loadedDriverClass = null;

      // Try the system class loader instead.
      try {
        loadedDriverClass = Class.forName(this.conObj.getJdbcType().getDriverClassName());
      } catch (final ClassNotFoundException e) {
        final SQLException e2 = new SQLException();
        e2.initCause(e);
        throw e2;
      }

      // Check it really is an instance of Driver.
      if (!Driver.class.isAssignableFrom(loadedDriverClass))
        throw new ClassCastException(Resources.get("driverClassNotJDBCDriver"));

      // Connect!
      final Properties properties = new Properties();
      properties.setProperty("user", this.conObj.getUserName());
      if (!this.conObj.getPassword().equals(""))
        properties.setProperty("password", this.conObj.getPassword());
      properties.setProperty("nullCatalogMeansCurrent", "false");
      /*
       * this.connection = DriverManager.getConnection( overrideDataLinkSchema == null ? this.conObj.getJdbcUrl():
       * (this.conObj.getJdbcUrl()) .replaceAll(this.getDataLinkSchema(), overrideDataLinkSchema), properties);
       */

      this.connection =
          DriverManager.getConnection(
              this.conObj.getJdbcUrl(), conObj.getUserName(), conObj.getPassword());
      // Check the schema name.
      final DatabaseMetaData dmd = this.connection.getMetaData();
      final String catalog = this.connection.getCatalog();
      this.realSchemaName = this.getDataLinkSchema();
      ResultSet rs = dmd.getTables(catalog, this.realSchemaName, "%", null);
      if (!rs.next()) {
        rs = dmd.getTables(catalog, this.realSchemaName.toUpperCase(), "%", null);
        if (rs.next()) this.realSchemaName = this.realSchemaName.toUpperCase();
      }
      if (!rs.next()) {
        rs = dmd.getTables(catalog, this.realSchemaName.toLowerCase(), "%", null);
        if (rs.next()) this.realSchemaName = this.realSchemaName.toLowerCase();
      }
      rs.close();
    }

    // Return the connection.
    return this.connection;
  }