2

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 ?

nskalis
  • 2,232
  • 8
  • 30
  • 49
  • 1
    See my answer [here](http://stackoverflow.com/a/25614063/2144390) about setting up Jython for JDBC on *nix. You would need to have `/usr/lib/oracle/12.1/client64/lib/ojdbc6.jar` in your CLASSPATH (instead of the JARs I mention for UCanAccess). Note that it uses `from com.ziclix.python.sql import zxJDBC`. Note also that invoking Jython is done with a `jython` command, not `java -jar jython.jar` (at least that's how it worked for me). – Gord Thompson Jan 18 '15 at 15:38
  • Thanks Gord. I would need a bit of extra help if possible. – nskalis Jan 18 '15 at 16:28
  • UCanAccess, i guess is not needed because is not about the MS Access DB – nskalis Jan 18 '15 at 16:30
  • could you please explain how to call jython and update CLASSPATH ? – nskalis Jan 18 '15 at 16:30
  • am calling Jython like this now : ./jython but still not changed to the better – nskalis Jan 18 '15 at 16:35
  • See if the instructions [here](http://docs.oracle.com/javase/tutorial/essential/environment/paths.html) help you out. In my case all I did was add the CLASSPATH declaration and `export` command to my .bashrc file. – Gord Thompson Jan 18 '15 at 16:44
  • export PATH=$PATH:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/lib/jvm/java-1.7.0-openjdk-1.7 .0.55.x86_64/jre/bin – nskalis Jan 18 '15 at 17:08
  • Adding the jar location to the PATH environment variable won't help. CLASSPATH is where it needs to go. – Gord Thompson Jan 18 '15 at 17:13
  • jython.jar is under /home_ldap/nskalis/jython directory – nskalis Jan 18 '15 at 17:21
  • java executable is at /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/bin – nskalis Jan 18 '15 at 17:22
  • i updated the bash_profile like : export CLASSPATH=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.55.x86_64/jre/bin:$CLASSPATH – nskalis Jan 18 '15 at 17:22
  • what would be the exact line ? am sorry but am not getting it – nskalis Jan 18 '15 at 17:23
  • 1
    Try `export CLASSPATH=/usr/lib/oracle/12.1/client64/lib/ojdbc6.jar` – Gord Thompson Jan 18 '15 at 20:09
  • export CLASSPATH=/usr/lib/oracle/12.1/client64/lib/ojdbc6.jar ... worked. thanks. – nskalis Jan 19 '15 at 11:02

1 Answers1

1

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418