nskalis
nskalis

Reputation: 2382

python: how to connect to oracle database using jdbc

Could you please enlighten me on how to connect to an Oracle instance using Python/Jython ?

After installing fully Jython, the Oracle website (http://www.oracle.com/technetwork/articles/dsl/mastering-oracle-python-providers-1395759.html) suggests to : All you need to provide is to make sure ojdbc6.jar is in the CLASSPATH or JYTHONPATH so that the connection driver could be resolved.

I read that when using the -jar option ignores the CLASSPATH environment variable. So I did like :

java -classpath /usr/lib/oracle/12.1/client64/lib/ojdbc6.jar  -jar jython.jar

from java.sql import DriverManager
db_connection = DriverManager.getConnection("jdbc:oracle:thin:@xxxxx:1521/P1FNTPE", "xxx", "xxx")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

java.sql.SQLException: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@xxxxx:1521/P1FNTPE

Could you please help/advise me on how to resolve this issue ?

Upvotes: 2

Views: 16075

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123839

As mentioned in the question, the full path to the JAR file for the JDBC driver must be present in either the CLASSPATH or the JYTHONPATH environment variable so the Jython script can find it. These variables can be modified in several ways depending on the environment (shell) being used, as described in the Jata tutorial here:

PATH and CLASSPATH

In this particular case, simply adding the line

export CLASSPATH=/usr/lib/oracle/12.1/client64/lib/ojdbc6.jar

to one of the startup files (e.g., ~/.bash_profile, ~/.profile, ~/.bashrc, ...) and then logging back in (or running source on the file) was the solution.

Upvotes: 1

Related Questions