17

I'm trying to connect to a local oracle database but I'm getting this cryptic error message: Invalid Oracle URL specified: OracleDataSource.makeURL.

I'm pretty sure this is due to an error with the database connection parameters I'm passing, but really, this error message does not help me in any way. Any hints as to what I'm doing wrong would be greatly appreciated.

FYI: Code used to connect is below, except for the hardcoded strings this is what is used on our production environment and does work there.

OracleDataSource dataSource = new OracleDataSource();
 dataSource.setServerName("localhost");
 dataSource.setUser(userName);
 dataSource.setPassword(password);
 dataSource.setDatabaseName("orcl");
return dataSource.getConnection();
Ticcie
  • 810
  • 1
  • 6
  • 14
  • Just handed the cryptic error message over to Google - did you check those search results already? Some of them sound promising. – Andreas Dolk Aug 26 '09 at 07:23
  • 2
    Checked those and non of them explain why or when this error message is given. This is just an illustration of why writing error messages is hard, this generic message just tells me "You did something wrong", yeah, I get that by now, now enable me to fix it. – Ticcie Aug 26 '09 at 07:41

3 Answers3

31

if you use setUrl (or if your container like glassfish does it)

make sure you use correct syntax

==== Notice the colon before @ ====

jdbc:oracle:thin:@localhost:1521:sid

or

jdbc:oracle:thin:@localhost:1521/servicename

The datasource class tries to parse it and gives cryptic error if syntax has issues

Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • The service name syntax should use forward slash otherwise you'll get a java.sql.SQLRecoverableException (java.sql.SQLRecoverableException: IO Error: Invalid number format for port number) jdbc:oracle:thin:@localhost:1521/servicename – kri Aug 31 '16 at 07:20
  • I had to use the TNS name instead of sid, like in **jdbc:oracle:thin@localhost:1521:tnsname**, and then I set the user and password and it worked – Sameer Achanta Dec 21 '17 at 08:48
  • 4
    The hard part for me was seeing that I didn't have a ":" just before the "@". I must have looked at it for 30 minutes. :( – kc2001 Feb 14 '18 at 13:20
  • @kc2001 That really helps, I actually omitted the `:` before the `@`. – user3437460 Apr 18 '18 at 02:29
  • 1
    You are awesome. The much overlooked part. This answer clearly nailed my issue. Thanks! – Marco99 Aug 22 '18 at 11:33
  • 1
    Thank you SO MUCH, @Kalpesh Soni. This little "detail" of the "/" instead of ":" made all the difference, since I am using a PDB and servicename is the rule instead of SID. THANK YOU VERY MUCH INDEED!!! – jaymzleutz Mar 01 '21 at 23:30
19

Surprisingly, after adding the following two lines to the code which created the connection, it worked.

dataSource.setPortNumber(1521);
dataSource.setDriverType("thin");

I don't understand why we did not have that problem before, but that may have something to do with my local install. My biggest beef is with the error message not giving any detail about what is wrong though.

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Ticcie
  • 810
  • 1
  • 6
  • 14
1

For WildFly users (v14.0.1.Final currently).

You need to add additional parameters for DataSource:

DataSource -> Connection -> Connection Properties

driverType=thin (press Enter to complite).

Also doublecheck you use right type of DataSource. In my case problem was solved by creating XA DataSource and pass URL to parameter.

Mirimas
  • 735
  • 10
  • 15