1

I need to connect to oracle db in java

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=servicename)))");

I am getting below error

java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.core.exceptions.WrongArgumentException: Malformed database URL, failed to parse the main URL sections.
Dolly
  • 97
  • 3
  • 18
  • 1
    Possible duplicate of [Java JDBC - How to connect to Oracle using Service Name instead of SID](https://stackoverflow.com/questions/4832056/java-jdbc-how-to-connect-to-oracle-using-service-name-instead-of-sid) – x80486 Jul 24 '19 at 16:40
  • 1
    Are you sure you have the Oracle JDBC driver on the class path? That error is from the MySQL Connector/J driver, and should not occur when trying to connect with Oracle (unless the MySQL Connector/J implementation doesn't correctly filter JDBC connection strings by vendor subprotocol, which would be a bug in MySQL Connector/J). – Mark Rotteveel Jul 24 '19 at 16:42
  • Ohh then what can be the possible solution for now to proceed @MarkRotteveel – Dolly Jul 24 '19 at 16:45
  • Remove MySQL Connector/J from the classpath and see if you can connect. However, probably you don't have the Oracle JDBC driver on the classpath, because `DriverManager` will try all loaded drivers until a non-null `Connection` is returned, or all drivers either returned null or threw an exception. – Mark Rotteveel Jul 24 '19 at 16:45
  • Try to register Oracle's driver with `DriverManager.registerDriver(new oracle.jdbc.OracleDriver());` That's not mandatory, but if there's no driver in your classpath - you'll get an error. – Pavel Smirnov Jul 24 '19 at 16:46
  • @PavelSmirnov User code should not call `registerDriver`, loading the class with `Class.forName` is enough. The driver will register itself when loaded. – Mark Rotteveel Jul 24 '19 at 16:47
  • @MarkRotteveel, that's just to check if the driver exists in the class path. – Pavel Smirnov Jul 24 '19 at 16:48
  • @PavelSmirnov Using `Class.forName` is enough. It will check existence on the classpath and it loads the driver. It also avoids registering the driver twice. – Mark Rotteveel Jul 24 '19 at 16:48
  • @MarkRotteveel tried removing that mysql connector and getting the same error. Also ojdbc8 driver is present in classpath – Dolly Jul 24 '19 at 16:50
  • If you get the exact same error after you removed MySQL Connector/J, then you still have another copy of MySQL Connector/J on the classpath. The exception in your question is from the MySQL Connector/J driver. – Mark Rotteveel Jul 24 '19 at 16:51
  • @MarkRotteveel i checked all jars and no mysql drivers are presnt. But cassandra is present .. shall i remove those as well? – Dolly Jul 24 '19 at 17:10
  • @MarkRotteveel after removing all jars except ojdbc, I am getting new error as below `java.sql.SQLException: invalid arguments in call` – Dolly Jul 25 '19 at 04:56
  • Please [edit] your question and provide the full stacktrace – Mark Rotteveel Jul 25 '19 at 08:13
  • Possible duplicate: https://stackoverflow.com/questions/57427713/getting-mysql-error-stack-trace-on-oracle-jdbc-connection – Mark Rotteveel Aug 09 '19 at 13:49

1 Answers1

1

Use this format

jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICE_NAME

Example:

jdbc:oracle:thin:@//10.25.18.122:1524/abcxyz
Muhammad Saimon
  • 233
  • 2
  • 10