Example #1
0
  public void connect(String databaseName) throws TechnicalException {

    this.databaseParameter.setDatabaseName(databaseName);

    try {
      String connectionString = null;
      if (databaseParameter.getRdbs().isMySql()) {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connectionString =
            "jdbc:mysql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isPostgreSql()) {
        connectionString =
            "jdbc:postgresql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isOracle()) {
        connectionString =
            "jdbc:oracle:thin:@"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + ":"
                + databaseParameter.getDatabaseName();
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // Need to load driver
      }

      connection =
          DriverManager.getConnection(
              connectionString,
              databaseParameter.getDatabaseUser(),
              databaseParameter.getDatabasePassword());

    } catch (SQLException e) {
      throw new TechnicalException(e);
    } catch (InstantiationException e) {
      throw new TechnicalException(e);
    } catch (IllegalAccessException e) {
      throw new TechnicalException(e);
    } catch (ClassNotFoundException e) {
      throw new TechnicalException(e);
    }
  }
Example #2
0
  public Connection connect() throws TechnicalException {

    Connection connection = null;
    String connectionString = null;
    try {
      if (databaseParameter.getRdbs().isMySql()) {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        connectionString =
            "jdbc:mysql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isPostgreSql()) {
        DriverManager.registerDriver(new org.postgresql.Driver());
        connectionString =
            "jdbc:postgresql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isOracle()) {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connectionString =
            "jdbc:oracle:thin:@"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + ":"
                + databaseParameter.getDatabaseName();
      }

      connection =
          DriverManager.getConnection(
              connectionString,
              databaseParameter.getDatabaseUser(),
              databaseParameter.getDatabasePassword());

      if (databaseParameter.getRdbs().isPostgreSql()) {
        connection.setAutoCommit(
            false); // Need to explicitely set auto-commit to false (for streaming results)
      }
    } catch (SQLException e) {
      throw new TechnicalException(connectionString, e);
    }
    return connection;
  }