francisden
francisden

Reputation: 1

JDBC Error: java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

I am creating a java scheduled task in OIM. Part of the code is to access OIM database via JDBC. My code below:

String URL = "jdbc:oracle:thin:@10.10.171.160:1523:IAMDB";
String USER = "username";
String PASS = "password";

Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(URL, USER, PASS);

I'm getting error in line 6

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

I tried accessing the database in SQL Developer using the same credentials and it's working

Upvotes: 0

Views: 1670

Answers (1)

Philipp Grigoryev
Philipp Grigoryev

Reputation: 2143

You can use jdbc Data Source configured on your WL server.

public static Optional<Connection> getDBConnection() {
  Optional<Connection> conn = Optional.empty();
  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
  
  try {
    Context context = new InitialContext();
    DataSource ds = (DataSource) context.lookup("jdbc/operationsDB");
    conn = Optional.of(ds.getConnection());
    
    logger.log(
      Level.INFO, "Database connection established successfully return with connection object");
  
  } catch (SQLException e) {
    logger.log(Level.SEVERE, e.getMessage(), e);
  } catch (NamingException e) {
    logger.log(Level.SEVERE, e.getExplanation(), e);
}
return conn;

}

Upvotes: 1

Related Questions