Create Oracle database JDBC connection in gradle

It is a usual  for a gradle script to get connected to a database like oracle. Doing this in gradle may seem pretty simple and straight forward. But sometimes things wont get right. If you are having hard time to get connected to oracle database, using java code inside gradle, this tutorial is for you.

Most usual error is not getting the dependencies resolved. The usual way that you will try is as follows:

The reason why we are picking the dependencies (ojdbc6.jar) from local maven repositories is because it is simply not available in the maven repo due to licensing issues. The above code has few problems also. Even if you get connected to the databse, you will get xml parsing errors while executing queries. For some people it will not even get connected. It will say wrong driver version and bla bla bla.

In order to get things right (the way which worked for me) you’ll have to do two things:

  1. Iterate through and load each class in a groovy sql class loader for the driver jar.
  2. Add oracle xml db java api to dependencies. (xdb.jar)

Download both jars and install in maven local repository. Follow the code below

The above code is almost complete and will do the trick for you. It basically loads the correct driver class from the jar so you will not get any issues resolving correct dependencies. I hope this helps.

Share this article:

Leave a Reply

Your email address will not be published. Required fields are marked *