Sunday, 25 September 2016

Java JDBC – java.sql.SQLException: Invalid Oracle URL Specified - Colon of difference


Many a times you find yourself staring at your screen in disbelief at some weird Oracle exception that would just not make sense. And the best part of these exceptions is that the fix for them is so simple that you just shake your head and wonder if things could not be more complicated than they already are. I was just going through some of the things that I had archived to blog about and found this one from April 2016. Now, let me give you some background. I was trying to connect to an Oracle database and design a database walker program. A DB walker basically will list out all the tables in a database schema and I wanted it to list out some 10 records from each table. Now, Java is my poison so I would be using Java and JDBC. So the first thing that you do is initialize the driver and obtain a connection to the database. This is shown in the next snippet. 

The program by itself is very simple. It connects to a database and if the connection is successful, it will print “Connection established” on the terminal.

Now, when I ran this program, it gave me this huge stack trace of an exception that was generated on line 15. The exception was something of this sort: -
“Invalid Oracle URL specified”  



I tried to figure out what this exception meant and it was clear that the connection statement was causing the issue. I checked with another program that I had and found that I had mentioned the driver name incorrect.

If you check the first snapshot, the getConnection method the part before @ is jdbc:oracle:thin

Actually, it has to be jdbc:oracle:thin:

I had missed the colon at the end!!!!
 
Once I corrected it, I was able to connect to the database as shown in the next snapshot.

No comments:

Post a Comment